What are Java Data Types and Variables?

What is a Java Variable?

  • A variable can be called as a container which holds some value in a java program. 
  • Variable has assigned a data type which defines what type of value variable can hold.
  • Variable has a name which locates to the memory.
  • If you want to use variable in a program, there are two steps associated with it you have to perform:
    • Variable Declaration
    • Variable Initialization
i. Variable Declaration
While declaring a variable you have to assign data type with variable name. Variable name has to be unique.

Example: String employee;

ii. Variable Initialization
It means assigning a value to the declared variable.

Example: employee = "John"


Above two steps (declaration and initialization) can also be joined together in one single step.

Example: String employee = "John";

Let's explain above example, 
  • 'String' is data type
  • 'employee' is name of variable
  • "John" is value assigned to variable named employee.

Types of Variables

Java has three types of variables:
  1. Local Variables
  2. Instance Variables
  3. Static Variables

1. Local Variables

Variable that is declared inside a method is local variable. These variables can only be used within that method as their scope lies only inside that method.


2. Instance Variables

Variable that is declared outside of method but inside class is called instance variable. It is not declared as static.


3. Static Variables

Variable which is declared with keyword 'static' and outside the method is called static variable. Static variables are allocated memory when class is loaded in memory. Static variables initialized before instance variables.

Code example for Java Variable Types:

package com.techlistic.java;

public class JavaVaribales {
 
 // Instance Variables
 int x = 15;
 
 
 // Static Variables
 static String message = "Hello World!";
 
 
 // Main Method
 public static void main(String[] args) {
  
  // Local variables
  int a = 10; 
  int b = 20;
  
  // Some code here
  // ---------
  // ---------
 }
 
 
 // Method
 void myMethod() {

  // Local Variables
  char ch = 'i';
  float num = (float) 1.5;

 }

}



Java Data Types

As we already discussed in previous tutorial that every java variable has a data type. Data type tell us the different sizes and values that can be stored in a variable.

Java has two types of Data Types:
  1. Primitive Data Types
  2. Non-Primitive Data Types

1. Primitive Data Types

Primitive data types are built-in with in Java language. No additional methods are required for it. There are 8 primitive data types:
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char

Data TypeDefault ValueDefault size
byte01 byte
short02 bytes
int04 bytes
long0L8 bytes
float0.0f4 bytes
double0.0d8 bytes
booleanfalse1 bit
char'\u0000'2 bytes
1. byte : 
It's value range lies between -128 to 127. Byte is used in place of int to save memory as byte is 4 times smaller than integer. It's default value is 0.

Example: 
byte x = 10;
byte y = 20;


2. short
The range of short data type lies between -32,768 to 32,767. Short data type can again be used to save memory instead of int. Short is 2 times smaller than int. It's default value is 0.

Example:
short s = 12000;
short t = -10000;


3. int
int is also known as integer. It's range value lies between -2,147,483,648 to 2,147,483,647. It's default value is 0. This is the most commonly used data type for integral values.

Example:
int x = 10;
int y = 2000;


4. long
long data type is of 8 bytes. It's value range lies between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. long data type is used when a large range of values required which are not provided by int. It's default value is 0.

Example:
long l = 100000L;
long m = -300000L;


5. float :
float data type is used for decimal values. It's range is unlimited. float is recommended over double data type to save memory. It's default value is 0.0F.

Example:
float f = 20.5f;


6. double :
double data type is generally used for decimal values just like float. It's range is unlimited. It's default value is 0.0d.

Example:
double d = 10.5;


7. boolean :
boolean  data type can only stores one of the two values: true or false.

Example:
boolean flag = false;
boolean isJavaInteresting = true;


8. char :
The char data type is used to store characters. Character must be with in single quotes ('a').

Example:
char grade = 'A';


2. Non-Primitive Data Types

Non-Primitive data types are not defined by Java and are created by the programmers except Strings. Unlike primitive, non-primitive data types can be null. Non-Primitive data types are:
  1. Strings
  2. Arrays
  3. Classes
  4. Interface etc.
i. Strings :
The String data type is used to store sequence of characters or text. Strings must be declared within double quotes like "Hello".

Example:
String myName = "John"; 

You will learn about other Non-primitive data types in the upcoming tutorials. Refer Java Tutorials Series


Java Class and Object <<Previous    ||    Next >>  Java Modifiers

Follow Us

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

How to Automate Google Search with Selenium WebDriver

Top 7 Web Development Trends in 2022

25+ Most Important Selenium WebDriver Commands Tutorial

50+ Most Important Selenium WebDriver Interview Questions and Answers

How to Find Broken Links on Webpage with Selenium WebDriver