bhylu.com

Advertisements

Java Primitive Values and Object References

Java distinguishes primitive values such as int and boolean from references to objects such as String. The difference matters when you copy a variable or decide whether a value can be missing.

Understand the idea

The eight primitive types are byte, short, int, long, float, double, char and boolean. Each has defined rules rather than the same storage or arithmetic behaviour as the others. A reference value identifies an object or is null; the declared reference type controls which members are available. Copying a primitive copies its value. Copying a reference does not create a second independent copy of the referenced object.

Run it locally

Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

public class Main {
    public static void main(String[] args) {
        int first = 11;
        int second = first;
        second = 14;
        String session = "Pottery";
        System.out.println(first + ":" + second);
        System.out.println(session.length());
    }
}

Expected output

11:14
7

Changing second does not rewrite first. session is a String reference, and length is a method supplied by String. A primitive int does not have that same method. Later lessons show how shared mutable objects behave differently from independent primitive locals.

Check the official Java documentation for detailed contracts and supported options.