java

Variable and Data type in Java language

Variable and Data type in Java

In this article, we will discuss  the concept of Variable and Data type in Java

Datatype

The data type and variables are an important concept in every programming language. A variable can be similar to a container, which holds the value of your program in Java. Every variable in Java has a data type that is identical to the compiler: what type of variable it and what type of data store it

Variable and Data type in Java language

There are two types of data type in Java

  1. Primitive data type
  2. Non-primitive data type

Classification of data type in Java


Variable

The variable is used to allocate and name the memory location to hold data.

To identify the storage memory location, each variable should be provided with a unique name.

Declaration

int a;
float b;
String name;
char letter;

Initialization

int a=10;   //Here a is a integer type ,which takes the value 10
float b=24.67f; //Here a is a floating point type ,which takes the value 24.67
String name="kamalan"; //Here a is a String type,which takes the String "kamalan"
char letter='a'; //Here a is a character type ,which takes the char "a"

Variables in memory location

  • a is a memory address
  • 10 is the value of the variable

Java has three types of variable

  1. Local
  2. Instant
  3. Static

Local

The local variable is a variable that is declared inside the method. It is called the local variable.

Instant /Global

The global variable is a variable that is declared inside the class but outside of the method. It is called instant variable/global variable

Static

A static variable is a variable that is declared as static using the static keyword,. It is called the static variable. It cannot be local.

Declaration and initialization

We can define a variable at two location  inside the class.

for Example

Inside the class but outside the method – Global  or Instance
Inside the class and inside the method – Local

Declare local and global variable inside the class

Example of Variable and data type

Example

Program 1

Variable declaration and displays its value in Java

 

Output
10
23.33
12.5
Kannan
A

Program 2

Addition of Two numbers using variable

Output
Total is :25

Primitive Data types in Java

1. Data typeboolean

The boolean data type has two posible values true and false
Default Value – false
Default Size – 1 bit
Range – True or false
    For example – boolean b=true

program

class BooleanEx{
public static void main(String args[]){
boolean isTrue=true;
System.out.print(isTrue);

}

}

When the above code is executed, The following output is displayed

true

 

2. Data type – char

it,s 16 bit unicode charactor
Default Value – ‘u0000’
Default Size – 2 byte
For example – char c=’A’

Program

class CharEx{
public static void main(String args[]){
char letter='A';
System.out.print(letter);

}
}

When the above code is executed, The following output is displayed

A

 

3. Data type – byte

It is used to apply int type data type to save memory
Default Value – 0
Default Size – 1 byte
    Range – –126 to 127
For example – byteb=15;

Program

class ByteEx{
public static void main(String args[]){
byte num=124;
System.out.print(num);

}
}

When the above code is executed, The following output is displayed

124

4. Data type – short

It is used to apply int type data type to save memory
Default Value – 0
Default Size – 2 byte
    Range –  –32768 to 32767
For example – short s=1000;

Program

class ShortExample1{
public static void main(String args[]){
short num=1234;
System.out.print(num);

}
}

When the above code is executed, The following output is displayed

1234

 

5. Data type – int
Default Value – 0
Default Size – 4 byte
Range –  –2147483648 to 2147483647
For example – int i=100;

Program

class Int_Ex1{
public static void main(String args[]){
int num=12345678;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

12345678

6. Data type – long
Default Value – 0L
Default Size – 8 byte
Range –  –9223372036854775808 to 9223372036854775807
For example – long l=100;

Program

class Long_Ex1{
public static void main(String args[]){
long num=-1234567800L;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

-1234567800

 

7. Data type – float
Default Value – 0.0f
Default Size – 4 byte
Range – 
For example – float f=34.5f;

Program

class Float_Ex1{
public static void main(String args[]){
float num=234.56f;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

234.56

8. Data type – double
Default Value – 0.0d
Default Size – 8 byte
    Range –  –32768 to 32767
For example – double d=56.78d;

Program
class Double_Ex1{
public static void main(String args[]){
Double num=3433.56;
System.out.print(num);

}
}

When the above code is executed, the following output is displayed

3433.56
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.