Learn Java
Data types and variables
There are two types of data in Java, primitive data types and objects (instances of classes).
A primitive data type consists of a set of values and operations that can be performed on them.
There are three categories of primitive data types, numbers, characters, and Booleans.
Number data types consist of integer data types and floating-point data types.
The following table summarises the integer data types (byte, short, int and long) and floating point data types (float and double).
Type | Size | Range | Default value |
---|---|---|---|
byte | 8 bits | -128 to 127 | 0 |
short | 16 bits | -32768 to 32767 | 0 |
int | 32 bits | -2147483648 to 2147483647 | 0 |
long | 64 bits | -9223372036854775808 to 9223372036854775807 | 0L |
float | 32 bits | 3 x 10(38) | 0.0F |
double | 64 bits | 17 x 10(307) | 0.0d |
The textual representation of the value of the these primitive types are called literals.
The default type of a decimal literal is type double, if you need to represent a value as type float then use the following literal number followed by an F. i.e 23F.
Floating point numbers can be represented using exponential notation. Such as 2.31E14, meaning move the decimal place 14 places to the right. Or as 2.31E-14, meaning move the decimal place 14 place to the left.
There is only one primitive data type for characters, char. Values for char consist of printable characters and non-printable characters such as control characters.
Values of the data type char can be represented by characters in single quotes such as 'a', this is called a character literal. They can also be represented by their Unicode code such as \u0072.
The last primitive data type is type boolean with the values true or false
Variables need to be declared before you can use them, this is done in a statement by giving its type followed by its name. i.e int aNumber;
A semicolon must be used at the end of each statement to inform the java interpreter it has come to the end of the statement
This statement reserves a 32-bit block of the computers memory
To assign a value to a variable we use an assignment statement i.e. aNumber = 4
Reference type variables are those variables that are declared to hold objects.
A reference type variable contains an address (reference) to where the object is stored in memory and not the actual object
This is because we cannot determine the class and therefore the size of the object that will be dynamically assigned to the variable at run-time. After a variable has been declared to hold a class, its subclass may subsequently be assigned to that variable during execution and this subclass may be a larger size.
To declare a reference type variable give the name of the class then the name of the variable as follows
Car fordFiesta;
This will declare a reference type variable called fordFiesta than can be used to reference instances of the class Car but will not yet reference anything but will hold the value null
We now need to create a Car object for the variable fordFiesta to reference. We use an operator called new that creates new objects followed by a constructor as follows.
new Car();
A constructor is some code in the class that initialises the state of an object when it is created. In the example above new creates a new Car object and Car() calls the constructor code in the class Car. The constructor always has the same name as the class of the object it initialises, in this case Car.
The new Car object then needs to be assigned to the variable fordFiesta so we can reference the new Car object
i.e. fordFiesta = new Car();
The whole process can be completed in one statement as follows
Car fordFiesta = new Car();
When a primitive type value is stored in a variable it is called value semantics
When an object is being assigned to a reference variable, the variable holds a reference to where the object is stored and this is called reference semantics
Previous Page | Page 2 | Next page |
Page 1 2 3 4 5 6 7 |