java.lang.Object
- See Also:
- Top Examples, Source Code,
Class
protected Object clone()
throws CloneNotSupportedException - See Also:
Cloneable
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1524]DecimalFormat By ednava58 { at } yahoo { dot } com on 2005/08/28 17:15:11 Rate
DecimalFormat percent = new DecimalFormat ( "0%" ) ; [1754]How many ways can we create an Object? By Anonymous on 2006/05/04 15:49:45 Rate
I can think of 6 possible ways: 1. call new ( ) which calls a constructor of a class 2. In a singleton class, call method named getInstance ( ) which returns a reference to a new single object the first time it is called. But, under the hood, it still calls the private constructor. Singleton is a pattern, meaning there is only one instance of the object. [ NB: on subsequent calls, getInstance ( ) returns the same singleton object, so the object is no longer "a newly created object". ] 3. Deserialize a object from data that is stored within a byte [ ] array, stored within a disk file, or has been received from some network prototocol ( for example, from a socket that uses TCP/IP protocol. ) . 4. Call a method which returns a created object. ( This might not be a fair answer, since the called method has to do 1, 2, or 3 internally ) . 5. Use reflection: http://javaalmanac.com/egs/java.lang.reflect/UseConstructor.html 6. Use dynamic proxy object: http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html
public boolean equals(Object obj) - See Also:
Hashtable , hashCode()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[385]Two equals objects have matching hashCode()s as well By Ry4an Brase on 2003/09/09 03:45:17 Rate
// The key is to make sure that two equals objects have matching // hashCode ( ) s as well public boolean equals ( Object o ) { if ( o == null || ! ( o instanceof ThisClass ) ) { return false; } if ( aField != o.aField ) { // and so on return false; } // ASSERT o.hashCode ( ) == hashCode ( ) return true; } [1694]Implementation of equals for Collections and Objects arrays. By prasuvalli_i { at } yahoo { dot } co { dot } in on 2006/01/05 16:05:28 Rate
public static boolean equals ( Collection c1, Collection c2 ) { boolean result = false; if ( c1.getClass ( ) != c2.getClass ( ) ) return false; Object [ ] o1 = ( Object [ ] ) c1.toArray ( new Object [ c1.size ( ) ] ) ; Object [ ] o2 = ( Object [ ] ) c2.toArray ( new Object [ c2.size ( ) ] ) ; result = equals ( o1, o2 ) ; return result; } public static boolean equals ( Object [ ] c1, Object [ ] c2 ) { if ( c1.length != c2.length ) { // could drop this check for fixed-length keys return false; } if ( c1 == c2 ) return true; if ( null == c1 || null == c2 ) return false; for ( int i = 0, len = c1.length; i < len; i++ ) { // could skip invariants if ( !equals ( c1 [ i ] ,c2 [ i ] ) ) { return false; } } return true; }
protected void finalize()
throws Throwable - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1901]Avoid Finalizers By Anonymous on 2007/07/05 08:24:12 Rate
Finalizers are unpredictable, often dangerous, and generally unnecessary. ... Finalizers have a few valid uses, which we'll cover later in this item, but as a rule of thumb, finalizers should be avoided. Further, System.gc and System.runFinalization may increase the odds of finalizers getting executed, but they don't guarantee it. [1902]A class to make the finalizer thread max priority to make sure stuff gets finalized By Anonymous on 2007/07/05 08:35:34 Rate
I suffered lots of OutOfMemory. What I finally figured out was that all the memory was sitting around waiting to be finalized. I had many threads creating garbage that needed to be finalized ( on a multi-processor server ) , and apparently on a single "low-priority" finalizer thread trying to clean up after the fact. My workaround consists of this code ( which I call each time my servlet is invoked ) : public static class finalizer_max { public void finalize ( ) { Thread.currentThread ( ) .setPriority ( Thread.MAX_PRIORITY ) ; } } private static boolean finalize_fixed = false; private synchronized static void fixFinalize ( ) { if ( !finalize_fixed ) { // Initialize the finalizer thread. We need to bump up the priority // to make sure stuff gets finalized finalizer_fixed = true; new finalizer_max ( ) ; System.gc ( ) ; System.runFinalization ( ) ; } }
public final Class<? extends Object> getClass() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[310]Invoke a method on the Object class By Anonymous on 2003/07/22 06:50:55 Rate
import java.lang.*; import java.lang.reflect.* ; public class try1 { public static void main ( String [ ] args ) throws Exception { try1 t = new try1 ( ) ; t.processActions ( t ) ; } public void processActions ( Object obj ) throws Exception { Method m = obj.getClass ( ) .getMethod ( "systemOut",null ) ; m.invoke ( obj, new Object [ ] { } ) ; } public void systemOut ( ) { System.out.println ( "Hello" ) ; } } [962]Class without instances By Anonymous on 2004/10/01 14:36:02 Rate
There's a getClass ( ) method for getting the Class object of a given object, but this means you have to have an instance of the object first. But what if you just want to compare a Class variable to an existing class ( like instanceof but without having an instance ) or you want the Class object of a static class ( i.e. a class without instances ) ? There is a special language construct for getting the class object of a given class: < classname > .class. So, if we want the class object of, say, System: Class systemclass = System.class; The notation suggests that class is actually a public static field of Object, but this is not so. If you want the class field of an object, use getClass ( ) .
public int hashCode() - See Also:
Hashtable , equals(java.lang.Object)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public final void notify() - See Also:
wait() , notifyAll() , IllegalMonitorStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public final void notifyAll() - See Also:
wait() , notify() , IllegalMonitorStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1402]Difference between notify() and notifyAll() By shamaz on 2005/04/26 11:11:59 Rate
this class shows the difference between notify ( ) and notifyAll ( ) public class Test { public void go ( ) { Test test = new Test ( ) ; ( new Thread1 ( test ) ) .start ( ) ; ( new Thread2 ( test ) ) .start ( ) ; ( new Notifier ( test ) ) .start ( ) ; } class Thread1 extends Thread { Test t; Thread1 ( Test t ) { this.t = t; } public void run ( ) { try { t.wait ( 5000 ) ; } catch ( InterruptedException ie ) { } System.out.println ( "thread 1" ) ; } } class Thread2 extends Thread { Test t; Thread2 ( Test t ) { this.t = t; } public void run ( ) { try { t.wait ( 5000 ) ; } catch ( InterruptedException ie ) { } System.out.println ( "thread 2" ) ; } } class Notifier extends Thread { Test t; Notifier ( Test t ) { this.t = t; } public void run ( ) { synchronized ( t ) { t.notify ( ) ; } } } } If you call the go ( ) method, then only one thread will awake. If you put t.notifyAll ( ) , instead of t.notify ( ) then the 2 threads will awake [1434]wait() methode need a synchronized block By Anonymous on 2005/05/19 04:24:53 Rate
class Test2 { int a =0; public static void main ( String [ ] args ) { new Test2 ( ) .go ( ) ; } public void go ( ) { Test2 test = new Test2 ( ) ; ( new Thread1 ( test ) ) .start ( ) ; ( new Thread2 ( test ) ) .start ( ) ; ( new Notifier ( test ) ) .start ( ) ; } class Thread1 extends Thread { Test2 t; Thread1 ( Test2 t ) { this.t = t; } public void run ( ) { synchronized ( t ) { try { t.wait ( 5000 ) ; t.wait ( 5000 ) ; } catch ( InterruptedException ie ) { } t.a=1; System.out.println ( "thread 1"+t.a ) ; } } } class Thread2 extends Thread { Test2 t; Thread2 ( Test2 t ) { this.t = t; } public void run ( ) { synchronized ( t ) { try { t.wait ( 5000 ) ; } catch ( InterruptedException ie ) { } t.a=2; System.out.println ( "thread 2"+t.a ) ; } } } class Notifier extends Thread { Test2 t; Notifier ( Test2 t ) { synchronized ( t ) { this.t = t; } } public void run ( ) { synchronized ( t ) { t.notifyAll ( ) ; } } } } Hello just to say that wait ( ) methode need a synchronized block.
public Object() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[177]Fine-grained locking By Anonymous on 2003/03/14 08:54:54 Rate
//The explicit block is better than the standard implementation ( synchronizing all methods ) //because both methods can run concurrently, which reduces locking costs and //increases scalability. The caveat is that you can't have two threads invoking //the same method on the same object, which is the consistency we want to enforce. import java.util.Date; public class Person1 { private String name, surname; private Date birth, death; private final Object nameLock = new Object ( ) , dateLock = new Object ( ) ; public void setNames ( String name, String surname ) { synchronized ( nameLock ) { this.name = name; this.surname = surname; } } public void setDates ( Date birth, Date death ) { synchronized ( dateLock ) { this.birth = birth; this.death = death; } } }
public String toString() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1486]Manipulate Array element through reflection By Sreenivas on 2005/07/17 10:00:05 Rate
public final class Tex3 { private boolean flag=false; public void sample ( ) { Inner in1=new Inner ( ) ; System.out.println ( "Sample \t"+in1 ) ; } public static void main ( String a [ ] ) { Tex3 e1=new Tex3 ( ) ; e1.sample ( ) ; } } class Inner { private boolean flag1=false; int i=555; public String toString ( ) { if ( flag1 ) return "true"+i; else return "false"+i; } }
public final void wait()
throws InterruptedException - See Also:
notifyAll() , notify() , IllegalMonitorStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[489]Sync block By Anonymous on 2003/11/04 10:04:17 Rate
java.lang.Object sync = new java.lang.Object ( ) ; synchronized ( sync ) { sync.wait ( ) ; }
public final void wait(long timeout)
throws InterruptedException - See Also:
notifyAll() , notify() , IllegalMonitorStateException, IllegalArgumentException, interrupted
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1542]Difference between sleep and wait By Anonymous on 2005/09/22 15:32:07 Rate
Unlike sleep ( 1000 ) which puts thread aside for exactly one second. wait ( 1000 ) causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify ( ) or notifyAll ( ) call.
public final void wait(long timeout,
int nanos)
throws InterruptedException - See Also:
- IllegalMonitorStateException, IllegalArgumentException,
wait(long) , notifyAll() , notify()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
| Popular Tags |