A primitive type is set based on the provided value. Assigning one primitive type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.
In Java, there are 8 primitive types:
The Boolean type is defined with the keyword: boolean
. You can set the value be true
or false
:
boolean isBool = true;
isBool = false;
The Character type is defined with the keyword: char
. You can set the value to any unicode character, surrounded by single quotes:
char myChar = 'a';
myChar = '@';
The Byte type is defined with the keyword: byte
. The byte
is an 8-bit signed integer that can be set to any integer between -128
to 127
:
byte myByte = 123;
myByte = -37;
The Short type is defined with the keyword: short
. The short
is a 16-bit signed integer that can be set to any integer between -32768
to 32767
:
short myShort = 1423;
myShort = -3787;
The Integer type is defined with the keyword: int
. The int
is a 32-bit signed integer that can be set to any integer between -2,147,483,648
to 2,147,483,647
:
int myInteger = 123;
myInteger = -456;
The Long type is defined with the keyword: long
. The long
is a 64-bit signed integer that can be set to any integer between -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
, appended with an L
:
long myLong = 9223372036854775807L;
myLong = -223372036854777L;
The Float type is defined with the keyword: float
. You can set the value to be a decimal number with around 7-digits of precision and appended with an f
:
float myFloat = 123.23f;
myFloat = -45.24743f;
The Double type is defined with the keyword: double
. You can set the value to be a decimal number with around 15-digits of precision:
double myDouble = 123.223;
myDouble = -45.24743245909;
In Java, the keyword null
represents the absence of value.
While reference types automacially support being set to null
, primitive types require an actual value. When you need to assign null
to a primitive type, you employ a reference "wrapper" class for that type:
// Here's a reference wrapper class for the boolean type
Boolean isBoolean = true;
isBoolean = null;
// You can do the same for other primitive types
Integer myInteger = null;
myInteger = 0;
// Reference types support null automatically
String myString = "Hello World";
myString = null;
Note: null
is not the same as 0
or an empty string: ""
0
and ""
are actual values, stored in memory or to disk as 0
or ""
null
represents the absence of value: nothing is stored in memory or to diskA reference type is set by storing the actual data (object) in memory and storing a reference to the object within the variable. Reference types in Java automatically support being set null
.
A String is a sequence of zero or more unicode characters, surrounded by double quotes.
String myString = "Hello World!";
You can concatenate strings with the +
symbol:
String message = "What's " + "up?"; // message equals "What's up?"
You can also perform string interpolation:
String name = "Cody Winton";
String message = String.format("My name is %s?", name); // message equals "My name is Cody Winton?"
You can split a single string into an array of strings:
// This splits `aString` using a separator `String` value
String aString = "1,2,3"; // the string to split
String[] numbers = aString.split(","); // numbers equals { "1", "2", "3" };
Arrays allow you to combine multiple variables of the same type in a single variable. You declare an array type by specifying the type, followed by double square brackets type[]
.
// We've created an array that can hold up to 5 ints
int[] ints = new int[5];
// You can also use type inference when you provide the variables for the array to contain
int[] intsLiteral = new [] { 7, 58, 5 }; // Array with set values using curly bracket notation
Arrays are zero-based index, meaning that the first value is stored at index 0
. You can access a value using its index, along with square bracket notation.
int[] intsLiteral = new [] { 7, 58, 5 };
int firstNumber = intsLiteral[0]; // Equals 7
int secondNumber = intsLiteral[1]; // Equals 58
intsLiteral[2] = 10; // Did change 5 to 10
You can join an array of strings into a single string:
String[] numbers = new [] { "1", "2", "3" }; // the array to join
String allNums = String.join(",", numbers); // allNums equals "1,2,3"
An Enum type is a distinct type used when you want to limit the value set to a fixed amount of options. This allows for very readable code and a delightful autocomplete experience within Eclipse.
// This enum defines every week day
public enum Day { SUN, MON, TUE, WED, THU, FRI, SAT }
// Can only be set to a day of the week
Day dayOfWeek = Day.TUE;
dayOfWeek = Day.WED;
With a primitive type, when you copy a variable you create a clone of the actual value.
Let's see how this works with an int
int a = 1; // primitive type int
int b = a; // clone the value of `a`
a = 2; // Change the contained value of `a`
// Now a equals 2 && b equals 1
With a reference type, when you copy a variable you create a clone of the reference to the same actual data value:
Let's see how this works with a class called Animal
.
Animal c = new Animal("Fido"); // reference type Animal
Animal d = c; // copy of reference to same object
// currently c.name equals "Fido" && d.name equals "Fido", but let's change that:
c.name = "Fluffy";
// You changed the property of the actual object
// So now c.name equals "Fluffy" && d.name equals "Fluffy"