Data types and Variables in Java - CodingAliens

Hellow there, welcome to the codingaliens..!!. In the previous tutorial, we learned about printing statement in Java programs.Now we will learn how we stores the values in Java programs and what are the types of data.

In this tutorial, you will learn -
  • What is variable
  • Variable declaration
  • Variable initialisation
  • The types of variables
  • Data types in java

What is a variable ?

A Variable is also called a container which holds the  value, during the the storing the data in a Java program.Every variable is assigned a data type which decides the types of data and quantity of value it can hold.

You can use variables in the Java program in two ways are -

First way - in the two steps
Second way - in the single step

First way : for using a variable in the Java program you need to perform two 

           STEPS :
            Step 1 variable declaration.
            Step 2 variable initialisation.

> Variable declaration :

To declare variable, you need to specify the data type and give the unique name to the variable in the Java program.

Examples of variable declarations are -


int num,age;

float pi;

double doble;

char character;

> Variable initialisation :

To initialise a variable you need to assign the valid value of that data type variable.


Examples of valid initialisations are-


pi =3.14f;

doble =20.22d;

charachter='v';

Second way : you can combine both the steps variable declaration and initialisation step and convert into single step.


Examples of this -


int age=21,num=412;

float pi=3.14f;

double doble=20.22d;

char character='v';


Types of variables -

In Java, there are three types of variables
  1. local variables.
  2. Instance variables.
  3. Static variables.

1. Local variables

local variables are the variables that are declared inside the body of any method like functions if else classes etc.

2. Instance variables

Are instance variables are defined without the static keyword. They are defined outside the body of the method. The object specific variables are also known as instance variables.

3. Static variables.

Static variables are initialised only once,at the starting of the program execution. This variable should be initialised first, before the initialisation of any instance variables.

Data Types in java


Data types are classified the different values to be stored in the variables. In Java there are two types of data types, primitive data types and non primitive data types.

1. Primitive data types

Primitive data types are predefined data types in the JAVA. Primitive values do not share state with other primitive values.

There are 8 primitive types : byte,short,int,long,double and Boolean 

Integer data types
byte (1 byte)
short (2 byte)
int (4 bytes)
long (8 bytes)


Floating data types
float  (4 bytes)
double (8 bytes)

Textual Data type
char (2 bytes)

Logical Data type
boolean (1 byte) (true / false)

Data Type
Size
Description
byte
1 byte
Stores whole numbers from -128 to 127
short
2 bytes
Stores whole numbers from -32,768 to 32,767
int
4 bytes
Stores whole numbers from -2,147,483,648 to 2,147,483,647
long
8 bytes
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
4 bytes
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double
8 bytes
Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean
1 bit
Stores true or false values
char
2 bytes
Stores a single character/letter or ASCII values

Post a Comment

0 Comments