Java Syntax & Structure
In this page:
public class ClassName {
public static void main(String[] args) {
statement; // each statement ends with ;
}
}
Case Sensitivity
Java treats uppercase and lowercase letters as entirely different characters, so myVariable and MyVariable are two distinct identifiers -- this trips up beginners coming from case-insensitive languages.
Example: Case Sensitivity
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare myVariable and set it to 1
int myVariable = 1;
// Declare MyVariable and set it to 2
int MyVariable = 2;
// Print a line to the console
System.out.println(myVariable + " and " + MyVariable + " are different variables.");
}
}
Login to try C/C++/Java/PHP code in the editor
Curly Braces
Every class body, method body, loop, and conditional block is delimited by a matching pair of curly braces, and a missing or misplaced brace is one of the most common compiler errors you'll encounter.
Example: Curly Braces
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Check whether true
if (true) {
// Print a line to the console
System.out.println("Inside the if block");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Class Naming Conventions
Convention (enforced by tooling, not the compiler) says class names use PascalCase, capitalizing the first letter of each word, like BankAccount or HttpClient -- following it keeps your code readable to other Java developers.
Example: Class Naming Conventions
// Define the class BankAccount
class BankAccount {
double balance;
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new BankAccount object and store it in account
BankAccount account = new BankAccount();
// Print a line to the console
System.out.println("Class named in PascalCase: BankAccount");
}
}
Login to try C/C++/Java/PHP code in the editor
Method Naming Conventions
Method names follow camelCase instead, starting lowercase, like calculateTotal() -- this visual distinction from PascalCase class names helps you tell at a glance whether an identifier is a type or a behavior.
Example: Method Naming Conventions
// Define the class Main
public class Main {
static int calculateTotal(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Method named in camelCase: " + calculateTotal(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Indentation and Whitespace
The compiler doesn't care how you indent your code, but consistent indentation (typically 4 spaces per nesting level) is what makes deeply nested loops and conditionals actually readable to a human.
Example: Indentation and Whitespace
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 2; i++) {
// Print a line to the console
System.out.println("Consistently indented nested block: " + i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Using the wrong case, such as
stringinstead ofString, since Java is case-sensitive. - Forgetting to close a curly brace, so the compiler reports an error at the end of the file rather than where the brace is missing.
- Leaving out the semicolon at the end of a statement, which gives
';' expected.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: