Hello,
It’s been taken a while for hitting my 5th post. In
my previous posts I had talked about setting up the Java Environment and
executing your first Java Program with errors and without errors..:P
So from this post I will be chipping into the fundamentals of
Java. I believe the first and foremost basics one should know in Java are about
Variables and Data Types. So let us discuss about Java Data Types in this post.!
Variables – We have data where we’ll want to store and
retrieve data for further operations/calculations. So for holding the data, we
require variables.
Data Types – To declare the type of data that we store in
the variables, we use Data Types. Sometimes you might want to store a name
"LindlarsForLife" which is a String,
sometimes you might want to store a number 1234 which is an Integer.
Data Types in Java can be classified as,
- Primitive Data Types
- Reference Data types
Primitive Data Types - In Java, everything is an object
except these primitive types. This is because the primitive data types directly
contain values instead of references. This is the reason we say Java is not 100% Object
Oriented Programming. Just because of Primitives..!
We will be talking about Reference Data Types iin future posts.
Moving forward, we have 8 primitives in Java.
- byte
- short
- int
- long
- char
- float
- double
- boolean
Before explaining each primitive in detail, here are few
points I want you to know for better understanding of primitives and their
working.
2's complements: We all know that the data will be stored in
the system in the form of binary bits (0 and 1). And some of the primitives
will store the data in the form of 2's complement of the binary bits. Here comes
a question with WHY..! Why the primitives stores in the 2's complement but not
in the form of their direct binary bits?
The answer is quite simple. 2's complement is the good way
of storing the integers for implementing simple math’s calculations. Try it, that’s
true..! :D
Signed and Unsigned Primitives:
The primitive that can hold both Positive and Negative
numbers is called Signed Primitive.
The primitive that can hold ONLY Positive numbers is called Unsigned
Primitive.
There are NO unsigned primitives in Java. Burn it..!
Syntax:
In most Programming languages, we need to declare the data
types before using the variable. Following the syntax,
[Data type] [Identifier] = [value];
Now lets see the Primitive Data Types in detail.
byte:
byte data type is an 8-bit signed 2's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (2^7 -1)
Default value is 0
When we need to use byte Data Type? - byte data type is very
useful for memory saving. Many java applications give a very high priority for
the memory usage.
So when you want to store a number between -128 to 127 (both
inclusive) for better and faster performance go for byte data type.
Example: byte a=
-128; byte b= 127; byte c= 100;
short:
Short data type is a 16-bit signed 2's complement integer.
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (2^15 -1)
Default value is 0
When we need to use short Data Type? - short data type is
also used for memory saving.
So when you want to store a number between -32,768 to 32,767
(both inclusive) for better and faster performance go for short data type.
Example: short a= -25,000; short b= 31,000; short c= 100;
int:
int data type is a 32-bit signed 2's complement integer.
Minimum value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647 (2^31 -1)
Default value is 0.
When we need to use int Data Type? - int data type is the
default data type that will be used for storing integer values. This data type
is large enough to store numbers your program use. If the variable keeps on
multiplying and the value keeps on updating then short and byte will not be
useful as their limits are small.
Example: int a = 100000, int b = -200000
long:
Long data type is a 64-bit signed 2's complement integer.
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.
Example: long a = 100000L, int b = -200000L
If you have observed the above example correctly, we add 'L'
after the number. Again we get a question with WHY..!
The reason behind this is when you initialize a whole number
Java by default assumes that it is an int
datatype. So for telling, "Dear Complier, it is not int, it is long data
type", we have to add 'L' at the end of the number. If you initialize a
long variable without 'L' as shown
below, then the compiler will throw an error.
Example:
long a = 12345678901;
- will throw error saying The literal 12345678901 of type int is out of range
- will throw error saying The literal 12345678901 of type int is out of range
long a = 12345678901L;
- will NOT throw any error because the number is converted to long type from int
- will NOT throw any error because the number is converted to long type from int
long a = 123456789;
- will NOT throw error coz the number 123456789 is in the range of int datatype. The compiler will consider the data type as int in spite of declaring long.
- will NOT throw error coz the number 123456789 is in the range of int datatype. The compiler will consider the data type as int in spite of declaring long.
float:
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. If you want to go
for precision, better choose BigDecimal (we will discuss more about this in
future).
Example:
float a = 234.5F;
Here
the reason for using ‘F’ is same as the reason for ‘L’. By default the Java compiler
takes decimal values as double data type.
double:
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. Same as
float, if your prefer precision better choose BigDecimal.
Default
value is 0.0.
Example:
double d = 123.4;
boolean:
boolean
data type represents one bit of information.
There
are only two possible values: true and false.
When we
need to use boolean Data Type? – Use this when you want to have a flag or like
a switch. IN or OUT. TRUE or FALSE.
Default
value is false.
Example:
boolean isAvailable = true;
char:
char
data type is a single 16-bit Unicode character.
Minimum
value is '\u0000' (or 0).
Maximum
value is '\uffff' (or 65,535).
Char
data type is used to store any single character.
Example:
char a ='A';
Hit Points:
- Java is not pure Object Oriented Programming Language. Primitive data types are one of the reason for it.
- Java doesn't have unsigned data types.
- Most of the data types in Java use 2's Complement.
- To declare long type we need to use 'L' and to declare float type we need to use 'F'.
Thanks :)
0 comments:
Post a Comment