How to Java programming- Variables & Data types

In this tutorial, we will learn Java variables & data types. Before yo continue, we recommend reviewing our previous guide.  This guide includes 3 main steps.

1. Variables in Java.
2. Data types.
3.Output the value a variable with its data type

 

1. Variables in Java.

Variables are names which we can declare to store some value. Variable is the name of the variable. You must declare all variables before they can be used.

data type variable [ = value][, variable [ = value] ...] ;

For example, if you want to declare a variable in Java in the main method.

int x = 5; this simply means that X is a variable of type integer which has a value.

int is integer data type.
x is the name of the variable.
5 is the value assigned to x.

 Java variables & data types

 

2. Data types.

Data type is a particular kind of data item, as defined by the values it can take, the operations that can be performed on it. There are two data types available in Java

Primitive Data Types
Reference Data Types

There are eight Primitive data types in Java.

 

Data Types
	package FirstPackage; 
	public class MyFirstClass {
		/* 	byte (number, 1 byte) 
			short (number, 2 bytes) 
			int (number, 4 bytes) 
			long (number, 8 bytes) 
			float (float number, 4 bytes) 
			double (float number, 8 bytes) 
			char (a character, 2 bytes) 
			boolean (true or false, 1 byte) */ 
		public static void main(String[] args) {
			// TODO Auto-generated method stub 
			int K = 5;
			// Here output "Hello World"
			System.out.println("Hello World"); 
		}
	}

 

1.byte (is a number, it can store 1 byte)
2.short (is a number, it can store 2 bytes)
3.int (is a number, it can store 4 bytes)
4.long (is a number, it can store 8 bytes)
5.float (is a float number, it can store 4 bytes)
6.double (is a float number, it can store 8 bytes)
7.char (is a character, it can store 2 bytes)
8.boolean (is true or false, it can store 1 byte)

 

3. Output the value a variable with its data type.

variables with datatypes
	package FirstPackage; 
	public class MyFirstClass{
		/* byte (number, 1 byte) 
		short (number, 2 bytes) 
		int (number, 4 bytes)
		long (number, 8 bytes)
		float (float number, 4 bytes)
		double (float number, 8 bytes)
		char (a character, 2 bytes)
		boolean (true or false, 1 byte) */ 
		public static void main(String[] args) {
			// T000 Auto-generated method stub 
			short my short = 10;
			int my_int = 999; 
			float my float = (float)6.8;
			double mvDouble = 25.25;
			char my_char = 'C'; 
			boolean   = false; 
			// Here output "Hello World" 
			System.out.println("Hello World"); 
		}
	}

 

The problem with Declaration of float in because of the value 4.5 is natively considered as a double and a double can also store decimal point but its range is 8 byte. so we need to convert this double value which is 4.5 to the float.

Let’s print all the variables with system.out.println() command.

Output variables
	package FirstPackage; 
	public class MyFirstClass{
		char (a character, 2 bytes)
		 boolean (true or false, 1 byte) */ 
		 public static void main(String[] args) {
			 // TODD Auto-generated method stub 
			 short my_short = 10; 
			 int my_int = 999; 
			 float my_float = (float)6.8; 
			 double myDouble = 25.25; 
			 char my_char = 'C';
			 boolean is_true = false;
			 // Here outputs all variables we declared.
			 System.out.println(my_short);
			 System.out.println(my_int);
			 System.out.println(my_float);
			 System.out.println(myDouble);
			 System.out.println(my_char);
			 System.out.println(is_true); 
		}
	}

 

Output
	10
	999
	6.8
	25.25
	C
	false

 

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...