Ocp Data Types

2 minute read

OCP - Data types

Java supports two data types:

Primitive data types

These are raw data stored as the value directly. The Java compiler and JVM both know exactly how much memory primitive types require and what actions can be performed on them.

Primitives are grouped as follows:

Integral data types: byte, char, short, int, long

  • These are named integral because they store integral values
  • char cannot store negative numbers and its value is interpreted as a unicode character. So it can store \u0061 for example.

Floating point data types: float, double

Boolean data types: boolean

  • This type can only ever store two values: true or false and so only requires one bit of memory.

Collectively, all the above data types are known as numeric data types.

Name Bits Range Default value Example
byte 8 -128 to 127 0 -1, 0, 1
char 16 0 to 65,535 0 0, 1, 2, ‘a’, ‘\u0061`
short 16 -32,768 to 32,767 0 -1, 2, 3
int 32 -2^31 to 2^31-1 0 -1, 2, 3
long 64 -2^63 to 2^63-1 0L -1, 2, 3
float 32 rediculously massive 0.0f 1.1f, 2.0f
double 64 even more rediculously massive 0.0d 1.1, 2.0
boolean 1 true or false false true or false

Note: Do not use float or double for precise values. Instead user BigDecimal.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Sizes

Primitive data types store the actual data and so we know the size of memory required based on the data type used. Reference data types, on the other hand, store an address to a location in memory. The amount of memory allocated to store this reference value depends on the machine that is running the JVM.

  • A 32 bit OS will allocate 4 bytes
  • A 64 bit OS will allocate 8 bytes

Now we know, at compile time, what the size of all of our objects will be because they are made of up either primitive or reference data types, both of which we know how much memory is allocated, so we can just sum up all of an objects instance variables. This size does not change during run time. All instances of a given class will all take up exactly the same amount of space in the heap memory.

Reference data types

These are data that the Java compiler and JVM have no knowledge of. For example: a class you have created called User. Java does not know what this is. It does not know how much space in memory it requires, what operations it supports, or what properties is has.

Reference data types are all classes, interfaces, and enums.

Null and Void

void is a keyword that means “nothing” and is used to declare that a method does not return any data type. For this reason it is a type specification, not a data type itself.

null is also a keyword, and it is used as a value of a reference type to indicate that the variable is not currently pointing to any object in the heap memory. A method can return null, but the method can also return a reference of the declared return type.

The equals (=) operator

The equals (=) operator can be used by both primitive and reference data types to assign values to variables. The difference, though, is that the value of a primitive is the actual value, but the value of a reference variable is an address to a location in memory where the object is located that stores the actual value. With the exception being null which can be assigned to a reference variable directly.