Flat Preloader Icon

Java Core Classes

Overview

  • Object
  • Class
  • System
  • String Classes
  • Arrays
  • Boxing & Unboxing
  • Varargs
  • format & printfMethods

Object

  • java.lang.Object
  • Represents a Java Object
  • All classes are descendants of Object Class.
  • Some methods
    • protected Object clone()
    • public Boolean equals()
    • protected void finalize()

Characteristics

  • Identity: Each object has a unique identity.
  • State: Objects have attributes that define their current state.
  • Behavior: Objects can perform actions (methods) based on their class’s behavior definition.

Creating Objects

  • To create an object, you use the new keyword followed by a constructor of the class.
  • Example:
				
					Car myCar = new Car("Red", "Toyota", "Camry");

				
			

Accessing Members

  • You can access an object’s attributes and methods using the dot notation.
  • Example:
				
					String carColor = myCar.color;
// Accessing an attribute
myCar.startEngine(); 
// Calling a method

				
			

Class

  • java.lang.Class
  • Gets created whenever JVM creates an object.
  • All instances of a class, share this created object of java.lang.Class
  • Methods
    • public static Class forName(“String className”)
      )
    • public Object newInstance()

Characteristics

  • Attributes (Fields/Properties): These are data members or variables that represent the characteristics or state of an object. For example, in a Car class, attributes could include color, make, and model.
  • Methods (Functions/Operations): These are functions or methods that define the behavior or actions that objects of the class can perform. For example, in a Car class, methods could include startEngine(), accelerate(), and brake().
  • Example:
				
					class Car {
    // Attributes
    String color;
    String make;
    String model;

    // Constructor
    public Car(String color, String make,
    String model) {
        this.color = color;
        this.make = make;
        this.model = model;
    }

    // Methods
    public void startEngine() {
        // Code to start the car's engine
    }

    public void accelerate() {
        // Code to accelerate the car
    }

    public void brake() {
        // Code to apply the brakes
    }
}

				
			
Java provides a rich set of core classes that are part of the Java Standard Library (also known as the Java API).