Home » Java programming language

Variable declaration, initialization in Java

Just like C programming language, we can declare and initialize variables in Java too, here is the syntax

data_type variable_name;


data_type variable_name=value;

Consider the following Integer declaration and initialization statements

int number;


int number=0;

We would recommend using second style of variable declaration and initialization, as such kind of declaration also stores the default value to the variable.

int number=0;

Other type of variables can also be declared following given syntaxes, consider the given example, here we are declaring some of the variables with initial (default) values to them and then we will print the values.

public class Numbers{

     public static void main(String []args){
		int 	a	=10;
		char 	b	='X';
		float 	c	=10.235f;
		String 	str	="Hello";
		System.out.println("a= "+a);
		System.out.println("b= "+b);
		System.out.println("c= "+c);
		System.out.println("str= "+str);


a= 10 
b= X
c= 10.235 
str= Hello