Wednesday, 25 April 2018

Core Java: Comments

There are two kinds of comments in Java:

1.      Multi-Line Comment: begins with a /* and ends with a */, and can span multiple lines.

2.      End-of-Line (Single-Line) Comment: begins with // and lasts till the end of the current line.

Statements and Blocks

Statement: A programming statement must be terminated by a semi-colon (;), just like an English sentence ends with a period. (Why not ends with a period like an English sentence? This is because period crashes with decimal point - it is hard for the dumb computer to differentiate between period and decimal point in the early days of computing!)

For examples,

// Each of the following lines is a programming statement, which ends with a semi-colon (;)

int number1 = 10;

int number2, number3=99;

int product;

product = number1 * number2 * number3;

System.out.println("Hello");

Block: A block is a group of statements surrounded by a pair of curly braces { }.

// Each of the followings is a "complex" statement comprising one or more blocks of statements.

// No terminating semi-colon needed after the closing brace to end the "complex" statement.

// Take note that a "complex" statement is usually written over a few lines for readability.

if (mark >= 50) {

   System.out.println("PASS");

   System.out.println("Well Done!");

   System.out.println("Keep it Up!");

}



if (number == 88) {

   System.out.println("Got it!");

} else {

   System.out.println("Try Again!");

}



i = 1;

while (i < 8) {

   System.out.print(i + " ");

   ++i;

}



public static void main(String[] args) {

   ...statements...

}

No comments:

Post a Comment

Please write your view and suggestion....