VARIABLE: It is a name
given to a memory location, which can hold a value and that can be changed any
number of times in future if required.
Advantages:
Reusability
Easy to
maintain
Future
updations are easy
NOTE: always use meaningful names.
Syntax: Datatype variablename=value;
Int a=10;
CONSTANT: It is a name
given to a memory location, which can hold a value that can’t be changed in
future.
Syntax: final Datatype constantname=value;
Final
int a=10;
Based on the
data type of a variable, the operating system allocates memory and decides what
can be stored in the reserved memory. Therefore, by assigning different data
types to variables, you can store integers, decimals, or characters in these
variables.
There are two data types available in Java:
- Primitive Data Types
- Reference Data Types
Primitive
Data Types:
There are eight primitive data types supported by
Java. Primitive data types are predefined by the language and named by a
keyword. Let us now look into detail about the eight primitive data types.
Byte: it is used to
store an integer value up to 8 bits (-2^7 to 2^7-1).
Minimum
value is -128 (-2^7)
Maximum
value is 127 (inclusive)(2^7 -1)
Default
value is 0
Byte data
type is used to save space in large arrays, mainly in place of integers, since
a byte is four times smaller than an int.
short: it is used to
store an integer value up to 16 bits (-2^15 to 2^15-1).
Minimum
value is -32,768 (-2^15)
Maximum
value is 32,767 (inclusive) (2^15 -1)
Short
data type can also be used to save memory as byte data type. A short is 2 times
smaller than an int
Default
value is 0.
Int: it is used to
store an integer value up to 32 bits (-2^31 to 2^31-1).
Minimum
value is - 2,147,483,648.(-2^31)
Maximum
value is 2,147,483,647(inclusive).(2^31 -1)
Int is
generally used as the default data type for integral values unless there is a
concern about memory.
The
default value is 0.
Long: it is used to
store an integer value up to 64 bits (-2^63 to 2^63-1).
Minimum
value is -9,223,372,036,854,775,808.(-2^63)
Maximum
value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
This type
is used when a wider range than int is needed.
Default
value is 0L.
Float : it is used to store a decimal value
up to 32 bits.
Float
data type is a single-precision 32-bit IEEE 754 floating point.
Float is
mainly used to save memory in large arrays of floating point numbers.
Default
value is 0.0f
Float
data type is never used for precise values such as currency.
doube : it is used to
store a decimal value up to 64 bits.
double
data type is a double-precision 64-bit IEEE 754 floating point.
This data
type is generally used as the default data type for decimal values, generally
the default choice.
Double
data type should never be used for precise values such as currency.
Default
value is 0.0d.
Boolean: it is used
for storing Boolean values like either TRUE or FALSE.
boolean
data type represents one bit of information.
There are
only two possible values: true and false.
This data
type is used for simple flags that track true/false conditions.
Default
value is false.
Char: it is used
for storing a single character.
boolean
data type represents one bit of information.
There are
only two possible values: true and false.
This data
type is used for simple flags that track true/false conditions.
Default
value is false.
Reference Data Types
Reference data
types are made by the logical grouping of primitive data types. These are
called reference data types because they contain the address of a value rather
than the value itself. Arrays, objects, interfaces etc. are examples of
reference data types.
- Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
- Class objects and various types of array variables come under reference data type.
- Default value of any reference variable is null.
- A reference variable can be used to refer to any object of the declared type or any compatible type.
No comments:
Post a Comment