Home
/ Data Types in Java
Data Types in Java
Hi friend! Today we are going to discuss primitive data types available in Java.
Java basically supports eight primitive data types. Data type is generally going to identify what type of data is going to be stored in the memory location.
Let us see why we need data type? In general we use to represent data either in number format or non number format. The data which is going to be represented may occupy some memory. Small numbers may need less memory and big numbers may need more memory. Like the same way your name is nothing but a collection of characters which need memory based on number of characters present in it.
Hence languages defined data types so that memory can be utilized efficiently and data can be identified by just seeing the data type. In computers every thing is represented in bits. For example if you want to store a number say 9 then in binary language it will be represented as 1001. If the number is large then it occupies more bits.
In java every variable must be declared before using it in the program.
The eight primitive data types are as follows:
byte: This is used to store integer values. In this we can store any number between -128 to +127. It occupies one byte of memory (8 bits). Why the limit is upto 127? Because 127 is the largest number that can be represented in 1 byte memory. Convert it into binary and see how many bits you are having! Do remember that one bit is reserved for sign. If the number is positive then the sign bit value will be zero otherwise one.
short: This is also used to store integer values. But it has longer range as compared to byte. The range is from -32768 to +32767. It occupies 2 bytes of memory.
int: This is also used to store integer values. This can accommodate values larger than short data type. The range is from -2,147,483,648 to +2,147,483,647. Both the numbers are inclusive. It occupies 4 bytes of memory.
long: This is also used to store integers. This can have values which can’t fit in int data type. The range includes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. It occupies 8 bytes of memory.
float: This data type is used to store real numbers. Numbers which are having fractional part in it. It occupies 4 bytes of memory.
double: This is also used to store real numbers or floating numbers and its range is always greater than float. It occupies 8 bytes of memory.
boolean: This data type can have only two values either true or false. Generally it represents one bit of information.
char: This data type is used to represent a single character. Every key on the keyboard can be considered as a character. It occupies two bytes of memory.
Apart from the above 8 primitive data types Java supports collection of characters as a String data type. Don’t confuse it with basic or primitive data types. String is a predefined class in Java.
When we do programs on the above data types then you will get more clear idea on these data types. Hope todays session is useful for beginners.
SAINADH REDDY
A to Z Info.org website dedicated to Young Indians. This website mainly focus on Education, Jobs, Tricks, Tips, Latest Technology Updates, Software's, Biographies of Eminent Personalities, Entertainment, Film News, Tutorials and many more. Please comment below how to improve our services
0 comments:
Post a Comment