|
|
| Data Types in Java | Learning data types is an
elementary step in knowledge of java. Consider You want to do an operation as
2+2 = 4 , then what all we need. We need something to keep 2 in. Learning java
is not know how of how to add , but how Java will add. This is easier to
understand that 2+2 will not be a long thing , but consider adding
23493094034900390349003 + 1 , most compilers will either take a long time, or
crash , or simply throw an exception. Learn how to add such numbers in Java, is
study of such stuff. I will try to provide intersting examples to understand
this .
For the beginners , Java has 8 primitive types , these data types provide the
basic necessity . These 8 primitive types are :
- byte - The range of values from -127 to 128 . I would rather use byte where i
want to save memory. byte is used as index values , and place where the data
types and storage is important.
- short - The value ranges from -32768 to 32768. The value i have entered earlier
can not be added using this data type.
- int - The value ranges for int data type in Java is
-2,147,483,648 to 2,147,483,647 .The int values can be unsigned and signed. By
default int is signed.
- char - Enough with numbers, the char is used to store characters in Java. Java
is a strongly typed language , and developers coming from C/C++ platform must
not confuse char with string. String is not a primitive data type in Java.
I am not saying String is not a data type , it is just not primitive data type.
The minimum value of char is '\u0000' and the maximum value is '\uffff' ( can be
read as (0,32768)
- float - Often used to represent the decimals valurs , float is not
recommended for use in production in general. Prefer using long instead
- long - initalised with L in the last , long is used to represent the decimal
numbers,9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807. So this
is my choice to add very large numbers
- double - If you have the decimal values , those are not very large , and needs
to be accessed frequently , double is the choice
- boolean - the data type to choose when the decision is in true or false. boolean
is used to store the data type as a boolean value. Learning the use of boolean
values will help a long way in going to write complex logic. Default value of
boolean is false.
Example of how to use Java integer type is given here | Uninitalized
local variables in Java will cause a compiler error. Even though Java will
initialize the static variables , it is still recommended to initialize the
variables. One advantage of Java is that variables can be initialised where they
are needed , unlike C , where the initialization had to take place at the top.
Here is an example what will happen in case the uninitialized Java Data type is
complied and run. | | |
|