Java Primitive Data Types (8)

Type
Contains
Default
Size
Range

boolean

true or false false 1 bit NA

char

Unicode character
unsigned
\u0000 16 bits or
2 bytes
0 to 216-1 or
\u0000 to \uFFFF

byte

Signed integer 0 8 bit or
1 byte
-27 to 27-1 or
-128 to 127

short

Signed integer 0 16 bit or
2 bytes
-215 to 215-1 or
-32768 to 32767

int

Signed integer 0 32 bit or
4 bytes
-231 to 231-1 or
-2147483648 to 2147483647

long

Signed integer 0 64 bit or
8 bytes
-263 to 263-1 or
-9223372036854775808 to
9223372036854775807

float

IEEE 754 floating point
single-precision
0.0f 32 bit or
4 bytes
±1.4E-45 to
±3.4028235E+38

double

IEEE 754 floating point
double-precision
0.0 64 bit or
8 bytes
±439E-324 to
±1.7976931348623157E+308

A boolean type, a character type, four integer types, and two floating types. data types GUO International

[String ][Wrapper Classes]

boolean

The boolean type has two possible values, representing two states: on or off, yes or no, true or false. Java reserves the words true and false to represent these two boolean values.

To declare a boolean variable:

public class Test {
    boolean b;
	
    public static void main(String[] args) {
        System.out.println(new Test().b); // prints false
       // System.out.println(b);//You can't reach b, which is not static variable.
    }

}

Return to top


char

The char type represents Unicode characters. Its size is 16 bits long.

To declare a char variable, simple place it between single quotes(apostrophes):

You can use escape sequence to represent character literal.
'\uxxxx is Unicode escape sequence, where xxxx is for hexadecimal digits
'\xxx' is Latin-1 character, where xxx is an octal(base 8) number between 000 and 377.

Nonprinting ASCII character:
'\t' -- horizontal tab
'\b' -- backspace
'\n' -- newline
'\f' -- form feed
'\r' -- carriage return

 public class Test {
     public static void main(String[] args) {
         char c = 'A';
         char tab = '\t'; 
         char nul = ' ';
         char aleph = '\u05D0';
         char backslash = '\\';
         char singleQuote ='\'';
         char doubleQuote = '\"';
     
         System.out.println(c);
          //...
     }
 }
 
You can't do these:
   char nul = '';  
   char singleQuote = ''';
   char doubleQuote = '"';
   char backslash = '\';
 

Return to top


byte, short, int, long

byte, short, int, long are called integer types. These four types differ only in the number of bits.

Literals for each type: byte, short and int are almost the same.

 public class Test {
     public static void main(String[] args) {
         byte b = 127; // Min:-128 Max: 127
         
         int i1 = 28;
         int i2 = 0x1c; //hexadecimal 28
         int i3 = 034; // octal 28
         int i4 = 0xCAFEBABE; //magic number used to identify Java class files
         
         long l1 = 12345L; //a long value
         long l2 = 12345l; //a long value
         long l3 = 0xffL; // a long value
         System.out.println(l3);
    }
 }
 
You can't do these:
   byte b = 128;
   short s = -32770;
   int i = 0xCAFEBABEFF;//too large
   if byte b1 = 127, b2 = 2; byte sum = b1 + b2; //not allowed, but
   System.out.println(b1+b2); //OK, because "+" in the .println() will 
   promote byte or short to int automatically.
 

Return to top


float and double

float and double data types represent real numbers. The float has at least 6 significant decimal digits and a double has at least 15 significant digits. The literals use a string of digits.

 public class Test {
     public static void main(String[] args) {
         float f = 6.02e23f; //represents 6.02 x 1023
         double d = 1e-6; // represents 1 x 10-6
         double d2 = 123.4; 

         //no problem to compile. 
         //no exception thrown even if illegal operations
         double inf = 1/0; //infinity
         double neginf = -1/0; // -infinity
         double negzero = -1/inf; // negative zero
         double NaN = 0/0; //NaN--not a number
     }
 }
 
To check whether a float or double value is NaN, you must use the Float.isNaN() and Double.isNaN() methods.

Return to top


String

The String type is a class, not a primitive type. String literals contain a string of letters including escape sequences that can appear as char literals.

 public class Test {
     public static void main(String[] args) {
         String s = "This is a test";
         String s1 = "\nThis is the second line";
         System.out.println(s+s1);
     }
 }
 

Return to top


Wrapper Classes

Wrapper classes are classes related primitives. Each primitive type has a corresponding wrapper class.

 boolean -- Boolean
 char -- Character*
 byte -- Byte
 short -- Short
 int -- Integer*
 long -- Long
 float -- Float
 double -- Double
 
 

Return to top