Posts

Showing posts with the label Java

Topics to Prepare for Java Interview

Companies looking for Java developers who have experience in following topics 1. Core Java 2. Java 8 3. Multithreading (must) 4. Spring 5. Spring Boot (Micro Services) 6. Hibernate 7. XML 8. Concurrency 9. Algorithms 10. Data structures 11. Design patterns 12. Object-Oriented Design 13. Architecture patterns 14. MVC Design 15. RESTful web services 16. Maven 17. MongoDB 18. Git 19. JUnit & Mockito 20. System Design Links to refer: Design patterns https://www.youtube.com/watch?v=EYOKqb2Mf7k&list=PLmCsXDGbJHdjZiLC2HjXrgw8-E4O3MXdN Singleton Implement Singleton using Eager Initialisation Implement Singleton using Lazy Initialisation What is the Double-Checked Locking? When Double-Checked Locking fails? How can you fix Double-Checked Locking fail? Ans: use Volatile Factory  Abstract Factory - Factory of Factories Spring Spring core Spring transactions Spring JDBC Spring MVC CoreJava & Java8 OOP...

Different ways to create an object In Java

Below are the different ways to create an object in java. 1. using new keyword 2. using newInstance() method of class Class 3. using newInstance() method of class Constructor 4. using clone() method 5. using deserialization Using new keyword: 1. Employee emp = new Employee(); Using newInstance() method of class Class: 1. Employee emp2 = Class.forName("Employee").newInstance(); 2. Employee emp3 = Employee.class.newInstance(); Using newInstance() method of class Constructor: 1. Constructor empConstructor = Employee.class.getConstructor();  Employee emp4 = empConstructor.newInstance() Using clone() method: 1. Calling clone() method, actually creates a new object and copies all the contents of old object into it. Creating an object using clone method doesn't invoke any constructor. 2. To use clone() method on an object, we need to implement Cloneable interface and define clone() method in it. Employee emp5 = (Employee) emp4.clone(); Using...

Java: How threads communicate with each other

Threads communicate with each other with the help of 3 Object class methods 1. wait() 2. notify() 3. notifyAll() Lets look at a simple example, Consider we have 2 threads, one is mail delivery thread and another is mail-processing thread. The mail-processing thread has to keep checking to see if there's any mail to process. The mail processing thread checks for any new mail for every 2 seconds. This is truly waste of time & cpu processing power. Instead of that using wait&notify mechanism, when the mail-delivery thread puts a mail in mailbox & notifies mail-processing thread, so that it processes new mail. Key Points: 1. wait(), notify() & notifyAll() must be called from within a synchronized context. A thread cannot invoke a wait or notify method on an object unless it owns that object's lock. 2. If the thread calling wait() does not own the lock, it will throw an IllegalMonitorStateException. This exception is not a checked exception. But a wait...

How Treeset checks for duplicates while inserting?

Treeset uses compareTo() or compare() methods to compare elements.  If the element you want to insert into treeset implements Comparable interface, then Treeset uses compareTo() method to compare with the elements already present in the set.  Treeset doesn't use equals() or hashcode() to compare elements. Some more points about TreeSet: The most fundamental behavior of TreeSet is it doesn't allow duplicate entries as it is also a set.  TreeSet preserves the order of elements in which they are inserted. TreeSet uses binary search to locate an item.  TreeSet is backed by TreeMap.  TreeSet implements NavigableSet If you have not provided Customer comparator and if the user defined element has not implemented Comparable interface, then adding an element throws Runtime error.  

Inner Classes & Nested Classes Java

As per java documentation An inner class is a non static class declared inside another class. A nested class is a static class declared inside another class, Even though it is declared inside another class, it won't be considered as an inner class. Some tricky points to remember: It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable. A constant variable is a final variable of primitive type or type "String" that is initialized with a constant expression.  

How to set proxy settings for JVM

You can configure proxy settings in JVM using 2 VM arguments. Those are Dhttp.proxyPort Dhttp.proxyHost As these are proxy arguments, while running a Java program from command line or from eclipse, you need to provide these arguments to Java. The sample code is given below. "C:\Program Files\Java\jrockit-jdk1.6.0_45-R28.2.7-4.1.0\bin\java" -Dhttp.proxyPort=80 -Dhttp.proxyHost=www-proxy.us.xxxx.com -jar "C:\Program Files\Java\jrockit-jdk1.6.0_45-R28.2.7-4.1.0\missioncontrol\mc.jar" Here i am trying to run mc.jar by setting proxy port and proxy host arguments. If you want to set proxy settings in eclipse, you can refer below screenshot. for running HttpURLConnectionExample, i am setting proxy vm arguments.