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]);
  }
}
$ 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.

  1. We need to declare a Java Package package scope; at the start of the source file which corresponds to the directory/ the code is in, package parent.child corresponds to parent/child/Class.java.
  2. Afterwards, we can write our import statements to import code from other packages.
  3. The filename Name.java must correspond to the main public class Name which contains a public 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

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());
    }
}

References