Java Basic Syntax


Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Class & Object


ClassName.java

public class ClassName {

	// Access_Specifier ReturnType MethodName
	public void methodName() {
		
		// Statement
	}
}
class MainClassName {
	
	public static void main(String args[]) {
		
		ClassName objectName = new ClassName();
		
		objectName.methodName();
	}
}


B. Data Types


ClassName.java

	int intVal;
	float floatVal;
	double doubleVal;
	boolean booleanVal;
	char charVal;
	String StrVal;
	
	int[] intArray = new int[10];
	float[] floatArray = new float[10];
	double[] doubleArray = new double[10];
	boolean[] booleanArray = new boolean[10];
	char[] charArray = new char[10];
	String[] StrArray = new String[10];



C. If Loop


ClassName.java

	if(val1 condition_check val2) {
		
		// Statement
	}

	if(val1 condition_check val2) {
		
		// Statement
	}
	else {
		
		// Statement
	}

	if(val1 condition_check val2) {
		
		// Statement
	}
	else if (val3 condition_check val4) {
		
		// Statement
	}

	if(val1 condition_check val2) {
		
		// Statement
	}
	if(val3 condition_check val4) {
		
		// Statement
	}
	if(val5 condition_check val6) {
		
		// Statement
	}


D. for Loop


ClassName.java

	for(initialize; condition_check; increment/ decrement) {
		
		// Statement
	}

	for(int i=0; i<n; i++) {
		
		// Statement
	}


E. while & do-while


ClassName.java

	while(condition_check) {
		
		// Statement
	}

	while(i < n) {
		
		// Statement
	}

	do {
		
		// Statement
	} while (condition_check);

	do {
		
		// Statement
	} while (i < n);


F. switch case


ClassName.java

	switch(caseValue) {
 
		case caseValue1 :
			// Statement-1 if caseValue1 = caseValue
			break;
		case caseValue2 :
			// Statement-2 if caseValue2 = caseValue
			break;
		case caseValue3 :
			// Statement-3 if caseValue3 = caseValue
			break;
		default :
			// Statement-4 if caseValue is not available..
	}


G. Constructor


ClassName.java

public class ClassName {
	
	ClassName() {
		
		// Statement
	}
}
class MainClassName {
	
	public static void main(String args[]) {
		
		new ClassName();
	}
}






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com