Chapter 4: Some Standard Classes: Object Class's Methods
Chapter 4: Some Standard Classes: Object Class's Methods
The reason the Object class exists is to provide default methods to all of its subclasses. It is not an abstract class, and
all of its methods have implementations. If any of the methods that Object provides are not suitable, then they are
simply overriden by one of the subclasses.
Line 2 System.out.println(s);
Output: SavingsAccount@fea485c4, where the output just informs the user the memory location of where that
SavingsAccount is located. It is quite useless. In order for a more useful output, the method must be overridden.
The equals method does not indicate if all the instance variables belonging to two objects are equal. This means
that if I have two Student objects, and they both have one instance variable "public name," and that name is equal, I
will not get true from the equals method.
You will only get true from the equals method if two reference variables are referencing the same object, meaning
if two reference variables represent the same object. Only then will the equals method give you a true.
Syntax: If d1 and d2 are objects, you can invoke the equals Method by stating:
(d1.equals(d2)) or (d2.equals(d1))
In addition, the equals method's implementation is equivalent to the == relation for objects. This means that we could
also have said:
NOTE The difference between the equals Method and the == operator:
The .equals() method checks for content comparison and the == operator checks for reference comparison.
String Objects:
An object of type String is a sequence of characters. Strings are non - primitive data types, as their memory locations
can expand to fit the entire length of the String.
Immutable: This means not able to be changed. String objects are immutable. However, you can created a new String
that is a mutated form of an existing String.
Although if there is a currently existing string object, you cannot change (mutate) it, because it is immutable, what you
can do is reassign that String reference to another string. Simple!
String s = "John";
s = "Harry"; // Here the string is redefined, NOT MUTATED!
// You can reassign the String reference variable s using the concatenation operator.
s = s + " Windsor";
It is important to note that as long as one of the operands is a string, the rest can be integers, doubles, or any
primitive type. Here, the non - String operand is converted to a String, and concatenation occurs.
Let's say that you want to concatenate two non - String operands. How do you do this? Well, simply use the dot
operator along with the toString() method to first convert the operands to strings, and then concatenate using the
Concatenation Operator.
Use the compareTo method. If you use it with the dot operator, you will have a couple of different possible outputs.
Let's examine them here:
Characters are compared according to their position in the ASCII chart. You need to know that all digits precede
capital letters, which precede all lowercase letters themselves. In addition, if the strings have identical characters, but
one ends before the other one, the string that ends first comes before the longer string.
New string, starts at startIndex, ends at endIndex - 1. startIndex is the first character in the substring where it
should start, and endIndex is the first character that you do not want.
Returns the index of the first occurrence of str within this string. If str is not a substring, 1 is returned.
Wrapper Classes
The wrapper class takes an existing object or a value of primitive type and "wraps or "boxes" it in an object, while
providing a new set of methods for that type.
It can be used in Java container classes that require the items to be objects.
The wrapper class allows the construction of an object from a single value through wrapping or boxing the primitive in
a wrapper object, as well as retrieval of the primitive value by unwrapping or unboxing from the wrapper object.
Integer(int value)
Returns 0 if the value of this Integer is equal to the value of other, a negative integer if it is less than the value
of other, and a positive integer if it is greater than the value of other.
int intValue()
Returns true if and only if this Integer has the same int value as obj.
String toString()
Returns 0 if the double values are equal, a negative integer if the double is less than the the value of other, and a
positive integer if the double is greater than the value of other.
Overrides equals in class Object and throws a ClassCastException if obj is not a Double or a double. Otherwise, it
returns true if and only if this Double has the same double value as obj.
String toString()
static double pow(double base, double exp), returning base^exp, assuming base > 0, or base = 0 and exp > 0, or base
< 0 and exp is an integer.
static double random(), returning a random number r, where 0.0 ≤ r < 1.0.
Little Technicality: Sometimes, it is tedious to always call Math Class functions everytime you use them. Thus, it is
possible to use the static import construct to use the static members of a class without the name prefix.
Random Numbers
The Math.random(); method produces a random real number r from 0.0 ≤ r < 1.0. In order to shift the range of the
random numbers, linear transformations can be made. The general structure for doing a linear shift is:
Example: Range lowV ≤ x < highV, where double x = (highV - lowV * Math.random() + lowV; V = Value)
Random Integers
Random integers are also very simple (at least for me) to understand. They can be created by using the
Math.random(); method and then downcasting its boolean value to int. Then, linear shifting can be applied.
Remember, however, when downcasting using (int), the new integers do not round. Take account of this.