Skip to content

Java Programming · Week 6 notes

Objects and Wrapper Classes

Introduction to Objects and Wrapper Classes

Overview: Understanding Objects and Wrapper Classes in Java

What are Objects?

Objects in Java are instances of classes. They represent real-world entities and contain both data (attributes) and code (methods).

What are Wrapper Classes?

Wrapper classes in Java provide a way to use primitive data types as objects. They "wrap" primitive types into objects, allowing them to be used in contexts that require objects.

Key Concepts

  • Object Creation
    • Objects are created using the 'new' keyword
    • Example:
      String str = new String("Hello, World!");
  • Wrapper Classes
    • Java provides wrapper classes for each primitive type
    • Example:
      Integer num = new Integer(42);
  • Autoboxing and Unboxing
    • Autoboxing: Automatic conversion of primitive types to wrapper objects
    • Unboxing: Automatic conversion of wrapper objects to primitive types
    • Example:
      Integer num = 42; // Autoboxing int value = num; // Unboxing

Why are Objects and Wrapper Classes important?

  • Objects allow for more complex and organized code structures
  • Wrapper classes enable the use of primitive types in object-oriented contexts
  • They provide useful methods for data manipulation and conversion
  • Essential for working with Java collections and generics

Select a lesson from the sidebar to dive deeper into objects and wrapper classes in Java!

Object Basics

Understanding Object Basics in Java

Objects are fundamental to Java programming. They are instances of classes and represent real-world entities in code.

Creating Objects

ClassName objectName = new ClassName();

Components of an Object

  • State: Represented by instance variables (attributes)
  • Behavior: Represented by methods

Example

public class Car { // State (attributes) String brand; String model; int year; // Constructor public Car(String brand, String model, int year) { this.brand = brand; this.model = model; this.year = year; } // Behavior (method) public void startEngine() { System.out.println("The " + brand + " " + model + " is starting..."); } public static void main(String[] args) { Car myCar = new Car("Toyota", "Corolla", 2022); myCar.startEngine(); // Outputs: The Toyota Corolla is starting... } }

Key Points

  • Objects are created from classes using the 'new' keyword
  • They encapsulate data and behavior
  • Objects interact with each other through method calls
  • The 'this' keyword refers to the current object

Wrapper Classes

Understanding Wrapper Classes in Java

Wrapper classes provide a way to use primitive data types as objects. Java provides a wrapper class for each primitive type.

Primitive Types and Their Wrapper Classes

  • byte - Byte
  • short - Short
  • int - Integer
  • long - Long
  • float - Float
  • double - Double
  • boolean - Boolean
  • char - Character

Creating Wrapper Objects

Integer num = new Integer(42); Double pi = new Double(3.14);

Autoboxing and Unboxing

Java provides automatic conversion between primitive types and their wrapper classes.

// Autoboxing Integer num = 42; // Unboxing int value = num;

Useful Methods in Wrapper Classes

  • parseInt() in Integer class
  • valueOf() in all wrapper classes
  • toString() to convert to String

Example

public class WrapperExample { public static void main(String[] args) { // Creating wrapper objects Integer num1 = new Integer(42); Integer num2 = Integer.valueOf(42); // Autoboxing Integer num3 = 42; // Unboxing int value = num3; // Using methods of wrapper classes String numStr = "123"; int parsedNum = Integer.parseInt(numStr); System.out.println("Parsed number: " + parsedNum); System.out.println("Maximum value of Integer: " + Integer.MAX_VALUE); } }

Object Methods

Understanding Object Methods in Java

Object methods are functions that belong to a class and operate on objects of that class. They define the behavior of objects.

Key Concepts of Object Methods

  • Methods are defined within a class
  • They can access and modify object state (instance variables)
  • Methods can take parameters and return values
  • They encapsulate behavior and promote code reuse

Example of Object Methods

public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } // Method to calculate area public double calculateArea() { return length * width; } // Method to calculate perimeter public double calculatePerimeter() { return 2 * (length + width); } // Method to check if it's a square public boolean isSquare() { return length == width; } public static void main(String[] args) { Rectangle rect = new Rectangle(5, 3); System.out.println("Area: " + rect.calculateArea()); System.out.println("Perimeter: " + rect.calculatePerimeter()); System.out.println("Is Square? " + rect.isSquare()); } }

Object Comparison

Understanding Object Comparison in Java

Comparing objects in Java is more complex than comparing primitive types. It involves understanding the difference between reference equality and content equality.

Key Concepts of Object Comparison

  • Reference Equality (==): Compares if two references point to the same object in memory
  • Content Equality (.equals()): Compares the content or state of two objects
  • The hashCode() method: Used in conjunction with equals() for consistent object comparison

Example of Object Comparison

public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Person person = (Person) obj; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name, age); } public static void main(String[] args) { Person p1 = new Person("Alice", 30); Person p2 = new Person("Alice", 30); Person p3 = p1; System.out.println("p1 == p2: " + (p1 == p2)); // false System.out.println("p1.equals(p2): " + p1.equals(p2)); // true System.out.println("p1 == p3: " + (p1 == p3)); // true } }