Java Objects

CSCI 1933 – Introduction to Algorithms and Data Structures
Adriana Picoral

Practice

  1. Download Complex.java
  2. Compile it and run it

Classes

  • Grouped methods and attributes (information/values)
  • Template, blueprint of cookie cutter to construct many objects

Modularity: Classes allow us to split the problem to be solved into distinct tasks

You should be able to think about what a class does, not how it does it

Objects

Java Objects are title-case

  • Example: String
    • Only object that gets special syntax (no need to call constructor with new)
    • Immutable
    • Use double quotes

Revisit types in Java

Two “flavors” of types:

  • Primitive (often called scaler or base)
    • Primitive type names are lower-case
    • variable literally and directly stores value
  • Object (reference)
    • Objects are title-case
    • variable stores a reference to object

Java Data Types and Memory Space

1 byte is 8 binary digits

  • short (2 bytes)
  • long (8 bytes)
  • double (8 bytes)
  • boolean (1 bit)
  • int (4 bytes)
  • float (4 bytes)
  • byte (1 byte)
  • char (2 bytes)

All Objects (everything else) (4 bytes)

Discussion

  • Why is Object data always 4 bytes even though the Object itself can be much larger?
  • The simple types are all fixed in size
  • How is this good?
  • How is this limiting?

String

String is a class (title capitalization)

We can construct strings two ways:

String str = new String("with new");
String str2 = "Don't need new"

Some string methods:

.equals(String otherString)
.length() // len() function in python
.charAt(int index) // [] indexing in python
.substring(int beginIndex, int endIndex) // [:] indexing in python
.indexOf(String str) // .find() method in python

Objects

These work like pointers:

  • variable stores location, not object itself
  • variable defaults to null (meaning “not pointing to object”)

Comparison == not the same as .equals()

public static void main(String[] args) {
        String wordOne = new String("Hello");
        String wordTwo = new String("Hello");
        
        System.out.println(wordOne == wordTwo);
        System.out.println(wordOne.equals(wordTwo));

    }

Gradescope attendance

Answer this short quiz on comparisons of objects on gradescope

Practice

  1. Download ComplexNumber.java
  2. Compile it and run it

Discuss

  • What are the attributes?
  • What are the constructors?
  • How do we call a constructor to create an object?
  • What are the methods?

In-class exercise

Write a class called MyTimejava that, given number (integer) as seconds, it converts it to hours:minutes:seconds. Your application should print the equivalent hours, minutes, and seconds in this format: hours:minutes:seconds

Examples: 1 should be converted to the "0:0:1", 3661 should be converted to the "1:1:1", 8274 should be converted to the "2:17:54"

HINTS: There are 3600 seconds in one hour. Use % to calculate minutes and seconds left.

“Traditional” Solution (no object instantiation)

public class MyTime {

    private static int getHours(int seconds) {
        return seconds/3600;
    }

    private static int getMinutes(int seconds) {
        return seconds/60;
    }

    public static String getTime(int seconds) {
        // get full hours
        int hours = getHours(seconds);
        // update how many seconds are left
        int secondsLeft = seconds - (hours * 3600);
        // calculate full minutes
        int minutes = getMinutes(secondsLeft);
        // update how many seconds are left
        secondsLeft = secondsLeft - (minutes * 60);
        
        return hours + ":" + minutes + ":" + secondsLeft;
    }

    public static void main(String[] args) {
        int seconds = 119;
        String result = getTime(seconds);
        System.out.println(result);

    }
}

Looking back

  • Good to know: the difference between simple/primitive and Object data
  • Good to know: the difference between a class and an Object