Your first Java program
1 | public class HelloWorld { |
Java is case-sensitive: HelloWorld and helloWorld are different identifiers.
Classes and source files
class declares a class, and public makes it accessible from other packages subject to Java’s access rules. A public top-level class must be stored in a file with the same name, so the example belongs in HelloWorld.java.
Class names conventionally use upper camel case:
1 | public class StudentRecord { |
The main method
For a conventional standalone application, the JVM begins with:
1 | public static void main(String[] args) { |
System.out.println() writes a value followed by a line terminator.
Identifiers
Identifiers name classes, variables, methods, interfaces, and other program elements. A Java identifier:
- may begin with a Unicode letter,
_, or$; - may contain digits after the first character;
- is case-sensitive;
- cannot be a keyword or reserved literal.
Although $name and _value can be legal, ordinary application code typically uses names such as studentName, calculateTotal, and OrderService.
Comments
Single-line comments begin with //:
1 | // Print a greeting. |
Block comments use /* ... */:
1 | /* This explanation can span |
Documentation comments begin with /** and can be processed by Javadoc:
1 | /** |
Comments should explain intent and constraints rather than restating obvious code. Blank lines are ignored by the compiler and can help separate logical sections.
Keywords
Words such as class, public, private, static, final, if, else, switch, for, while, new, return, try, catch, throw, extends, and implements have language-defined meanings and cannot be used as identifiers. Java keywords are lowercase.