Java Primitive Data Types

Computers programs store data in variables. A data type or simply type is the type of data that can be stored in a variable. 
Java is a strongly typed language. That means that when we declare a variable in Java, we must always specify its type.
java data types are divided into two groups:
- Primitive types or Basic Types
- Non primitive types or Reference types
Primitive data types are built in to the programming language. This data types are predefined by the language.
Reference data types are the programmer-defined classes and the types defined by classes in the Java API.
There are eight primitive data types in Java:

Data Type
Description
Size (Bytes)
Range
Default Value
Wrapper Class
byte
Signed integer
1
-128 to 127
0
Byte
short
Signed integer
2
-32.768 to 32.767
0
Short
int
Signed integer
4
-2147483648 to 2147483647
0
Integer
long
Signed integer
8
-9223372036854775808 to 9223372036854775807
0
Long
float
Single-precision floating point
4
± 3.4x10-38 to ± 3.4x1038
0.0
Float
double
Double-precision floating point
8
± 1.8x10-308 to
±
1.8x10308
0.0
Double
char
Unicode character
2
\u0000 to \uFFFF
\u0000
Character
boolean
Logical data type
-
true - false
false
Boolean

INTEGER DATA TYPES
There are four integer data types: byte, short, int, long.
For example:
int a = 3;
byte n1, n2;
short x = -1;
FLOATING POINT DATA TYPES
There are two floating point data types: float, double.
For example:
float weight;
double price;
double amount = 1.7E4;  // 1.7 * 104
double z = .123;   //0.123
CHARACTER DATA TYPE
The character data type is represented by the char type.
The char type is used to store a Unicode character in the range \u0000 to \uFFFF.
The first 127 Unicode characters correspond with the ASCII code.
For example:
char ch;
char ch1 = 'z';
char ch2 = '\u0061'; // unicode for ‘a’
BOOLEAN DATA TYPE
The following declares two boolean variables:
boolean first = true;
boolean last = false;

No comments :

Post a Comment