# Java types: "primitives" vs "reference type" variables

The Java, we are working with 2 kinds of types:

  • primitives : in the variable we keep the value (Ex.: boolean, double, float, long, int, short, byte, char)
  • references : in the variable we keep the reference to the memory address where we keep the object/value.

We consider the following example (created with Spring Boot):

package com.example.demo;

public class Car {
  String plateNumber;
  String color;

  void changePlateNumber(String plateNumber){
    this.plateNumber = plateNumber;
    System.out.println("Car :> Plate number changed.");
  }

  void changeColor(String color){
    this.color = color;
    System.out.println("Car :> Color changed.");
  }

  public Car(String plateNumber, String color) {
    this.plateNumber = plateNumber;
    this.color = color;
  }
}
package com.example.demo;

public class ProfessionalCarPeinting {

    Car peintInBlue(Car car) {
        System.out.println("Car identity (3) = "+car+" >> ProfessionalCarPeinting");
        car.color = "Blue";
        return car;
    }

}
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

	public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(Demo1Application.class, args);

		Car car1 = new Car("B 3333", "Red");
		System.out.println("Car identity (1) = "+car1+" >> main");

		var pcp = new ProfessionalCarPeinting();

		Car car2 = pcp.peintInBlue(car1);
		System.out.println("Car identity (2) = "+car2+" >> main");

		System.out.println("Car1 color = "+car1.color);

		car2.changePlateNumber("AD 22");
		System.out.println("Car1 plate number = "+car1.plateNumber);
	}
}

When we run we can see the following result:

Car identity (1) = com.example.demo.Car@70925b45 >> main
Car identity (3) = com.example.demo.Car@70925b45 >> ProfessionalCarPeinting
Car identity (2) = com.example.demo.Car@70925b45 >> main
Car1 color = Blue
Car :> Plate number changed.
Car1 plate number = AD 22