Java Variables, Data Types, and Operators

We have discussed the introduction to Java, JVM, JDK, and JRE in the previous blog post. Let’s take the knowledge journey one step ahead and today we are going to discuss different types of Java variables, data types, and operators. These are the basic knowledge points which you need to know before becoming the full-fledged Java SDET. Well, my goal is to share the crisp and concise Java knowledge by keeping testers in mind.

Do not miss the hot article on Java topics for Selenium Automation testers. This article will help you plan and strategize your Java learning. If you find any difficult just drop me an email: avinash@inviul.com

What are Java Variables?

Java variables are data holders that store the value while java programs are in execution. These variables are assigned with data types which we will discuss in the latter part of this article. Java variables simply define the name of the memory location where values are stored.

In layman, variables are such memory location whose value varies.

What are the types of Java Variables?

There are mainly three types of Java variables, those are listed as below:

  1. Local Variable
  2. Instance Variable
  3. Static Variable

Java variables data types and operators

Local Variable

A local variable is like your lane’s dog, he is powerful in your lane and dogs from another lane don’t know him. Similarly, a local variable is defined within the method itself and other methods of the same class are not aware of this variable. The lifecycle of the local variable is the calling of the method. Local variables can’t be declared with the static keyword.

Instance Variable

An instance variable is defined within the class and outside any method of the class. Such a variable is not declared with the static keyword. Values of the instance variable are not commonly shared with different instances of the class, rather, it’s instance specific which means that value for instance variable will be assigned fresh for each new instance.

Thus memory allocation of instance variable occurs each time a new instance is created.

Static Variable

When a variable declared with a static keyword is called as a static variable. These variables can never be a local variable. The memory of static variables is declared only at once and a common value is shared among each instance of the class. Hence, memory allocation occurs only once and static variables belong to class memory.

package my.project;

public class Variables {
	
	int a = 10; //Instance Variable
	static int b = 20; //Static variable
	
	public void myMethod(){
		int c = 30; //Local Variable
	}
}

What are the Java data types?

Defining data types explicitly simply refers to the amount of memory a variable is going to occupy within the stack. Hence, we have explicit data types to store integer, string, character, double, float, etc.

Different Data types in Java

There are mainly two data types are available in Java. Those are listed as below:

  • Primitive Data Types: They are in ascending order of their memory size- boolean, char, byte, short, int, long, float & double.
  • Non-primitive Data Types: Classes, Interfaces & Arrays.

Why do we declare Data Types in Java for Java variables?

We need to declare all the Java variables before its use because Java is a statically typed programming language.

Let’s have a look at different primitive data types in Java with their default size.

Primitive Data Types Default Size
boolean 1 bit
char 2 byte
byte 1 bye
short 2 byte
int 4 byte
long 8 byte
float 4 byte
double 8 byte

 

Operators in Java

Operators in Java are the symbols which are used to perform mathematical and logical operations. Java operators are mentioned in following table:

Operators Precedence
Unary val++ ,  val– , !  , ~ , –val , ++val
Aritmatic + – * / %
Shift << , >> , >>>
Relational instanceof  , <=  , >= , ==  , !=, < , >
Bitwise &, ^, |
Logical &&, ||
Ternary ? :
Assignment =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=

 

This tutorial was the fundamentals of your Java learning journey. If you face any difficulties in understanding them you can post your questions by using comment form. See you in the next tutorial.

Leave a Reply