wisconsin
Objects

Objects

Curated by Last modified: 3 min read

Objects are a way to store data and functions together. They are a way to model real-world entities in code. For example, a Car object might have data like make, model, and year, and functions like drive() and stop().

Note

Strings, arrays, and other data types (except for primitive types) are objects in Java.

Representing Objects

To create an object in Java, you use the new keyword followed by the class name and parentheses. For example, to create a Car object, you would write:

Car myCar = new Car();

If the class has a constructor that takes parameters, you would pass those parameters in the parentheses. For example, if the Car class has a constructor that takes a make, model, and year, you would write:

Car myCar = new Car("Honda", "Civic", 2022);

In Java, objects are referenced by pointers. When you create an object, you are creating a pointer to a location in memory where the object is stored. The object itself is not stored in the variable; the variable only holds the memory address of the object.

This is different from primitive data types, which are stored directly in the variable.

graph LR
    A[myCar] -- Pointer to 0x1234 --> B["Car(#quot;Honda#quot;, #quot;Civic#quot;, 2022)"]
    myInt --> 5

Calling Methods on Objects

To call a method on an object, you use the dot operator (.) followed by the method name and parentheses. For example, to call the drive() method on a Car object, you would write:

myCar.drive();

If the method takes parameters, you would pass those parameters in the parentheses. For example, if the drive() method takes a speed parameter, you would write:

myCar.drive(60);

Static Methods vs. Instance Methods

In Java, methods can be either static or instance methods. Static methods are associated with the class itself, while instance methods are associated with individual objects of the class.

Tip

Static methods can immediately be differentiated from instance methods by the capitalization of the class name. For example, the Math class has static methods like Math.abs() and Math.sqrt(), while the String class has instance methods like String.length() and String.charAt().

When a class is capitalized, it refers to the class itself, while when the class is lowercase, it refers to an instance of the class.

Wrapper Classes

Wrapper classes are classes that wrap primitive data types. They provide a way to treat primitive data types as objects. For example, the Integer class wraps the int primitive data type.

int i = 5;
Integer integer = new Integer(i);

Note

Autoboxing and unboxing allow you to automatically convert between primitive data types and their corresponding wrapper classes.

With wrapper classes, you can use methods and properties that are not available with primitive data types. For example, the Integer class has methods like parseInt() and toString().

Additionally, wrapper classes are used in collections and generics, as they only accept objects and not primitive data types.

List<Integer> numbers = new ArrayList<>();

numbers.add(5); // Autoboxing: converts int to Integer

Null

In Java, null is a special value that represents the absence of a value. It can be assigned to any reference type variable, including objects. When a variable is assigned null, it means that it does not refer to any object in memory.

Car myCar = null;

Attempting to access a method or property on a null object will result in a NullPointerException. It is important to check if an object is null before using it to avoid this exception.

if (myCar != null) {
    myCar.drive();
}

Source

The existing course note was retained and normalized during the merge.

Related: Topic index.

Linked to this page