4.1.1.1 - Data types and references (Java)
Choose suitable Java types, distinguish values from references, and group related data into arrays and your own structured types.
Java route status: incomplete: runnable support pending. The source examples and code answers here are written practice.
Built-in types and representation
A type determines the values a piece of data can hold and the operations that make sense on it.
Data type
A classification of data that defines its possible values and permitted operations.
An integer count can be added; a Boolean decision can be combined with another condition. The digits in a telephone number are usually a string because leading zeroes matter and arithmetic does not.
| Required type | Java example | Suitable use |
|---|---|---|
| Integer | int, or long for larger ranges | Number of bookings |
| Real/float | double, or float | A measured temperature |
| Boolean | boolean | Whether a seat is available |
| Character | char | A single grade such as 'B' |
| String | String | A name such as "Amira" |
| Date/time | LocalDate, LocalTime, LocalDateTime from java.time | An appointment's date and local clock time |
Java is statically typed: declarations specify a type, and the compiler checks compatible operations before execution. int seats = 12; declares and initialises a variable. seats = 8; later replaces its value. A local variable must be given a value before it is read. Java identifiers and type names are case-sensitive, so String and string are different.
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
int seats = 12;
double temperature = 18.75;
boolean open = true;
char zone = 'B';
String organiser = "Amira";
LocalDateTime meeting = LocalDateTime.of(2027, 3, 5, 14, 30);
System.out.println(organiser + " " + seats);
}
}
main is the program's entry point; statements inside its braces execute in order. System.out.println displays a value and then a newline. The example displays Amira 12. Single quotes denote a character; double quotes denote a string. LocalDateTime represents a local date and time without a time zone. A structured date type permits date operations rather than fragile character slicing.
int, double, boolean and char are primitive types. Floating-point values approximate many real numbers: a double is useful for measurements but does not represent every decimal fraction exactly. A type also has limits: an int is a fixed-size signed whole number, not an unlimited mathematical integer. These limits should inform a choice of type.
References and dynamic objects
A reference variable identifies an object rather than containing all of that object's data.
Reference
A value used to locate an object created in memory. At the abstract level it refers to that object's memory location; Java hides the actual address and does not allow pointer arithmetic.
An explicit pointer in some languages stores a memory address and may permit operations on addresses. Java gives controlled references instead. new creates an object dynamically during execution. Arrays and class instances are objects; a variable referring to one can hold null, meaning no object. Reading an element through null is an error.
int[] original = {4, 7};
int[] alias = original;
alias[0] = 9;
System.out.println(original[0]);
alias = new int[] {1, 2};
System.out.println(original[0]);
System.out.println(alias[0]);
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account