Basics

Statement

Java

 

Class

Java 8
public class MyClass {
  
  public static void main(String[] args) {
    System.out.println("Hello World!");  // Hello World!
  }
  
}

Java is an Object Oriented Programming language, and Every piece of code should be inside some Class.

We may ignore Class and Method definitions from time to time in our examples for readability purposes.

The Sample code here will print Hello World! on the console.

 

Statement

Java 8
System.out.println("statement 1");
System.out.println("statement 2");
//...

Statements are similar to sentences in natural languages.

Statement is a complete unit of execution.

Statements should be terminated with a semicolon (;).

 

Whitespaces

Java 8
System  .out. println (  "statement 1" );

System.out.println
  ("statement 2");

System.out.println(
  "statement 3"
);

Java ignores Whitespaces.

Space, Tab, Line-breaks are all considered whitespace.

You can use this as a tool to make your code more readable.

 

Case Sensitivity

Java 8
int age = 12;

System.out.println(age);  // 12
System.out.println(Age);  // error
System.Out.PrintLN(age);  // error

Java is a Case Sensitive language.

 

Blocks

Java 8
{
  System.out.println("statement 1");
  System.out.println("statement 2");
}

Block is used to group zero or more statements.

Blocks are indicated by a pair of curly brackets.

Block can be prefixed or postfixed by other keywords like if, while to run them in a specific way.

 

Block Label

Java 8
myblock: {
    System.out.println("statement 1");
    System.out.println("statement 2");
}

Block may optionally be labeled.

These labels can then be used by continue and break keywords.

 

Empty Statement

Java 8
;

An empty statement is used to provide no statement, although Java syntax would expect one.

For example, if you want to run a for loop without a block.