Skip to main content

Posts

Showing posts from January, 2018

Does an interface extend Object class in java?

This is one of the toughest and trickiest question in java language. You may also know that only classes in java are inherited from java.lang.Object class. Interfaces in java don’t inherit from Object class. They don’t have default parent like classes in java. But, following two cases may surprise you. Case 1 : If an interface does not extend Object class, then why we can call methods of Object class on interface variable like below ? interface A { } class InterfaceTest { public static void main(String[] args) { A a = null; a.equals(null); a.hashCode(); a.toString(); } } Case 2 : If an interface does not extend Object class, then why the methods of Object class are visible in interface.? interface A { @Override public boolean equals(Object obj); @Override public int hashCode(); @Override public String toString(); } Explanation : Here is the answer, for every public method in Object class, there is an implicit abstract and public method declared in every interface which does not have di...

Java Interview Question : What is 'volatile' keyword and what are the use cases?

What is the Java volatile keyword? Daily we are reading about the java its key points, today we are going to get the internals of volatile keyword, volatile is used to indicate that a variable is going to reside in volatile memory only i.e. Main memory aka RAM as we know that when CPU operates on any variable it will copied that object into its registers and it will operate on the register and after completion of its operation it will send the modified value to RAM, so in case of a multi-threaded application it may possible that two thread will operate on same variable but under different CPU registers so two avoid this kind of situation we can use "volatile" keyword with the variable. Declaring a volatile Java variable means: The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory" aka RAM. Now we can say that The Java volatile keyword is used to mark a Java variable as "being stored in main memory...

Java Interview Question : What is 'Intern()' method in String class?

Java String intern The java string intern() method returns the interned string. So in java we have various ways to create a String. 1. By String class constructor. String str = new String ("Hello"); here two objects will get created in the memory one in side the heap on object area and another one inside the string constant pool but str will hold the reference of the string that is created under object area. 2. By literal. Here only one object will get created inside the string constant pool. 3. By toString() Method on any object (String representation of an object). (same as first) Now what if we want the string object from SCP(String constant pool), so to get the string reference from SCP java provides intern method. Method Signature The signature of intern method is given below: public String intern() Lets understand the things by example: package net.jubiliation.example; public class StringInterMethodExample { public static void main(String[] args) { String s1 = new S...

Interview Question : How garbage collector works?

How garbage collector works? Before going to understand the working of garbage collection functionality of Java, I hope you already gone through the previous post i.e. What is garbage collection? if not please go through it.  Let's start how garbage collection works, in general many people think garbage collection collects and discards dead objects. In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage. As you’ll see, this fundamental misunderstanding can lead to many performance problems. B efore moving ahead let's recall few important points about garbage collection in Java. Objects are created on the heap in Java irrespective of their scope e.g. local or member variable. while it's worth noting that class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread. Garbage collection is a mechanism provided by Java Virtual...

Interview Question : What is garbage collection in Java?

What is garbage collection? The definition say that garbage collection is a process that is responsible for making space in a computer's memory by removing data that is no longer required or in use. Java Garbage Collection: In java, garbage means unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects. To do so, we were using free() function in C language and delete() in C++. But, in java it is performed automatically. So, java provides better memory management. Advantage of Garbage Collection It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts. How can an object be unreferenced? 1) By nulling a reference: Employee e=new Employee(); e=null; 2) By assigning a reference to another: Employee e1=new Employee(); Empl...