Introduction

Reference Types

General Information

public class Cat {
    String name;
    int age;
    String color;
    String occupation;

    public Cat(String name, int age, String color, String occupation) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.occupation = occupation;
    }
}


public static void main(String[] args) {
    Cat MrTibbens = new Cat("Mr.Tibbens", 3, "Black", new Occupation("World Domination"));
    Cat Khajiit = new Cat("Khajiit", 5, "White", new Occupation("Thief"));
}

Main Pre-declared Reference Types

Reference Type Description
Annotation Provides a way to associate metadata (data about data) with program elements.
Array Provides a fixed-size data structure that stores data elements of the same type.
Class Designed to provide inheritance, polymorphism, and encapsulation. Usually models something in the real world and consists of a set of values that holds data and a set of methods that operates on the data.
Enumeration A reference for a set of objects that represents a related set of choices.
Interface Provides a public API and is “implemented” by Java classes.

*Source OREILLY

Example:

public class refTest{
    public static class House{
        private int price;
        public House(int price){
            this.price = price;
        }
        public void setPrice(int newPrice){
            this.price = newPrice;
        }
        public int getPrice(){
            return this.price;
        }
    }

    public static void main(String[] args){
        House house1 = new House(0);
        house1.setPrice(500000); // setting house price to $500,000, accessing same spot in memory to change
        System.out.println("House Price: " + "$" + house1.getPrice()); // printing the house price that was set
        House house2 = new House(0);
        house2 = house1; // telling house 2 to reference the same spot in memory as house 1. price should be the same :)
        System.out.println("House Price of House 2: " + "$" + house1.getPrice()); // printing the house price that was set
    }
}
refTest.main(null);
House Price: $500000
House Price of House 2: $500000

Value Types

Value Types (Primitive Types)

General Information

Differences

  1. Number and Definition:
  2. Memory Location:
  3. Assignment (Note the Difference!):
  4. Method Parameter Passing:
Reference Types Primitive Types
Unlimited number of reference types, as they are defined by the user. Consists of boolean and numeric types: char, byte, short, int, long, float, and double.
Memory location stores a reference to the data. Memory location stores actual data held by the primitive type.
When a reference type is assigned to another reference type, both will point to the same object. When a value of a primitive is assigned to another variable of the same type, a copy is made.
When an object is passed into a method, the called method can change the contents of the object passed to it but not the address of the object. When a primitive is passed into a method, only a copy of the primitive is passed. The called method does not have access to the original primitive value and therefore cannot change it. The called method can change the copied value.

*Source OREILLY

public class PrimitiveTypesExample {
    public static void main(String[] args) {
        // Declaration and initialization of primitive variables
        boolean isJavaFun = true;
        char grade = 'A';
        byte byteValue = 127; // byte range: -128 to 127
        short shortValue = 32000; // short range: -32,768 to 32,767
        int intValue = 42;
        long longValue = 123456789L; // The 'L' indicates a long literal
        float floatValue = 3.14f; // The 'f' indicates a float literal
        double doubleValue = 2.71828;

        // Displaying values
        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Grade: " + grade);
        System.out.println("Byte Value: " + byteValue);
        System.out.println("Short Value: " + shortValue);
        System.out.println("Int Value: " + intValue);
        System.out.println("Long Value: " + longValue);
        System.out.println("Float Value: " + floatValue);
        System.out.println("Double Value: " + doubleValue);
    }
}

PrimitiveTypesExample.main(null);
Is Java fun? true
Grade: A
Byte Value: 127
Short Value: 32000
Int Value: 42
Long Value: 123456789
Float Value: 3.14
Double Value: 2.71828

Hacks

Part 1

public class Person {
    String name;
    int age;
    int height;
    String job;

    public Person(String name, int age, int height, String job) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.job = job;
    }
}

public static void main(String[] args) {
    Person person1 = new Person("Carl", 25, 165, "Construction Worker");
    Person person2 = new Person("Adam", 29, 160, "Truck Driver");
    Person person3 = person1;
    int number = 16;
    System.out.println(number);
}
main(null);
16

Answer the following questions based on the code above:

  1. What kind of types are person1 and person2?
  2. Do person1 and person3 point to the same value in memory?
  3. Is the integer “number” stored in the heap or in the stack?
  4. Is the value that “person1” points to stored in the heap or in the stack?

Part 2

Question 1: Primitive Types vs Reference Types (Unit 1)

Situation: You are developing a banking application where you need to represent customer information. You have decided to use both primitive types and reference types for this purpose.

(a) Define primitive types and reference types in Java. Provide examples of each.

(b) Explain the differences between primitive types and reference types in terms of memory allocation and usage in Java programs.

(c) Code:

You have a method calculateInterest that takes a primitive double type representing the principal amount and a reference type Customer representing the customer information. Write the method signature and the method implementation. Include comments to explain your code.

public class BankingApplication {
    
    // this is the customer class used in the calculateInterest method
    public static class Customer {
        
        private String name; // refrence type

        public Customer(String name) {
            this.name = name;
        }
    }

    // method signature for calculating interest
    public static double calculateInterest(double principalAmount, Customer customer) {

        double interestRate = 0.05; // 5%    primitive type
        double interest = principalAmount * interestRate; // primitive type
        
        return interest;
    }

    public static void main(String[] args) {
        Customer customer = new Customer("Soham Kamat");
        
        double principalAmount = 1000.00;
        double interest = calculateInterest(principalAmount, customer);
        
        System.out.println("Interest: $" + interest);
        System.out.println("Total: $" + (interest + principalAmount));
    }
}

BankingApplication.main(null);
Interest: $50.0
Total: $1050.0