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"));
}
MrTibbens
and Khajiit
of the Cat class they are stored on the stack and the values that they represent however are stored in the heap which the reference is pointing to. So this would be their respective names, age, color, occupation, etc. The object in the stack is “referring” to the value stored in the stack.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
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
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
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:
a) Both person1 and person2 are references to objects of type Person.
b) Yes, person1 and person3 point to the same value in memory. When you assign person1 to person3 (Person person3 = person1;
), you’re not creating a new object, but rather making person3 refer to the same object as person1.
c) The integer “number” is stored in the stack. Primitive types like integers are typically stored in the stack memory.
d) The value that “person1” points to (an instance of the Person class) is stored in the heap. Objects in Java are stored in the heap memory. So, person1 stores a reference to the actual object in the heap where the data for that person is stored.
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.
(a) Primitive Types vs Reference Types in Java:
int
, double
, boolean
, char
, etc. Examples:int num = 10;
double price = 20.5;
boolean isValid = true;
char grade = 'A';
String name = "John Doe";
Customer customer = new Customer();
ArrayList<Integer> numbers = new ArrayList<>();
(b) Differences between Primitive Types and Reference Types:
(c) Method Signature and Implementation:
public class BankApplication {
// Method to calculate interest
// Takes a primitive double representing principal amount
// and a reference type Customer representing customer information
public double calculateInterest(double principalAmount, Customer customer) {
// Example implementation: calculate interest based on customer type
double interestRate;
if (customer.getType() == CustomerType.REGULAR) {
interestRate = 0.05; // Regular customer interest rate
} else {
interestRate = 0.07; // Premium customer interest rate
}
return principalAmount * interestRate;
}
// Customer class for reference type example
static class Customer {
private String name;
private CustomerType type;
// Constructor
public Customer(String name, CustomerType type) {
this.name = name;
this.type = type;
}
// Getter for customer type
public CustomerType getType() {
return type;
}
}
// Enum for customer type
enum CustomerType {
REGULAR,
PREMIUM
}
// Example usage
public static void main(String[] args) {
BankApplication bankApp = new BankApplication();
Customer customer1 = new Customer("John Doe", CustomerType.REGULAR);
double principalAmount = 1000.0;
double interest = bankApp.calculateInterest(principalAmount, customer1);
System.out.println("Interest for " + customer1.getName() + ": $" + interest);
}
}
calculateInterest
method takes a primitive double
for principal amount and a reference type Customer
.Customer
class is a reference type representing customer information.main
method demonstrates how to use the calculateInterest
method with a Customer
object.