Object
class is the superclass of all Java classes. All Java classes inherited from this class. This makes it possible that we can have methods that are available in all Java classes. This simplifies things compared to C++ where this is not the case.
Object class methods | Description |
---|---|
boolean equals(Object o ); | Gives generic way to compare objects |
Class getClass(); | TheClass class gives us more information about the object |
int hashCode(); | Returns a hash value that is used to search objects in a collection |
void notify(); | Used in synchronizing threads |
void notifyAll(); | Used in synchronizing threads |
String toString(); | Can be used to convert the object to String |
void wait(); | Used in synchronizing threads |
protected Object clone() throws CloneNotSupportedException ; | Return a new object that are exactly the same as the current object |
protected void finalize() throws Throwable; | This method is called just before an object is garbage collected |
boolean
equals(Object
o )
method gives a generic way to compare objects for equality. You need to override it, in your class. Then you can write:public
boolean
isCustomerExist( Customer newCustomer ){boolean
isRet =false
; Iterator iter = _collAllCustomer.iterator();while
( iter.hasNext() ) {if
( newCustomer.equals( (Customer) iter.next() ) { // -- Customer was found --- isRet =true
; } } return isRet;}
Keep in mind that when you override equals(), you always need to also override hashCode() so the two methods are consistent. If two objects are equal, they must have the same hashcode.
For more information also seeJava Programming/Comparing Objects
There is aClass
object for each class in your program. Every array also belongs to a class that is reflected as aClass
object that is shared by all arrays with the same element type and number of dimensions. Theprimitive Java types (boolean
,byte
,char
,short
,int
,long
,float
, anddouble
), and the keywordvoid
are also represented asClass
objects. Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded
For more information seeClass
.
The most popular use of theClass
is to find out the object's class name during runtime.
import
com.yourCompany.Customer;...Object obj = new Customer();...System.out.println( "Name:" + obj.getClass().getName() );
The output:
Name: com.yourCompany.Customer
In most cases you should not override this method, since the default implementation of this method returns a unique number for the object. The number is used when the object is put into acollection. Finding an object in a big collection may take a while, if objects are compared one by one sequentially. To speed the search up, objects may be placed in a tree structure, weighted by an integer hash code. Comparing the hash code while navigating through the tree, the number of object comparisons can be reduced.
_______ A _____ | | __ B__ __C__| | | |D E F G... ... ... ...
To give you a general idea of how it may work, see the above diagram. Let's say we are searching object G. If at each 'node' of the tree we can decide which way to go, then by3 steps we reach the object G.
By constrast in a linear search:
A --- B ----- C ---- C ---- D ---- E ---- F ---- G
We would need8 steps to reach the object G.
So the search will be faster with the tree structure. Adding a new object however, will be slower because the tree structure needs to be maintained. The place of the new object in the tree has to be found first.
This method can be used to convert an object to aString
. It is automatically used in many places to convert objects to String; for example: in PrintStream, in StringBuffer, and for the string concatenation operator when used on objects.
The default implementation returns a weird string with the class name and the hash code.
For example:
String
str = "This customer is " + objCust;
ThetoString()
method is called on theobjCust
object.
ThetoString()
method can also be used for debugging:
public
class
Customer{private
String
_name;private
String
_address;private
String
_age;...public
String
toString() { StringBuffer buf = new StringBuffer(); buf.append( "Name = " ); buf.append( _name ); buf.append( "\n" ); buf.append( "Address= " ); buf.append( _address ); buf.append( "\n" ); buf.append( "Age = " ); buf.append( _age ); buf.append( "\n" ); ... return buf.toString(); }...}
After that whenever in your code, you want to see what a customer object is, just call:
System.out.println( objCustomer );
In a multi-threaded environment, when more than one thread can access and modify a resource, the outcome could be unpredictable. For example, let's have a counter variable that is incremented by more than one thread.
Beware!Synchronization is an ambiguous term. It doesn't consist of making all threads executing the same code section at the same time. It is the opposite. It prevents any two threads from executing the same code section at the same time. It synchronizes the end of one processing with the beginning of a second processing.
![]() | Code section 1.1: Counter implementationintcounter=0;...counter+=1; |
The above code is built up by the following sub-operations:
counter
counter
Let's say that two threads need to execute that code, and if the initial value of thecounter
variable is zero, we expect after the operations the value to be 2.
Thread 1 | Thread 2 | |||
Read 0 | Read 0 | |||
Add 1 | Add 1 | |||
Save 1 | Save 1 | |||
In the above case Thread 1 operation is lost, because Thread 2 overwrites its value. We'd like Thread 2 to wait until Thread 1 finishes the operation. See below:
Thread 1 | Thread 2 | |||
Read 0 | blocked | |||
Add 1 | blocked | |||
Save 1 | unblocked | |||
Read 1 | ||||
Add 1 | ||||
Save 2 | ||||
counter+=1
must be executed by one and only one thread at any given time. That is calledcritical section. During programming, in a multi-threading environment we have to identify all those pieces of code that belongs to a critical section, and make sure that only one thread can execute those codes at any given time. That is called synchronization.synchronized
keyword.synchronized
keyword.wait()
method on the object. It has to be the same object the thread obtained its object monitor from.wait()
method only if there is at least one other thread out there who will call thenotify()
method when the resource is available, otherwise the thread will wait for ever, unless a time interval is specified as parameter.final
void wait()
methodnotifyAll
method. The thread then waits until it can re-obtain ownership of the monitor and resume execution.final
void wait(long time)
final
void notify()
final
void notifyAll()
notify()
, but it wakes up all threads that are waiting on this object's monitor. Thread.sleep(millis)
wait()
Object
class. The thread must have obtained the object-monitor of that object first before calling the wait() method. The object monitor is released by the wait() method, so it does not block other waiting threads wanting this object-monitor.