Posts

Showing posts from 2012

How class.forName loads the Database Driver in JDBC

When Class.forName() is called with an argument like this: Class.forName("org.gjt.mm.mysql.Driver"); the classloader attempts to load and link the Driver class in the “org.gjt.mm.mysql” package and if successful, the static initializer is run. The MySQL Driver static initializer looks like this: static {   try {     java.sql.DriverManager.registerDriver(new Driver());   } catch (SQLException E) {     throw new RuntimeException("Can't register driver!");   } } So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.   To understand the why you have to look at the next line in the initial code example: Connection con = DriverManager.getConnection(url,   "myLogin", "myPassword"); The DriverManager class returns a database connection given a JDBC URL string, a username and a password. In order to create that connection, the DriverManager class has to know

Problems on Bit Manipulations

Check if two numbers have opposite sign We can use the operation if    (x ^ y) < 0     where x and y are non zero numbers {  True   --- if numbers of opposite sign {  False  --- if numbers of same sign int   x=-10,y=2; System.out .println(((x ^ y) < 0)); Output :- False Check if number is power of 2 We can use    x & (x - 1)) == 0     True   --- if numbers is a power of 2                                  False  --- if numbers is not a power of 2 int  x=16; System.out.println((x & (x - 1))==0); Output :- True More bitwise tricks x&(x-1)           :---------------- Returns number x with the lowest bit set off x ^ ( x & (x-1 )   :----------------  Returns the lowest bit of a number x x & 1<<n           :----------------   Returns 1<<n if the n-th bit is set in x x | 1<<n           :----------------   Returns the number x with the n-th bit set x ^ 1<<n           :----------------   Toggles the state

Immutability in java

In Java, Object can be of two types :- Mutable Immutable Mutable objects are the ones whose state can be changes during its lifetime. This is in contrast with Immutable objects , whose state cannot be changed once created. We have many classes in Java which provide immutable objects like String, Integer etc. In this post, I will discuss the immutability in detail, with its features and where can we use this. In basic terms, Immutable objects are the ones, whose state cannot be changed.e.g Integer i = new Integer(10) ; The Integer class provides us no method which can change the state/value of i. So we say Integer is an immutable class & object i is an immutable object. We have many benefits for it: - They are good candidates to cache :- Since immutable objects do not change their state, we can share or cache them and retrieve them in future, without copying and cloning the object. Solid keys :- Immutable objects makes the best keys for Hash classes. HashCode()

Generate Heap Dump in Unix

Image
Heap dump provides the insight into the JVM - shows live objects on the heap (reachable and unreachable) along with their types and values. In addition it also provides no of objects and their size as %age of total heap size.We have many ways to get the heap dump in Windows. Some are as follows:- jconsole jVisual VM Eclipse MAT ....and it goes on Out of above , i like jVisualVM the most due to its monitoring capability, interactive display, and easy to use. But the problem comes with the unix. Even though we can monitor the application remotely  but we cannot see the live objects, their size and number i.e we cannot get the heap dump remotely. To get the heap dump on Unix machine, we can use j map.  To use jmap , we need the process id of java application. At times, we have lot many java process running in unix and its difficult to identify. Java provides jps tool which list the JVM with the process Id which it got the access . The steps are as follows:- Type jps on un

Why Time gets truncated with ResutSet getDate() function.

Recently I stuck in an issue on populating date from stored procedure. I am reading a date from a resultset using rs.getDate(), storing in a java.util.Date instance variable and displaying on the screen. Java.util.Date  mydate = rs.getDate(1); Output is only date while time is truncated.  But why …! That confused me a lot. So I google it. Spending lot of time in reading blogs, oracle docs and trying different things on oracle server like checking  NLS_DATE_FORMAT, I am not able to get the time. Next I come across a Timestamp thing in Resultset class. Rs.getTimestamp (int colIndex) When I use it, I immediately get the time.  Now, the important question, why the resultset function getDate() does not return the Time. Now my search is preety narrow. What I found is :- In java, we have java.util.Date for java.sq.Date . So functionalities available in sql.Date are also available in util.Date. But there is one difference, sql.Date stores only Date and not Time of date. To get the