Posts

Showing posts with the label Core Java

public vs protected vs default access modifiers - Java

| Class | Package | Subclass | Subclass | World | | |(same pkg)|(diff pkg)| ————————————+———————+—————————+——————————+——————————+———————— public | + | + | + | + | + ————————————+———————+—————————+——————————+——————————+———————— protected | + | + | + | + | ————————————+———————+—————————+——————————+——————————+———————— no modifier | + | + | + | | ————————————+———————+—————————+——————————+——————————+———————— private | + | | | | + : accessible blank : not accessible

Java - Thread join

In this code, what does the two joins and break mean?  t1.join()  causes  t2  to stop until  t1 terminates? Thread t1 = new Thread ( new EventThread ( "e1" )); t1 . start (); Thread t2 = new Thread ( new EventThread ( "e2" )); t2 . start (); while ( true ) { try { t1 . join (); t2 . join (); break ; } catch ( InterruptedException e ) { e . printStackTrace (); } } There is a thread that is running your example code which is probably the  main thread . The main thread creates and starts the  t1  and  t2  threads. The two threads start running in parallel. The main thread calls  t1.join()  to wait for the  t1  thread to finish. The  t1  thread completes and the  t1.join()  method returns in the main thread. The main thread calls  t2.join()  to wait for the  t2  thread to finish. The  t2  thread complet...

Garbage collection question - Java

class A { Boolean b ; A easyMethod ( A a ){ a = null ; // the reference to a2 was passed in, but is set to null // a2 is not set to null - this copy of a reference is! return a ; // null is returned } public static void main ( String [] args ){ A a1 = new A (); // 1 obj A a2 = new A (); // 2 obj A a3 = new A (); // 3 obj a3 = a1 . easyMethod ( a2 ); // a3 set to null and flagged for GC - see above for why a1 = null ; // so far, a1 and a3 have been set to null and flagged // Some other code } } Two objects are eligible for garbage collection (a1 and a3).  b  is not because it's only a reference to null. No  Boolean  was ever made. To get around the inane subtleties of what  // Some other code  might be, I instead posit the question be reworded into the following: Prdict and explain the following output: class A...

Class, Reference and Object

If you like housing metaphors a  class  is like the blueprint for a house. Using this blueprint, you can build as many houses as you like. each house you build (or instantiate, in OO lingo) is an  object , also known as an  instance . each house also has an address, of course. If you want to tell someone where the house is, you give them a card with the address written on it. That card is the object's  reference . If you want to visit the house, you look at the address written on the card. This is called  dereferencing . You can copy that reference as much as you like, but there's just one house -- you're just copying the card that has the address on it, not the house itself. Java methods are always pass-by-value, but the value could be an object's reference. So, if I have: Foo myFoo = new Foo (); // 1 callBar ( myFoo ); // 2 myFoo . doSomething () // 4 void callBar ( Foo foo ) { foo = new Foo (); ...

Adding "implements serializable" effects - Java

The cost is close to zero, not worth being concerned about. Some further details: There is no increase in the size of each object instance There is a small increase in the size of the class itself, but as this is a one-off cost it is trivial when amortised over a large number of instances There may be a slight additional runtime cost for anything that needs to do interface checks at runtime (reflection, instancof lookups, extra pressure on inline caches etc.). Again, this is likely to be negligible for most purposes. Serializable is a  marker interface , there are no methods that require to be implemented. Other marker interface examples are: Clonable, SingleThreadModel, Event listener. ------------------------------------------------------------------------------------- There is no performance impact unless you perform serialization/deserialization but there are trade offs in terms of api design. From  Effective java  by Joshua Bloch A major cost of...

retainAll method in Java

retainAll()  method of  Set : Set < String > s1 ; Set < String > s2 ; s1 . retainAll ( s2 ); // s1 now contains only elements in both sets If you want to preserve the sets, create a  new  set to hold the intersection: Set < String > intersection = new HashSet < String >( s1 ); // use the copy constructor intersection . retainAll ( s2 ); The  javadoc  of  retainAll()  says it's exactly what you want: Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set,  this operation effectively modifies this set so that its value is the  intersection  of the two sets. Ref: Stackoverflow 

Thread local variable - Java

The  ThreadLocal  class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal  variable, then the two threads cannot see each other's  ThreadLocal  variables. Creating a ThreadLocal Here is a code example that shows how to create a  ThreadLocal  variable: private ThreadLocal myThreadLocal = new ThreadLocal(); As you can see, you instantiate a new  ThreadLocal  object. This only needs to be done once per thread. Even if different threads execute the same code which accesses a  ThreadLococal , each thread will see only its own  ThreadLocal  instance. Even if two different threads set different values on the same ThreadLocal  object, they cannot see each other's values. Accessing a ThreadLocal Once a  ThreadLocal  has been created you can set the value to be stored...

Override equals and hashcode in Java

It is not always necessary to override hashcode and equals. But if you think you need to override one, then you need to override both of them. Let's analyze what whould happen if we override one but not the other and we attempt to use a  Map . Say we have a class like this and that two objects of  MyClass  are equal if their  importantField is equal (with  hashCode  and  equals  generated by eclipse) public class MyClass { private final String importantField ; private final String anotherField ; public MyClass ( final String equalField , final String anotherField ) { this . importantField = equalField ; this . anotherField = anotherField ; } public String getEqualField () { return importantField ; } public String getAnotherField () { return anotherField ; } @Override public int hashCode () { final int prime = 31 ...