Posts

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 (); ...

Hibernate 'hql' does not support UNION - Hibernate

So in this case run each query individually using 'HQL' and then add the results to a HashSet

Criteria - Hibernate

In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements. Programmatic behavior offers compile-time syntax checking; as a result, most error messages are delivered during compilation. Although it's convenient, this does not provide any performance enhancing ability over HQL or native SQL queries. In fact, it's a preference given to developers to choose according to taste and necessity. You may use all three or stick to one. Let's see if we can find some examples to appreciate the Criteria Query API. Getting Started The  org.hibernate.Criteria  interface defines several methods for creating query objects programmatically. We generally use a  Session  object to call the  createCriteria()  method and crea...

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...

J2EE Vs Spring

Java EE is an standard, official, specification for a full featured Enterprise Application Framework stack. Includes stuff like Object-Relational Mapping, Security, Web Applications, database connectivity, transactions... On top of Java EE specifications there are JavaEE  implementations/application servers  like: JBoss, Glassfish, WebSphere, Weblogic. Spring on the other hand, is a framework doing lots of the stuff on the Java EE specifications, but in its own form. They don't follow Java EE specifications and APIs for that. But they do include a Web Framework, transaction management, security and several other solutions Java EE offers. ------------------------------------------------------ Java EE: Java EE industry approved standard API based framework IT is predominantly based on annotation and CDI JFC MVC framework for web development JPA implementation to process DB operation JTA API with implementation EJB container and POJO based implementation Ora...

Implicit Cursor vs Explicit Cursor - Oracle DB

A cursor can be explicit or implicit, and either type can be used in a FOR loop.  Why use an explicit cursor FOR loop over an implicit cursor FOR loop? Use an explicit cursor FOR loop when the query will be reused, otherwise an implicit cursor is preferred. Why use a loop with a FETCH rather than a FOR loop that doesn’t have an explicit FETCH? Use a FETCH inside a loop when you need to bulk collect or when you need dynamic SQL. Here is some useful information from the documentation. Example of Implicit Cursor FOR LOOP BEGIN FOR vItems IN ( SELECT last_name FROM employees WHERE manager_id > 120 ORDER BY last_name ) LOOP DBMS_OUTPUT . PUT_LINE ( 'Name = ' || vItems . last_name ); END LOOP ; END ; / Example of Explicit Cursor FOR LOOP DECLARE CURSOR c1 IS SELECT last_name FROM employees WHERE manager_id > 120 ORDER BY last_name ; BEGIN FOR vItems I...