Posts

Showing posts with the label java tips

How to attach source in eclipse for Jars, debugging and code look-up � JDK Example

Attaching source of any Jar in Eclipse e.g. JDK or open source libraries like Spring framework is a good idea because it help during debugging and code development. As a Java programmer at least you should attach the source of JDK in Eclipse IDE to find out more about JDK classes. Though Eclipse IDE is pretty good on code assist, sometimes You want to know what's going inside a library or a JDK method, rather than just reading the documentation. Interview questions like How HashMap works in Java or How substring cause memory leak can only be answered if you are familiar with the source code of these classes. Once you attach source code of Java or Spring in Eclipse IDE , You can check code with just a single click. Some one may argue for Java decompiler like JAD which can create source from the .class file which is also a smart way to look code for open source library, but decompiled source file is not same as the original source file, as you lost comment and readability. As per...

10 Tips to override toString() method in Java - ToStringBuilder Netbeans Eclipse

Java toString method toString method in Java is used to provide clear and concise information about Object in human readable format. A correctly overridden toString method can help in logging and debugging of Java program by providing valuable and meaningful information. Since toString() is defined in java.lang.Object class and its default implementation don't provide much information, it's always a best practice to override the toString method in sub class. In fact, if you are creating value class or domain class e.g. Order , Trade or Employee , always override equals , hashCode , compareTo and toString method in Java. By default toString implementation produces output in the form package.class@hashCode e.g. for our toString() example, Country class� toString() method will print test.Country@18e2b22 where 18e2b22 is hashCode of an object in hex format, if you call hashCode method it will return 26094370, which is decimal equivalent of 18e2b22. This infor...