English
Follow Us Online
Twitter    YouTube(TM)
Call a method
Call method in Class
Call a Class
Call Super Class method
Check if a file exists
Check String is Number
Declare a variable
Declare a String
Declare an Array
Declare constants
Define enum
Define a method
Exit program
Exit a for loop
Exit a method
Format date
Format double
Find current directory
Get current date
Get user input
Get Class name
Java How to A B C D E F G H I J K L M N O P Q R S T U V W Y W Z
Java How to declare a variable
Related Links Declare a variable Declare a String Declare an Array Next Page >>>
Notes:
  1. Syntax is variable_type variable_name1,variable_name2.
  2. Declaration and initialization of variables sntax is variable_type variable_name1 = variable_value.
/**
 * Example showing how to declare a variable
 * 
 * 
 * @author www.f5java.com
 * @return : void
 */
public void declareVariableExample() {

	int a, b; 		// 1. Declare variable_type variable_name1,variable_name2
	double amount; 	// 2. Declaring a variable of type double

	// 3. Declaration cum initialization of variables
	int i = 10;
	char ch = 'c';

	System.out.println("Value of the integer variable i is :: " + i);
	System.out.println("Value of char variable ch is :: " + ch);
} 
Java How to declare an Array
Notes:
  1. Arrays in Java are objects.
  2. Array index always starts from 0.
  3. Array length is obtained using the value of 'length' instance variable of array object.
Steps:
  1. Declare array_type[] array_name.
  2. Set the array size.
  3. Initialize the array using index.
  4. Display the array elements.
  5. Alternatively, declare and initialize the array with a single statement.
  6. Display numbers array elements.
/**
 * Example demonstrating declaration, initialization and 
 * looping of an Array
 * 
 * @author www.f5java.com
 * @return : void
 */
public void declareArrayExample() {
	int[] int_array; 			// 1. Declare array_type[] array_name;
	int_array = new int[5]; 	// 2. Set the array size to 5

	// 3. Initialize the array using index; array index starts from 0
	int_array[0] = 1;
	int_array[1] = 4;
	int_array[2] = 8;
	int_array[3] = 2;
	int_array[4] = 9;

	// 4. Display the int_array elements
	for (int i = 0; i < int_array.length; i++) {
		System.out.println("Element at position " + i + " is " + int_array[i]);
	}

	// 5. Alternatively, declare and initialize the array with a single statement
	int[] numbers = { 4, 7, 3, 0, 6 };

	// 6. Display numbers array elements
	for (int i = 0; i < numbers.length; i++) {
		System.out.println("Element at position " + i + " is " + numbers[i]);
	}
}
     
Java How to declare a String
Notes:
  1. Declaration and initialisation example shown below.
/**
 * Example demonstrating declaration and initialization of
 * String
 * 
 * @author www.f5java.com
 * @return : void
 */
public void declareStringExample() {
	String firstString; 	// 1. Declare a String
	firstString = "Hello";  // 2. Initialize the string variable declared above
	
	String secondString = "World"; // 3. Declaration + inialization at the same time
	System.out.println("Value of the firstString  :: " + firstString);
	System.out.println("Value of the secondString :: " + secondString);
}