Chapter 2 - Java for Beginners Course

Primitives

When we introduced the concept of variables, we started talking about the different types in Java, for example, the int type that is used to store integer numbers.

It is important to note that although Java is an Object Oriented Programming language, it supports 8 data types that are built into the language and that are not objects created from classes, in contrast to what we introduced in Chapter 1.

These special built-in data types are called primitive data types and are identified by language keywords as follows:

Integer values

Java offers 4 primitive data types that are meant for the storage of two-complement integers, each with different sizes:

Data type Description Size in Memory

byte

Integer values between -128 and 127

8-bit

short

Integer values between -32768 and 32767

16-bit

int

Integer values between -2^31 and 2^31-1

32-bit

long

Integer values between -2^63 and 2^63-1

64-bit

In general, for most use cases, the most common data type for storing integer values is int, however if memory savings are required then byte or short tend to be used, for example, if you need to store large arrays of integer numbers that don’t exceed the limits of a byte or short.

It is important to note, that starting from Java 8, the language offers tools to treat int and long as unsigned numbers, meaning their minimum and maximum values are different. For an unsigned int they’ll range from 0 to 2^32 and for an unsigned long they’ll range from 0 to 2^64.

Examples of integer values:

byte b = 1;
short s = 2;
int i = 3;
long l = 4;
long aBigNumber = 12345678900L;

Floating point numbers

Java offers 2 primitive data types for storing floating point numbers as defined by the IEEE 754 standard, as follows:

Data type Description Size in Memory

float

Single precision floating point number

32-bit

double

Double precision floating point number

64-bit

These data types shouldn’t be used for precise values, for example, if you’re handling financial transactions.

Examples of floating point numbers:

float f = 1.2F;
double d = 2.4D;

The boolean data type

Data type Description

boolean

Holds a true/false value

Examples of boolean values:

boolean bool1 = true;
boolean anotherBool = false;

The char data type

Data type Description

char

Holds a 16-bit Unicode character

This is the basis of storing text or strings in Java.

Example of a char value:

char c = 'a';

About literal values

When assigning a fixed value to a primitive variable we provide what is known as a literal value, which is the representation in code of the value we want to assign. For numerical values, we can just pass the number directly, for characters we indicate the character we want to assign enclosed in single quotes, and for booleans we pass in directly the true/false value.

As primitive values are not objects created from classes, there is no need to use the new keyword we saw in chapter 1.

Clarifying things for the compiler

You may have noticed that some of the numerical values above have letters in them. This is because the compiler assumes that by default numbers are either:

  • Of type int for integer values, or,

  • Of type double for floating-point values

This is why, for literal values that are long, we need to specify an L or l at the end of the value, and for float values we need to specify an F or f. For double values, a D or d can be specified, but it is optional.

However, these are not the only letters/characters we can use, for example, we can use:

  1. A 0x prefix to represent a hexadecimal number, for example: int a = 0xf (15 in decimal).

  2. A 0b prefix to represent a binary number, for example: int a = 0b100 (4 in decimal).

  3. An e to represent numbers in scientific notation, for example: double d = 1.2e3.

  4. An underscore _ to group digits of numbers, to make them easier to read, for example: int million = 1_000_000.

The String class - the primitive that isn’t

Class Description

String

Holds a string of characters

The String class provided by Java (java.lang.String) provides supports for strings of characters. The word String isn’t a keyword of the language and in contrast to the primitive types, you need to create objects/instances to store different string values.

An example of a String:

String text = "Example text";

Although it isn’t a primitive, notice that in the example we are not using the new keyword to create an instance of it. This is because the Java language provides a special treatment to string literals, identified by text enclosed in double quotes.

Java will automatically create a new String object for the string literals, as in the example provided.

A note about default values

Although you shouldn’t code by relying on default values as it is considered a bad practice, it is important to understand that Java will initialize fields that don’t declare an initial value to a reasonable default.

  • For all numeric primitive types, the default value is 0.

  • For boolean fields, the default value is false.

  • For non-primitive types (for example, String), the default value is null.

These default values are not assigned to local variables automatically and only apply to fields.