Java – Basic Data Types
Integers
Java defines four integer types: byte, short, int, and long.
Type | Width in Bits | Range |
---|---|---|
byte |
8
|
–128 to 127 |
short |
16
|
–32,768 to 32,767 |
int |
32
|
–2,147,483,648 to 2,147,483,647 |
long |
64
|
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Java does not support unsigned (positive-only) integers.
Floating-Point Types
Type | Width in Bits | Range |
---|---|---|
double |
64
|
4.9e–324 to 1.8e+308 |
float |
32
|
1.4e–045 to 3.4e+038 |
Characters
In Java, characters are not 8-bit quantities like they are in most other computer languages. Instead, Java uses Unicode. Unicode defines a character set that can represent all of the characters found in all human languages. Thus, in Java, char is an unsigned 16-bit type having a range of 0 to 65,536. The standard 8-bit ASCII character set is a subset of Unicode and ranges from 0 to 127. Thus, the ASCII characters are still valid Java characters.
A character variable can be assigned a value by enclosing the character in single quotes. For example, this assigns the variable ch the letter X:
char ch; ch = 'X';
You can output a char value using a println( ) statement. For example, this line outputs the value in ch:
System.out.println("This is ch: " + ch);
In java it is possible to perform arithmetic operations on characters like increment character with + sign. example :
class CharArithmetic { public static void main(String args[]) { char ch; ch = 'X'; System.out.println("ch contains " + ch); ch++; // increment ch System.out.println("ch is now " + ch); ch = 90; // give ch the value Z System.out.println("ch is now " + ch); } }
Booleans
The boolean type represents true/false values. Java defines the values true and false using the reserved words true and false. Thus, a variable or expression of type boolean will be one of these two values.
class BooleanCheck { public static void main(String args[]) { boolean b1; b1 = false; System.out.println("b is " + b1); b1 = true; System.out.println("b is " + b1); // a boolean value can control the if statement, because if(true or // false) is evaluation for if condition which was o and 1 in c and c++ if (b1) System.out.println("b1 is true"); b1 = false; if (b1) System.out.println("b1 is false, will not executed"); } }