# Objects

Source: https://wisconsin.twango.dev/fa24-cs300/u1-java-programming/using-objects/objects

# Objects

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:

```java
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:

```java
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.

```mermaid
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:

```java
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:

```java
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.

```java
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.

```java
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.

```java
Car myCar = null;
```

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

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

[npe]: https://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html

## Source

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

Related: [[u1-java-programming/using-objects/index|Topic index]].
