|
java.lang package.
|
Object
class
Class java.lang.Object is the root
of the java class hierarchy. Every class has
Object as a superclass. All objects,
including arrays, inherit the methods of this
class.
More in ebook
Important methods.
- toString() method of Object class returns
string representation of the object.
public String toString();
- equals() method of Object class compare the
object references.
public boolean equals(Object o);
Note :- If objects of a particular class are to be stored
and retrieved from a collection, it is important for that class
to override equals() method and hashcode() methods. Since
equals() in Object class only compare object references, it
returns true only when both object
references are pointing to the same object. Hence, this methods
needs to be overridden to do logical equality test.
(More)
- The hashCode method defined by class Object
returns distinct integers for distinct objects.
This is typically implemented by converting the
internal (memory) address of the object into an
integer.
public native int hashCode();
The relation between equals() method and hashcode() is such
that if two objects are equal according to the equals(Object)
method, then calling the hashCode() method on each of the two
objects must produce the same integer result.
Note that it is generally necessary to
override the hashCode() whenever equals() is
overridden.
- Object class has supporting methods for
java threading mechanism. These methods are
namely wait(), notify and notifyAll().
(
More in ebook)
|
|
Math
Class
More in ebook
More in ebook
|
|
|
Wrapper
classes
More in ebook
- java.lang package provides standard library
classes that are closely related to primitives.
These are called as wrappers.
- These classes are Byte, Short, Character,
Integer, Long, Float, Double and Boolean.
- Wrapper class names differ from the
primitives only in the initial upper case
letter.
Exceptions are Integer, which wraps
int and
Character, which wraps char values.
- All wrapper objects can be constructed by
passing a string EXCEPT Character wrapper class
for char data type. Most of these constructor
throws NumberFormatException, which is a
runtime exception.
- Wrapper classes override equals() method.
equals() on wrappers return false if both the
objects are not instances of the same class.
This statement is true even when the value they
are wrapping has the same numerical value.
- All wrapper objects are immutable. Once an
object is created, the wrapped primitive value
cannot be changed.
More in ebook
- Wrapper classes are final and hence cannot be
sub classed.
|
|
Wrappers practical use
Primitives cannot be directly added to a
collection such as Vector. Therefore, they are
first wrapped into the corresponding wrapper as
an object and then stored.
|
|
String
More in ebook
- java.lang.String is a final class hence it
cannot be sub classed.
- String object represents a sequence of 16
bit unicode characters.
- Strings are immutable which means a String
object has a constant (unchanging) value.
More in ebook
- String literals are created and stored on
the pool of literals.
- equals() in String class compares Strings.
It true if the argument is not null and is a
String object that represents the same sequence
of characters as this object.
More in ebook
|
|
StringBuffer
|
|
String Concatenation and operator +
More in ebook
Consider expression,
expr1 + expr2 + expr3
And if any of the three expressions is of type String then
,
expr1 + expr2 +
expr3 is equivalent to new
StringBuffer().append(expr1).append(expr2).append(expr3).toString();
|
|