Variable in Java is a data container that stores the data values during Java program execution. Every variable is assigned data type which designates the type and quantity of value it can hold. Variable is a memory location name of the data. The Java variables have mainly three types : Local, Instance and Static.
In order to use a variable in a program you to need to perform 2 steps
In this Java tutorial, you will learn-
To declare a variable, you must specify the data type & give the variable a unique name.
Examples of other Valid Declarations are
int a,b,c; float pi; double d; char a;
To initialize a variable, you must assign it a valid value.
Example of other Valid Initializations are
pi =3.14f; do =20.22d; a=’v’;
You can combine variable declaration and initialization.
int a=2,b=4,c=6; float pi=3.14f; double do=20.22d; char a=’v’;
In Java, there are three types of variables:
Local Variables are a variable that are declared inside the body of a method.
Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.
Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.
class Guru99 < static int a = 1; //static variable int data = 99; //instance variable void method() < int b = 90; //local variable >>
Data Types in Java are defined as specifiers that allocate different sizes and types of values that can be stored in the variable or an identifier. Java has a rich set of data types. Data types in Java can be divided into two parts :
Primitive Data Types are predefined and available within the Java language. Primitive values do not share state with other primitive values.
There are 8 primitive types: byte, short, int, long, char, float, double, and boolean
Integer data types
byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes)
Floating Data Type
float (4 bytes) double (8 bytes)
Textual Data Type
char (2 bytes)
Logical
boolean (1 byte) (true/false)
Data Type | Default Value | Default size |
---|---|---|
byte | 0 | 1 byte |
short | 0 | 2 bytes |
int | 0 | 4 bytes |
long | 0L | 8 bytes |
float | 0.0f | 4 bytes |
double | 0.0d | 8 bytes |
boolean | false | 1 bit |
char | ‘\u0000’ | 2 bytes |
Points to Remember:
A variable of one type can receive the value of another type. Here there are 2 cases –
Case 1) Variable of smaller capacity is be assigned to another variable of bigger capacity.
This process is Automatic, and non-explicit is known as Conversion
Case 2) Variable of larger capacity is be assigned to another variable of smaller capacity
In such cases, you have to explicitly specify the type cast operator. This process is known as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses.
Example: To Understand Type Casting
Step 1) Copy the following code into an editor.
class Demo < public static void main(String args[]) < byte x; int a = 270; double b = 128.128; System.out.println("int converted to byte"); x = (byte) a; System.out.println("a and x " + a + " " + x); System.out.println("double converted to int"); a = (int) b; System.out.println("b and a " + b + " " + a); System.out.println("\ndouble converted to byte"); x = (byte)b; System.out.println("b and x " + b + " " + x); >>
Step 2) Save, Compile & Run the code.
Expected Output:
int converted to byte a and x 270 14 double converted to int b and a 128.128 128 double converted to byte b and x 128.128 -128
You Might Like: