Java examples for Language Basics:Primitive Types
Type | Explanation |
---|---|
int | A 32-bit (4-byte) integer value |
short | A 16-bit (2-byte) integer value |
long | A 64-bit (8-byte) integer value |
byte | An 8-bit (1-byte) integer value |
float | A 32-bit (4-byte) floating-point value |
double | A 64-bit (8-byte) floating-point value |
char | A 16-bit character using the Unicode encoding scheme |
boolean | A true or false value |
Java allows you to promote an integer type to a larger integer type.
For example, Java allows the following:
public class Main { public static void main(String[] args) { int xInt;//from ww w. ja v a2 s.c om long yLong; xInt = 32; yLong = xInt; } }
Floating-point numbers are numbers that have fractional parts.
When you use a floating-point literal, you should always include a decimal point, like this:
public class Main { public static void main(String[] args) { double period = 99.0; //from w w w. ja v a 2s . c om System.out.println(period); } }
If you omit the decimal point, the Java compiler treats the literal as an integer.
You can add an F or D suffix to a floating-point literal to indicate whether the literal itself is of type float or double.
For example:
public class Main { public static void main(String[] args) { float value1 = 199.33F; double value2 = 123456.995D; }/*from w ww . j a va 2s . c o m*/ }