Variables and Data Types in Java With Example

In this chapter, we are going to learn about variables and data types in Java. Let’s get started.

1. Data Types in Java

We always deal with some data while writing a program.

Therefore, to write any program or to deal with any kind of data we need to define the type of data we are going to store or manipulate.

Here comes, data types in Java.

We have two types of Data Types in Java:

  • Primitive Data Type
  • Non-Primitive Data Types
Variables_Java

1.1 Primitive Data Types:

Primitive data types are predefined in Java and they have no additional methods. There are 8 types of primitive data types in Java and can store below-given numbers when defined.

Data TypesSizeLimit
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytes6 to 7 decimal digits
double8 bytes15 decimal digits
boolean1 bittrue or false
char2 bytessingle character/letter

Example:

public class DataType{

	public static void main(String[] args) {
		byte singleByte = 1;
		short shortValue = 21;
		int intValue = 347;
		long longValue = 428282;
		float floatValue = 5.02929f;
		double doubleValue = 1.282828282822d;
		boolean booleanValue = true;
		char singleChar = '8';

		System.out.println("This is byte " + singleByte);
		System.out.println("This is short " + shortValue);
		System.out.println("This is int " + intValue);
		System.out.println("This is long " + longValue);
		System.out.println("This is float " + floatValue);
		System.out.println("This is double " + doubleValue);
		System.out.println("This is boolean " + booleanValue);
		System.out.println("This is char " + singleChar);
	}
}

1.2 Non-Primitive Data Types:

Non Primitive Data Types are created by programmers actually. For instance,

Therefore, We will be going to learn about Non-primitive data types later in this course.

Recommended Read: Class & Package in Java

Always Remember:

  • Primitive data types are predefined and Non-Primitive data types are user-defined.
  • The primitive data type will always contain a value but Non-Primitive can be NULL.
  • The primitive data type will start with lowercase and Non-Primitive will start from Uppercase.

2. Variables in Java

Suppose, you declare any Data Type for adding two numbers then you have to store that data somewhere in the computer memory.

In addition, Variable is the name of that particular area where data is stored.

In other words, variables are the name of the memory location.

Variables_Java

Declaration:

Syntax: dataType variableName value

Example: int var = 10;

There are three types of Variable in Java:

  1. Local Variable
  2. Instance Variable
  3. static Variable

2.1 Local Variable:

When we declare any variable inside any method then it is called a local variable. Therefore, they can be used only within the method, block, not outside the method.

Example:

public class Calculator {
	public void add() {
		// a,b are variables here.
		int a = 5;  // local variable created inside method
		int b = 6;  // we can't use a,b outside this method.
		System.out.println(a + b);
	}

	public static void main(String[] args) {

		Calculator obj = new Calculator();
		obj.add();
	}
}

2.2 Instance Variable:

When we declare any variable outside any method and within-class are called Instance variables. Therefore, they can be used anywhere in the class and can be used while creating an object.

Example:

public class Calculator {

	int a = 5; // instance variable created outside method (at class level)
	int b = 6; // we can use a,b anywhere in this class

	public void add() {
		System.out.println(a + b);
	}

	public static void main(String[] args) {
		Calculator obj = new Calculator();
		obj.add();
	}
}

2.3 Static Variable:

When we define a variable, we need to add a static keyword before the name of the variable. Therefore, we can use this variable among all the instance of the class.

Example:

public class Calculator {

	static int firstNumber; // static variable created at class level with static keyword
	static int secondNumber; // we can call a,b directly with class name.

	// static variables are destroyed when the program ends.

	public static void main(String[] args) {

		Calculator.firstNumber = 5;
		Calculator.secondNumber = 6;

		System.out.println(firstNumber + secondNumber);
	}
}

I hope you’ve understood variables and data types in java. Now, navigate to the next chapter