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): |
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 treatchar
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 |
|
---|---|
|
They return the value of the number as a primitive data type |
|
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 |
|
---|---|
|
Constants holding the maximum and minimum value an int can have. |
|
Implementation of the method in interface Comparable<Integer> :
|
|
Number subclasses have constructors from String and primitive, but they don’t have a no-args constructor |
|
Creates wrapper from primitive |
|
The string s can be:
|
|
|
|
|
|
Converts primitive to String |