Created: 2023-08-22 21:51
Status: #concept
Subject: Programming
Tags: Programming Language Java Data Type Java Class Java Package Object-Oriented Programming
Java
A strongly-typed Compiled Object-oriented programming language which encodes programs into Java Bytecode which can be Interpreted and ran in any machine with a Java Virtual Machine (JVM).
public class Hello {
/* this is the "entry point" of the .java program when we run it */
public static void main(String[] args) {
System.out.println("You passed in: ", + args[0]);
}
}
public
means that theclass Hello
's function can be invoked outside the class inside other classes within the same Java Package.static
means that we have to call the function as a Method together with the class name, ie.Hello.main( ... );
without importing theHello
class.void
is the return type of themain
Function.String[]
is the type of the Parameter, an array ofString
objects.args
is the parameter name we can use the access the arguments passed into the functionmain
when we runjava Hello.class "I am args"
in a CLI.
$ javac Hello.java
$ java Hello someArgument
You passed in: someArgument
Writing A Java Program
Every
.java
has some constraints before we can compile them into a .class
with javac
.
- We need to declare a Java Package
package scope;
at the start of the source file which corresponds to thedirectory/
the code is in,package parent.child
corresponds toparent/child/Class.java
. - Afterwards, we can write our
import
statements to import code from other packages. - The filename
Name.java
must correspond to the mainpublic class Name
which contains apublic static void main(String[] args)
Method as the entrypoint.
package shipping.reports;
import shipping.domain.*; // * denotes the wildcard RegEx character
import java.util.List;
import java.io.*; // imports all files with the 'java.io.' scope prefix
public class VehicleCapacityReport {
private List vehicles;
public void generateReport(Writer output) {...}
}
Standard Library Utilities
Common Pitfalls
- there are no Truthy or Falsy Values when passing other data types like
int
into awhile(bool expr) { ... }
loop because they are not compatible types.- We need to typecast them first via
PrimitiveType.valueOf
or use Comparison Operators to make a boolean expression.
- We need to typecast them first via
- values are internally UTF-16 Unicode characters.
- Java does not have undefined declared variables, it shifts all bits to
0
. - ALL non-primitive types (Objects) are Reference Types, so they are passed by Memory Address and are mutable when passed into functions.
Unit Testing
Any class with the suffix
Test
will be included in the "Test Packages" directory in the Netbeans IDE.Without it, the tests in the class will not be executed.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void calculatorInitialValueZero() {
Calculator calculator = new Calculator();
assertEquals(0, calculator.getValue());
}
}
- see JUnit Documentation for more info on
org.junit
.