Skip to content

Types

Variables are used to temporarily store data in memory. Declaring a variable in Java follows the camelCase convention and is done as such:

int age = 30;

There are three main components to this declaration.

  1. The type of variable int (whole numbers)
  2. A Name/Label or Identifier of the variable age
  3. An assignment operator =
  4. An initial value of 30

When we declare the variable we are assigning the above to memory which we can then return in our functions and methods.

Constants are declared to ensure the value of the declarative is not modified, in Java we use the declarative type of final to ensure a constant, it is conventional to use uppercase to name final variables.

final float PI = 3.14F;

TypeBytesRange
byte1[-128, 127]
short2[-32K, 32K]
int4[-2B, 2B]
long8
float4
double8
char2A, B, C, …
boolean1True / False

Primitives are a great way to optimise the amount of bytes stored in memory storing simple types, as an example our int declaration of age should not exceed 127 so a better option as the type would be a byte, where as a YouTube video’s view count might exceed 32,000 so an int with a range of up to 2 Billion may be applicable.

There are some syntax rules when it comes declarations to be aware of.

  • Using an _ allows for traditional number formatting e.g. 1,123,456 can be declared as 1_123_456
  • For long types the value should have a suffix of the letter L as an example long viewCount = 3_123_456_789L;
  • For float types the value should have a suffix of the letter F as an example float price = 10.99F;
  • A single character char type value be encased in single quotes as an example char Letter = `A`;

Reference types are used to store complex objects, any type outside of a primitive type is considered a reference type. A reference type needs to be instantiated with the new operator, as an example: Date now = new Date();.

What we are doing is creating an instance of an existing class (generally imported) stored in memory. Once the reference type is instantiated it can be accessed like a regular class.

Take this example:

byte x = 1;
byte y = x;
x = 2;
System.out.println(y);

In this example our variables above would be stored in completely different memory locations meaning changing the value of X will not change the value of y returning the original value of 1 in our system out.

This behaviour changes with reference types.

Point point1 = new Point(1, 1);
Point point2 = point1;

So I’d say the key idea is to know if a variable represents the data itself or a reference to the data - JeremyLT freeCodeCamp

When the JRE executes Point point1 = new Point(1, 1); it will allocate memory to store Point(1,1) lets say the address of this memory is 100 then in a separate memory location it will store the address of 100 lets call the address of this location point1.

All written out like below:

Memory Location "Addr:100" = Point(1,1)
Memory Location "Addr:point1" = 100

This is the critical difference between Primitives and Reference types. Our variable is holding the address of our object rather then the object itself allowing for us to reference our object accordingly.

Image explaining memory pointing to the address of our object

Strings are defined in java.lang a string can be defined with the String type, example below:

String message = "Hello World";

Whilst string’s are declared like primitive variables they are actually reference types with plenty of methods available.

Escape Sequences

Prefixing special characters with a \ is best practice to ensuring special characters are returned as a String literal.

String Methods

Arrays are reference types defined in java.util, we use arrays to store lists. To declare an Array we use [] as an example:

int[] numbers = new int[5];

this would return a list with 5 elements with an index starting at 0.

As an example:

int[0] = 1;

The modern method of initialising arrays would be

int[] numbers = { 2,3,4,5,1};

In Java arrays have a fixed length once created they cannot have the length modified.

Array Methods

Multi dimensional arrays are useful in scientific algorithms where you may be implementing something like a matrix.

Multiple dimensions can be added to an Array utilising additional [] brackets we need to ensure to repeat those brackets in both our declarative and value:

int[][] numbers = new int[2][3];
numbers[0][0] = 1;
System.out.println(Arrays.deepToString(numbers));
// This would print [[1, 0, 0], [0,0,0]]

To implement this with modern methods we can follow the below syntax:

int[][] numbers= {{1,2,3}, {4,5,6}};
System.out.println(Arrays.deepToString(numbers));
// This would print [[1, 2, 3], [4,5,6]]