Java Data Types
Data types in Java:
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float,
double, and boolean. These can be put in four groups:
■ Integers This group includes byte, short, int, and long, which are for wholevalued
signed numbers.
byte
Size(in byte) 1 byte
Range –128 to 127
The smallest integer type is byte.
Byte variables are declared by use of the byte keyword. For example, the following
declares two byte variables called b and c:
byte b, c;
short
Size(in byte) 2 bytes
Range –32,768 to 32,767
Here are some examples of short variable declarations:
short s;
short t;
int
Size(in byte) 4 bytes
Range –2,147,483,648 to 2,147,483,647
The most commonly used integer type is int.
int n;
long
Size(in byte) 8 bytes
Range –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The range of a long is quite large. This makes it useful when big, whole numbers are needed.
long l;
■ Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
double
Size(in byte) 8 bytes
Range 4.9e–324 to 1.8e+308
double pi;
float
Size(in byte) 4 bytes
Range 1.4e−045 to 3.4e+038
Here are some example float variable declarations:
float f;
■ Characters This group includes char, which represents symbols in a character
set, like letters and numbers.
In Java, the data type used to store characters is char.
Size(in byte) 2 bytes
Range 0 to 65,536.
char ch;
■ Boolean This group includes boolean, which is a special type for representing
true/false values.
It can have only one of two possible values, true or false.
boolean b;
1- Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2- Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
Data Type | Default Value | Default size |
---|---|---|
boolean | false | 1 bit |
char | ‘\u0000’ | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
double | 0.0d | 8 byte |
Example Programs:
Variables Initialization in JAVA
Character Escape Sequences
Escape | Sequence Description |
---|---|
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal UNICODE character (xxxx) |
\’ | Single quote |
\” | Double quote |
\\ | Backslash |
\r | Carriage return |
\n | New line (also known as line feed) |
\f | Form feed |
\t | Tab |
\b | Backspace |