Packages are used to group related classes, interfaces and sub-packages.
Helps in avoiding name collision and access control
It is recommended to put each new type in a package. If not, they are part of the default package
We lose the benefits of having a package structure and we can’t have sub-packages
We can’t import the types in the default package from other packages
The protected and package-private access scopes would be meaningless
Naming
All lower case
Period delimited
Reversed domain + sub package name
In case of a name conflict, use the fully qualified name
Program structure
public class SimpleAddition { public static void main(String[] args) { int a = 10; int b = 5; double c = a + b; System.out.println( a + " + " + b + " = " + c); }}
The basic unit of a Java program is a class
For a class to be executable, it needs a main method
The args in the main method is the command-line arguments
We can define more the one main method in an application but we have to specify which method the JVM uses with a MANIFEST.MF file
Execution
javac SimpleAddition.javajava SimpleAddition
We first compile the source code to generate the bytecode (.class file) using javac