Which method is executed by object of its class?

We'll now begin our journey into the world of object-oriented programming. We'll start with focusing on describing concepts and data using objects. From there on, we'll learn how to add functionality, i.e., methods to our program.

Object-oriented programming is concerned with isolating concepts of a problem domain into separate entities and then using those entities to solve problems. Concepts related to a problem can only be considered once they've been identified. In other words, we can form abstractions from problems that make those problems easier to approach.

Once concepts related to a given problem have been identified, we can also begin to build constructs that represent them into programs. These constructs, and the individual instances that are formed from them, i.e., objects, are used in solving the problem. The statement "programs are built from small, clear, and cooperative objects" may not make much sense yet. However, it will appear more sensible as we progress through the course, perhaps even self-evident.

Classes and Objects

We've already used some of the classes and objects provided by Java. A class defines the attributes of objects, i.e., the information related to them (instance variables), and their commands, i.e., their methods. The values of instance (i.e., object) variables define the internal state of an individual object, whereas methods define the functionality it offers.

A Method is a piece of source code written inside a class that's been named and has the ability to be called. A method is always part of some class and is often used to modify the internal state of an object instantiated from a class.

As an example,

public class Person {
    private String name;
    private int age;
}
9 is a class offered by Java, and we've made use of objects instantiated from it in our programs. Below, an
public class Person {
    private String name;
    private int age;
}
9 object named
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
1 is created and some integers are added to it.

// we create an object from the ArrayList class named integers
ArrayList<Integer> integers = new ArrayList<>();

// let's add the values 15, 34, 65, 111 to the integers object
integers.add(15);
integers.add(34);
integers.add(65);
integers.add(111);

// we print the size of the integers object
System.out.println(integers.size());

An object is always instantiated by calling a method that created an object, i.e., a constructor by using the

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
2 keyword.

The Relationship Between a Class and an Object

A class lays out a blueprint for any objects that are instantiated from it. Let's draw from an analogy from outside the world of computers. Detached houses are most likely familiar to most, and we can safely assume the existence of drawings somewhere that determine what exactly a detached house is to be like. A class is a blueprint. In other words, it specifies what kinds of objects can be instantiated from it:

Which method is executed by object of its class?

Individual objects, detached houses in this case, are all created based on the same blueprints - they're instances of the same class. The states of individual objects, i.e., their attributes (such as the wall color, the building material of the roof, the color of its foundations, the doors' materials and color, ...) may all vary, however. The following is an "object of a detached-house class":

Which method is executed by object of its class?

Loading

Loading

Loading interface...

:

Loading interface...

Log in to view the quiz

Creating Classes

A class specifies what the objects instantiated from it are like.

  • The object's variables (instance variables) specify the internal state of the object
  • The object's methods specify what the object does

We'll now familiarize ourselves with creating our own classes and defining the variable that belong to them.

A class is defined to represent some meaningful entity, where a "meaningful entity" often refers to a real-world object or concept. If a computer program had to process personal information, it would perhaps be meaningful to define a seperate class

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3 consisting of methods and attributes related to an individual.

Let's begin. We'll assume that we have a project template that has an empty main program:

public class Main {

    public static void main(String[] args) {

    }
}

Creating a New Class

In NetBeans, a new class can be created by going to the projects section located on the left, right-clicking new, and then java class. The class is provided a name in the dialog that opens.

As with variables and methods, the name of a class should be as descriptive as possible. It's usual for a class to live on and take on a different form as a program develops. As such, the class may have to be renamed at some later point.

Let's create a class named

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3. For this class, we create a separate file named
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
5. Our program now consists of two separate files since the main program is also in its own file. The
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
5 file initially contains the class definition public class Person and the curly brackets that confine the contents of the class.

public class Person {

}

After creating a new file in NetBeans, the current state is as follows. In the image below, the class

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3 has been added to the SandboxExercise.

Which method is executed by object of its class?

You can also draw a class diagram to depict a class. We'll become familiar with its notations as we go along. An empty person-named class looks like this:

Which method is executed by object of its class?

A class defines the attributes and behaviors of objects that are created from it. Let's decide that each person object has a name and an age. It's natural to represent the name as a string, and the age as an integer. We'll go ahead and add these to our blueprint:

public class Person {
    private String name;
    private int age;
}

We specify above that each object created from the

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3 class has a
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
9 and an
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
0. Variables defined inside a class are called instance variables, or object fields or object attributes. Other names also seem to exist.

Instance variables are written on the lines following the class definition

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
1. Each variable is preceded by the keyword private. The keyword private means that the variables are "hidden" inside the object. This is known as encapsulation.

In the class diagram, the variables associated with the class are defined as "variableName: variableType". The minus sign before the variable name indicates that the variable is encapsulated (it has the keyword private).

Which method is executed by object of its class?

We have now defined a blueprint — a class — for the person object. Each new person object has the variables

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
9 and
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
0, which are able to hold object-specific values. The "state" of a person consists of the values assigned to their name and age.

Loading

Defining a Constructor

We want to set an initial state for an object that's created. Custom objects are created the same way as objects from pre-made Java classes, such as

public class Person {
    private String name;
    private int age;
}
9, using the
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
2 keyword. It'd be convenient to pass values ​​to the variables of that object as it's being created. For example, when creating a new person object, it's useful to be able to provide it with a name:

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}

This is achieved by defining the method that creates the object, i.e., its constructor. The constructor is defined after the instance variables. In the following example, a constructor is defined for the Person class, which can be used to create a new Person object. The constructor sets the age of the object being created to 0, and the string passed to the constructor as a parameter as its name:

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}

The constructor's name is always the same as the class name. The class in the example above is named Person, so the constructor will also have to be named Person. The constructor is also provided, as a parameter, the name of the person object to be created. The parameter is enclosed in parentheses and follows the constructor's name. The parentheses that contain optional parameters are followed by curly brackets. In between these brackets is the source code that the program executes when the constructor is called (e.g.,

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
6).

Objects are always created using a constructor.

A few things to note: the constructor contains the expression

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
7. This expression sets the instance variable
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
0 of the newly created object (i.e., "this" object's age) to 0. The second expression
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
9 likewise assigns the string passed as a parameter to the instance variable
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
9 of the object created.

Which method is executed by object of its class?

Loading

Default Constructor

If the programmer does not define a constructor for a class, Java automatically creates a default one for it. A default constructor is a constructor that doesn't do anything apart from creating the object. The object's variables remain uninitialized (generally, the value of any object references will be

public class Person {
    private String name;
    private int age;
}
1, meaning that they do not point to anything, and the values of primitives will be
public class Person {
    private String name;
    private int age;
}
2)

For example, an object can be created from the class below by making the call

public class Person {
    private String name;
    private int age;
}
3

public class Person {
    private String name;
    private int age;
}

If a constructor has been defined for a class, no default constructor exists. For the class below, calling

public class Person {
    private String name;
    private int age;
}
4 would cause an error, as Java cannot find a constructor in the class that has no parameters.

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}

Defining Methods For an Object

We know how to create an object and initialize its variables. However, an object also needs methods to be able to do anything. As we've learned, a method is a named section of source code inside a class which can be invoked.

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}

A method is written inside of the class beneath the constructor. The method name is preceded by

public class Person {
    private String name;
    private int age;
}
5, since the method is intended to be visible to the outside world (
public class Person {
    private String name;
    private int age;
}
6), and it does not return a value (
public class Person {
    private String name;
    private int age;
}
7).

Objects and the Static Modifier

We've used the modifier

public class Person {
    private String name;
    private int age;
}
8 in some of the methods that we've written. The
public class Person {
    private String name;
    private int age;
}
8 modifier indicates that the method in question does not belong to an object and thus cannot be used to access any variables that belong to objects.

Going forward, our methods will not include the

public class Person {
    private String name;
    private int age;
}
8 keyword if they're used to process information about objects created from a given class. If a method receives as parameters all the variables whose values ​​it uses, it can have a
public class Person {
    private String name;
    private int age;
}
8 modifier.

In addition to the class name, instance variables and constructor, the class diagram now also includes the method

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2. Since the method comes with the
public class Person {
    private String name;
    private int age;
}
6 modifier, the method name is prefixed with a plus sign. No parameters are defined for the method, so nothing is put inside the method's parentheses. The method is also marked with information indicating that it does not return a value, here
public class Person {
    private String name;
    private int age;
}
7.

Which method is executed by object of its class?

The method

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2 contains one line of code that makes use of the instance variables
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
9 and
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
0 — the class diagram says nothing about its internal implementations. Instance variables are referred to with the prefix
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
8. All of the object's variables are visible and available from within the method.

Let's create three persons in the main program and request them to print themselves:

public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}

Prints:

Sample output

Ada, age 0 years Antti, age 0 years Martin, age 0 years

This as a screencast:

 

Loading

Loading

Loading

Changing an Instance Variable's Value in a Method

Let's add a method to the previously created person class that increments the age of the person by a year.

public class Main {

    public static void main(String[] args) {

    }
}
0

The method is written inside the

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3 class just as the
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2 method was. The method increments the value of the instance variable
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
0 by one.

The class diagram also gets an update.

Which method is executed by object of its class?

Let's call the method and see what happens:

public class Main {

    public static void main(String[] args) {

    }
}
1

The program's print output is as follows:

Sample output

Ada, age 0 years Antti, age 0 years

Ada, age 2 years Antti, age 0 years

That is to say that when the two objects are "born" they're both zero-years old (

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
2 is executed in the constructor). The
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
3 object's
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
4 method is called twice. As the print output demonstrates, the age of Ada is 2 years after growing older. Calling the method on an object corresponding to Ada has no impact on the age of the other person object since each object instantiated from a class has its own instance variables.

The method can also contain conditional statements and loops. The growOlder method below limits aging to 30 years.

public class Main {

    public static void main(String[] args) {

    }
}
2

Loading

Loading

Returning a Value From a Method

A method can return a value. The methods we've created in our objects haven't so far returned anything. This has been marked by typing the keyword void in the method definition.

public class Main {

    public static void main(String[] args) {

    }
}
3

The keyword void means that the method does not return a value.

If we want the method to return a value, we need to replace the

public class Person {
    private String name;
    private int age;
}
7 keyword with the type of the variable to be returned. In the following example, the Teacher class has a method
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
6 that always returns an integer-type (
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
7) variable (in this case, the value 10). The value is always returned with the return command:

public class Main {

    public static void main(String[] args) {

    }
}
4

The method above returns an

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
7 type variable of value 10 when called. For the return value to be used, it needs to be assigned to a variable. This happens the same way as regular value assignment, i.e., by using the equals sign:

public class Main {

    public static void main(String[] args) {

    }
}
5

Sample output

The grade received is 10

The method's return value is assigned to a variable of type

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }

    public void printPerson() {
        System.out.println(this.name + ", age " + this.age + " years");
    }
}
7 value just as any other int value would be. The return value could also be used to form part of an expression.

public class Main {

    public static void main(String[] args) {

    }
}
6

Sample output

Grading average 10.0

All the variables we've encountered so far can also be returned by a method. To sum:

  • A method that returns nothing has the
    public class Person {
        private String name;
        private int age;
    }
    7 modifier as the type of variable to be returned.

public class Main {

    public static void main(String[] args) {

    }
}
7

  • A method that returns an integer variable has the
    public class Person {
        private String name;
        private int age;
    
        public Person(String initialName) {
            this.age = 0;
            this.name = initialName;
        }
    
        public void printPerson() {
            System.out.println(this.name + ", age " + this.age + " years");
        }
    }
    7 modifier as the type of variable to be returned.

public class Main {

    public static void main(String[] args) {

    }
}
8

  • A method that returns a string has the
    public class Main {
    
        public static void main(String[] args) {
            Person ada = new Person("Ada");
            Person antti = new Person("Antti");
            Person martin = new Person("Martin");
    
            ada.printPerson();
            antti.printPerson();
            martin.printPerson();
        }
    }
    2 modifier as the type of the variable to be returned

public class Main {

    public static void main(String[] args) {

    }
}
9

  • A method that returns a double-precision number has the
    public class Main {
    
        public static void main(String[] args) {
            Person ada = new Person("Ada");
            Person antti = new Person("Antti");
            Person martin = new Person("Martin");
    
            ada.printPerson();
            antti.printPerson();
            martin.printPerson();
        }
    }
    3 modifier as the type of the variable to be returned.

public class Person {

}
0

Let's continue with the Person class and add a

public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
4 method that returns the person's age.

public class Person {

}
1

The class in its entirety:

Which method is executed by object of its class?

Let's illustrate how the method works:

public class Person {

}
2

Sample output

Pekka's age 2 Antti's age 1

Pekka's and Antti's combined age 3 years

Loading interface...

:

Loading interface...

Log in to view the quiz

Loading

Loading

As we came to notice, methods can contain source code in the same way as other parts of our program. Methods can have conditionals or loops, and other methods can also be called from them.

Let's now write a method for the person that determines if the person is of legal age. The method returns a boolean - either

public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
5 or
public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
6:

public class Person {

}
3

And let's test it out:

public class Person {

}
4

Sample output

underage: Antti, age 1 years of legal age: Pekka, age 30 years

Let's fine-tune the solution a bit more. In its current form, a person can only be "printed" in a way that includes both the name and the age. Situations exist, however, where we may only want to know the name of an object. Let's write a separate method for this use case:

public class Person {

}
5

The

public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
7 method returns the instance variable
public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
9 to the caller. The name of this method is somewhat strange. It is the convention in Java to name a method that returns an instance variable exactly this way, i.e.,
public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
9. Such methods are often referred to as "getters".

The class as a whole:

Which method is executed by object of its class?

Let's mould the main program to use the new "getter" method:

public class Person {

}
6

The print output is starting to turn out quit neat:

Sample output

Antti is underage Pekka is of legal age

Loading

A string representation of an object and the toString-method

We are guilty of programming in a somewhat poor style by creating a method for printing the object, i.e., the

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2 method. A preferred way is to define a method for the object that returns a "string representation" of the object. The method returning the string representation is always
public class Main {

    public static void main(String[] args) {

    }
}
01 in Java. Let's define this method for the person in the following example:

public class Person {

}
7

The

public class Main {

    public static void main(String[] args) {

    }
}
01 functions as
public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2 does. However, it doesn't itself print anything but instead returns a string representation, which the calling method can execute for printing as needed.

The method is used in a somewhat surprising way:

public class Person {

}
8

In principle, the

public class Main {

    public static void main(String[] args) {

    }
}
04 method requests the object's string representation and prints it. The call to the
public class Main {

    public static void main(String[] args) {

    }
}
01 method returning the string representation does not have to be written explicitly, as Java adds it automatically. When a programmer writes:

public class Person {

}
9

Java extends the call at run time to the following form:

public class Person {
    private String name;
    private int age;
}
0

As such, the call

public class Main {

    public static void main(String[] args) {

    }
}
06 calls the
public class Main {

    public static void main(String[] args) {

    }
}
01 method of the
public class Main {

    public static void main(String[] args) {

    }
}
08 object and prints the string returned by it.

We can remove the now obsolete

public class Person {
    private String name;
    private int age;

    public Person(String initialName) {
        this.age = 0;
        this.name = initialName;
    }
}
2 method from the Person class.

The second part of the screencast:

 

Loading

Method parameters

Let's continue with the

public static void main(String[] args) {
    Person ada = new Person("Ada");
    // ...
}
3 class once more. We've decided that we want to calculate people's body mass indexes. To do this, we write methods for the person to set both the height and the weight, and also a method to calculate the body mass index. The new and changed parts of the Person object are as follows:

public class Person {
    private String name;
    private int age;
}
1

The instance variables

public class Main {

    public static void main(String[] args) {

    }
}
11 and
public class Main {

    public static void main(String[] args) {

    }
}
12 were added to the person. Values for these can be set using the
public class Main {

    public static void main(String[] args) {

    }
}
13 and
public class Main {

    public static void main(String[] args) {

    }
}
14 methods. Java's standard naming convention is used once again, that is, if the method's only purpose is to set a value to an instance variable, then it's named as
public class Main {

    public static void main(String[] args) {

    }
}
15. Value-setting methods are often called "setters". The new methods are put to use in the following case:

public class Person {
    private String name;
    private int age;
}
2

Prints:

Sample output

Matti, body mass index is 26.54320987654321 Juhana, body mass index is 20.897959183673468

A parameter and instance variable having the same name!

In the preceding example, the

public class Main {

    public static void main(String[] args) {

    }
}
13 method sets the value of the parameter
public class Main {

    public static void main(String[] args) {

    }
}
17 to the instance variable
public class Main {

    public static void main(String[] args) {

    }
}
11:

public class Person {
    private String name;
    private int age;
}
3

The parameter's name could also be the same as the instance variable's, so the following would also work:

public class Person {
    private String name;
    private int age;
}
4

In this case,

public class Main {

    public static void main(String[] args) {

    }
}
11 in the method refers specifically to a parameter named height and
public class Main {

    public static void main(String[] args) {

    }
}
20 to an instance variable of the same name. For example, the following example would not work as the code does not refer to the instance variable height at all. What the code does in effect is set the
public class Main {

    public static void main(String[] args) {

    }
}
11 variable received as a parameter to the value it already contains:

public class Person {
    private String name;
    private int age;
}
5

public class Person {
    private String name;
    private int age;
}
6

Loading

Calling an internal method

The object may also call its methods. For example, if we wanted the string representation returned by toString to also tell of a person's body mass index, the object's own

public class Main {

    public static void main(String[] args) {

    }
}
22 method should be called in the
public class Main {

    public static void main(String[] args) {

    }
}
01 method:

public class Person {
    private String name;
    private int age;
}
7

So, when an object calls an internal method, the name of the method and this prefix suffice. An alternative way is to call the object's own method in the form

public class Main {

    public static void main(String[] args) {

    }
}
24, whereby no emphasis is placed on the fact that the object's own bodyMassIndex method is being called:

public class Person {
    private String name;
    private int age;
}
8

The screencast's third part:

 

Loading

Loading

Rounding errors

You probably noticed that some of the figures have rounding errors. In the previous exercise, for example, Pekka's balance of 30.7 may be printed as

public class Main {

    public static void main(String[] args) {

    }
}
25. This is because floating-point numbers, such as
public class Main {

    public static void main(String[] args) {
        Person ada = new Person("Ada");
        Person antti = new Person("Antti");
        Person martin = new Person("Martin");

        ada.printPerson();
        antti.printPerson();
        martin.printPerson();
    }
}
3, are actually stored in binary form. That is, in zeros and ones using only a limited number of numbers. As the number of floating-point numbers is infinite — (in case you're wondering "how infinite?", think how many floating-point or decimal values fit between the numbers 5 and 6 for instance). All of the floating-point numbers simply cannot be represented by a finite number of zeros and ones. Thus, the computer must place a limit on the accuracy of stored numbers.

Normally, account balances, for instance, are saved as integers such that, say, the value 1 represents one cent.

Which is a method from the Object class?

The Object class provides multiple methods which are as follows: tostring() method. hashCode() method. equals(Object obj) method.

Which three of the following are methods of the Object class?

Object class has below methods.
toString() Method..
hashCode() Method..
equals(Object obj) Method..
getClass() method..
finalize() method..
clone() method..
wait(), notify() notifyAll() Methods..