--------------------------------------------------------------------------------------------------------------
Java Variable And Types:-
o A variable is a
container which holds the value while the Java Program is executed.
A variable is assigned with a data type.
o Variable is a name of
memory location. There are three types of variables in java: local, instance
and static.
There are two types of
Data Types in java
primitive and non-primitive.
Variable
o A variable is the name
of a reserved area allocated in memory. In other words, it is a name of the
memory location. It is a combination of "vary + able" which means its
value can be changed.
Types of Variables
There are three types of variables in Java
- local variable
- instance variable
- static variable
1) Local Variable
A variable declared inside the body of the method is called
local variable. You can use this variable only within that method and the other
methods in the class aren't even aware that the variable exists.
A local variable cannot be defined with "static"
keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared as Static.
It is called an instance variable because its value is
instance-specific and is not shared among instances.
3) Static variable
A variable that is declared as static is called a static
variable. It cannot be local. You can create a single copy of the static
variable and share it among all the instances of the class. Memory allocation
for static variables happens only once when the class is loaded in the memory.
Example:-
- public class A
- {
- static int m=100;//static variable
- void method()
- {
- int n=90;//local variable
- }
- public static void main(String args[])
- {
- int data=50;//instance variable
- }
- }//end of class
Primitive data types in Java are predefined by the Java language
and named as the reserved keywords. A primitive data type does not share a
state with other primitive values. Java Programming Language supports the
following eight primitive data types.
- Boolean
data type
- byte
data type
- int
data type
- long
data type
- float
data type
- double
data type
- char
data type
- short
data type
1) Boolean Data Type
A Boolean Data Type can have two types of values,
which are true and false. It is
used to add a simple flag that displays true/false conditions. It represents
one bit of information. It's is not a specific size data type. So, we can not
precisely define its size.
Exmple :- boolean Isname = true; //define in true or false only
2) Byte Data Type
It is an 8-bit signed 2’s Complement integer. It can
have a value of (-128) to 127 (inclusive). Below are
the benefits of using the byte data type:
o
It is useful for saving memory in large Arrays.
o
It can be used instead of int to clarify our code using its
limits.
o
It saves memory, too, because it is 4 times smaller than an
integer.
Exmple :-
byte a = 100;
System.out.println(a);
3) Int Data Type
The int stands for Integer; it is a 32-bit signed two's
complement integer. It's value can be from -231 to (231-1),
which is -32,768 to 32,767 (inclusive). Its default value is zero. It
represents an unsigned 32-bit integer, which has a value range from 0 to
32,767.
If memory saving is not our primary goal, then the int data
type is used to define the integer value.
Example:
1.
int num= 50000;
2.
System.out.println(num);
4) Long Data Type
It is a 64-bit 2's complement integer with a value range of (-263)
to (263 -1) inclusive. It is used for the higher values that can not
be handled by the int data type.
Example:
1.
long l = 7000000000L;
2.
System.out.println(l);
5) Float Data Type
The Float Data Type is used to declare the floating values
(fractional numbers). It is a single-precision 32-bit IEEE 754 floating-point
data type.
Its value range is infinite. While declaring the floating, we
must end the value with an f.
It is useful for saving memory in large arrays of floating-point
numbers. It is recommended to use float data type instead of double while
saving the floating numbers in large arrays, and not use it with precise
numbers such as currency.
Example:
1.
float num = 5.75f;
2.
System.out.println(num);
Note: A Scientific number can also be used to represent a
scientific number with the power of 'e', where e represents the power of 10.
for example, float f1= 25e2f; double d1= 15E2d; etc.
6) Double Data Type
The Double Data Type is also used for the floating-point (Fractional
values) number. It is much similar to the float data type. But, generally, it
is used for decimal values. Like the float data type, its value range is
infinite and also can not be used for precise values such as currency.
The default value of the double data type is 0.0d. While
declaring the double type values, we must end the value with a d.
Example:
1.
double num= 19.99d;
2.
System.out.println(num);
7) Char Data Type
The Char Data Type is also an essential primitive data type
in Java. It is used to declare the character values. It is a single 16-bit
Unicode Character with a value range of 0 to 65,535 (inclusive).
While declaring a character variable, its value must be
surrounded by a single quote ('').
Example:
1.
char myChar= 'H';
2.
System.out.println(myChar);
Note: The ASCII character values can also be used to
display the characters.
8) Short Data Type
The Short Data Type is also used to store the integer
values. It is a 16-bit signed 2's complement integer with a value range of -32,768
to 32,767 (inclusive). It is also used to save memory, just like the byte data
type.
It is recommended to use the short data type in a large array
when memory saving is essential.
1.
short num = 5000;
2.
System.out.println(num);
No comments:
Post a Comment