Primitive Data Types

The following table lists the primitive data types that are available in Java:

Type Size (bits) Default value Range
byte 8 0 -128 to 127
short 16 0 -32768 to 32767
int 32 0 -2n-1 to 2n-1-1
long 64 0L -2n-1 to 2n-1-1
float 32 0.0f No need to know for the exam(1)
double 64 0.0d No need to know for the exam(1)
char 16 0 (\u0000) Unicode: \u0000 to \uFFFF

Integer(2): 0 to 65535

boolean Platform dependent false false or true

(1) You just need to know that for precise values such as currency you shouldn’t use float or double but the java.math.BigDecimal class instead

(2) See below: Characters as Integer Values

Characters as Integer Values

You can always treat char values as if they were integer values. For example, the character 'A' corresponds to the hexadecimal Unicode \u0041 and also to its equivalent decimal value 65.

You don’t need to know the equivalent integer values for every character, but it could be helpful for some exam questions if you’re able to remember the following three values: 'A'=65, 'a'=97 and '0'=48.

The Wrapper Classes

Every primitive data type has its corresponding object wrapper class: Character, Boolean and the subclasses of the abstract class Number for numeric values: Byte, Short, Integer, Long, Float and Double.

Number main methods
  • byte  byteValue()
  • short shortValue()
  • int intValue()
  • long longValue()
  • float floatValue()
  • double doubleValue()
They return the value of the number as a primitive data type
  • boolean equals(Object o)
It checks for same class and value. It first checks if the two objects are of same class and if they aren’t, it immediately returns false

The table below shows the most significant members of the Number subclasses, using Integer as a specific subclass example:

Integer main members
  • static int MIN_VALUE
  • static int MAX_VALUE
Constants holding the maximum and minimum value an int can have.
  • int compareTo(Integer i)
Implementation of the method in interface Comparable<Integer>:

  • 0 if this == i
  • < 0 if this < i
  • > 0 if this > i
  • Integer(int value)
  • Integer(String s)
Number subclasses have constructors from String and primitive, but they don’t have a no-args constructor
  • static Integer valueOf(int i)
Creates wrapper from primitive
  • static Integer decode(String s)
The string s can be:

  • Decimal: "32"
  • Octal: "O40"
  • Hexadecimal: "0x20", "#20"
  • static Integer valueOf(String s [, int radix])
  • Be careful: return type is different for both methods.
  • The radix specifies the format of the string: 2 (binary), 8 (octal), 10 (decimal, this is default), 16 (hex), 27, etc.
  • The brackets [] mean that there are two versions of the method (with/without the radix parameter)
  • static int parseInt(String s [, int radix])
  • static String toString(int i)
Converts primitive to String

Leave a Reply