Java – Syntax


  • // Denotes a line comment
  • /* Denotes a block comment that can span multiple lines */
  • /** Comment notation for extracting documentation via javadoc

Naming Conventions

  • Start the names of variables, methods, and objects with a lowercase letter. Indicate work boundaries with an uppercase letter, and restrict the remaining characters to digits and lowercase letters.
  • Start the names of classes with an uppercase letter and, otherwise, adhere to the rules above


  • Constants of type char are expressed with single quotes
  • Constants for strings are characters expressed with double quotes
  • boolean type constants must be spelled with lowercase letters

Naming Constants

  • Prevents value from being changed inadvertently
  • All upper case with underscores in between words
  public static final int INCHES_PER_FOOT = 12;
  public static final double RATE = 0.14;


Variable Declarations
Every variable in a Java program must be declared before it is used

  • A variable declaration tells the compiler what kind of data (type) will be stored in the variable
  • The type of variable is followed by one or more variable names separated by commas, and terminated with a semicolon
  • Variables are typically declared just before they are used or at the start of a block
// Local Variable
// age is a local variable that is only available inside the Test method

public class Test{ 
   public void pupAge(){
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   public static void main(String args[]){
      Test test = new Test();

// Instance Variable
// Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
// Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.
// Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.


public class Employee{
   // this instance variable is visible for any child class.
   public String name;
   // salary  variable is visible in Employee class only.
   private double salary;
   // The name variable is assigned in the constructor. 
   public Employee (String empName){
      name = empName;

   // The salary variable is assigned a value.
   public void setSalary(double empSal){
      salary = empSal;
   // This method prints the employee details.
   public void printEmp(){
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);

   public static void main(String args[]){
      Employee empOne = new Employee("Ransika");

Shorthand Assignment Statements

  count += 5; 
    is equivalent to 
  count = count + 5;

  amount *= count1 + count2; 
    is equivalent to 
  amount = amount * (count1 + count2);

Increment and Decrement

  • n = 2; 2*(++n) evaluates to 6
  • n = 2; 2*(n++) evaluates to 4

Escape Sequences

  • A backslash (\) immediately preceding a character without spaces denotes an escape sequence
    • The character following the backslash does not have its usual meaning
    • It is viewed as a single character

And Or

  • And -> &&
    • & // can be used to require program to evaluate both arguments
  • Or -> ||
    • | // can be used to require program to evaluate both arguments


  • statement will immediately end the program as soon as it is invoked:

// The exit statement takes one integer argument
  // By tradition, a zero argument is used to indicate a normal ending of the program

ASCII Ordering