The Best Technical Blog Ever

twitterrss

Back to Basics: Java Syntax Reference

In this session, you are going to refresh some basic set of rules(java syntax), defining how a Java program is written and interpreted. After learning the basics, you'll find that you can easily grab onto more advanced concepts.


1.Getting Started:

i. The first step in java programming is adding a main public class to your source file(Sample.java). The curly braces following the class name delimits what belongs to the class. The braces, along with their content, is referred to as a code block, or just a block.


public class Sample{}

ii. For this class, add the main method. It is the starting point for your program execution.


public class Sample{
	public static void main(String[] args) {}
}

iii.Printing the result is done with System.out.print() method.


System.out.println("text");
System.out.print("text");
System.out.printf("value: % 4d", intValue);

iv. For compiling,


DOS> javac Sample.java
(in Eclipse: just save file)

v. For Executing,


DOS> java Sample
In Eclipse: Right click, Run As, Java Application

2.Comments:

Comments are used to insert notes into the source code and will have no effect on the end program.


// single-line comment
					
/* multi-line
comment */

/** javadoc
comment */

3. Variables:

A variable is used to provide some named storage that can be manipulated by our programs. Each variable has a specific data type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Variables must be defined before they are used.


int result;    		//variable declaration
result=20;			//variable initialization (assigned a value)
int result=20;  		//variable declaration + initialization
int a = 10, b = 20, c;  	//declaring more than one variable
									
									
char option = 'A';
boolean boo = false;
double r = 6.44;
float r=3.14; 			// error: possible loss of precision
float r=3.14F;
float r = (float)3.14;   //explicit cast

In java, constant floating-point numbers are always kept internally as doubles. Therefore, whenever you try to assign a double to a float you will get an error, because a double has a higher precision than a float. To fix this problem, you can append an "F" character to the constant, which says that the number is a float.


4. Conditional statements:

These statements execute the block of code based on given conditions.


a. if statement:
if (Boolean Expression) {
   // Code block to get executed if the Boolean expression is true
}
Example:
if (a ==b) {
	System.out.print("a and b are equal");
}

b. if...else Statement:   
if(Boolean_expression){
     //Executes if the Boolean expression is true
}else{
     //Executes if the Boolean expression is false
}
Example:
if (a>b) {
	System.out.print(a+ "is big");
}else{
	System.out.print(b+ "is big");
}


c. if...else if... Statement:
if(Boolean_expression 1){
     //Executes if the Boolean expression 1 is true
}else if(Boolean_expression 2){
      //Executes if the Boolean expression 2 is true
}else {
     //Executes when the above conditions are false.
}
Example:
if (a>80) {
	System.out.print("first class"");
}else if(a>60){
	System.out.print("second class");
}else{
	System.out.print("you mark is below 60");
}

d. nested if...else Statement:
if(Boolean_expression 1){
        //Executes if the Boolean expression 1 is true
      if(Boolean_expression 2){
   	      //Executes if the Boolean expression 2 is true
      }
}else{
       //Executes when the above conditions are false.
}
Example:
if (mark>35) {
	if (mark>80) {
		System.out.print("first class");
	}
}else{
	System.out.print("fail");
}

e. switch Statement:
switch(expression){
    case value1 :
    	//Statements
    case value2 :
         //Statements
          .
          .
    default :
        //Statements
}
Example:
switch (c){
	case 0: System.out.print(c + " is 0"); break;
	case 1: System.out.print(c + " is 1"); break;
	default:System.out.print(c + " is something else");
}

5. Loop statements:

Loop allows a block of statements to be executed repetitively.


a. while loop:
while(Boolean_expression){
   //Body of the loop
}
Example:
int i = 0;
while (i < 5) { 
	System.out.print(i++); 
}

b. do...while Loop:   
do{
   	//body of the loop
}while(Boolean_expression);
Example:
int i = 0;
do{ 
	System.out.print(i++); 
}while (i < 5);

c. for Loop:
for(initialization; Boolean_expression; increment/decrement)
{
   	//body of the loop
}
Example:
for (int i = 0; i < 5; i++){
	System.out.print(i); 
}  

d. foreach/ Enhanced for loop:
for(declaration : expression)
{
    	//body of the loop
}
Example:
int[] myArray = { 125,56,33 };
for (int element : array) {
	System.out.print(element); 
}

6. break, continue and return:

The break keyword ends the loop structure, and continue skips the rest of the current iteration and continues at the beginning of the next iteration. The return statement is used in methods to return a value and transfers the control back to the calling method.


break; 	 // stop the iteration and transfers control to the line immediate outside of loop
continue;	  // start next iteration
return x++;	 //return a value and transfers the control back to the calling method.
return;

For a complete detail of the Java Programming language, it is recommended to visit Java Tutorial.

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status