This article explains different primitive data types in Java.
Java Data Types
Java supports several types of data .. you can use the corresponding types to declare variables and arrays to store this data.
Java is strongly typed. Every variable has a type and the type is strictly defined.
The compiler checks all assignments for this variable and expressions involving it for type compatibility.
The primitive datatypes broadly fall into below categories :
- Integer Data types
- Floating point data types
- Character data type
- Boolean Data type
Integer data type
Java provides 4 integer types : byte, short, int and long.
All these are signed, positive and negative. Java does not support unsigned positive only integers.
Here are some examples of integer variable assignments :
byte b ;
short s ;
int i ;
long l ;
|byte||8 bit||-128 to 127|
|short||2 bytes||-32768 to 32767|
|int||4 bytes||-2147483648 to 2147483647|
|long||8 bytes||-9223372036854775808 to 9223372036854775807|
Float data type
These are useful to represent numbers requiring fractional precision.
There are two kinds of floating point types, float and double, which represent single and double precision numbers.
|float||4 bytes||1.4e-045 to 3.4e+038|
|double||8 bytes||4.9e-324 to1.8e+308|
float pi = 3.142F;
double d = 10.153;
Character data type
It represents a single character.
char ch = 'Y';
|char||2 bytes||0 to 65535|
Boolean data type
Boolean data types represent logical values, true or false. JVM uses 1 bit to represent a Boolean value internally.
boolean bool = true;
String Data type
String represents a group of characters.
Simplest way to create a sting is by assigning a literal as follows :
String str = "Hello";
String can also be created using object notation that we will see in next articles.
© 2015 – 2017, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post