Skip to main content

Posts

Showing posts with the label CORE JAVA

How to determine whether an array contains a particular value in Java?

Java Array : Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. So to determine or to check whether a value is contained by the array or not, we have so many solutions. You can use ArrayUtils.contains from Apache Commons Lang public static boolean contains(Object[] array, Object objectToFind) Note that this method returns false if the passed array is null. There are also methods available for primitive arrays of all kinds. But after Java 8 introduction of stream api's and enhacements of java array its become easy to deal such situations. Since java-8 you can now use Streams. String[] values = {"AB","BC","CD","AE"}; boolean contains = Arrays.stream(values).anyMatch("s"::equals); To check whether an array of int, double or l...

What are heap memory generation in Java?

This is a very frequently asked question in Java Tuning. Other related question are like  What is the young generation? What is the old generation? What is the permanent generation? How does the three generations interact/relate to each other? For the HotSpot Java VM, the memory pools for serial garbage collection are the following. Eden Space (heap): The pool from which memory is initially allocated for most objects. Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space. Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space. Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used f...

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

Core Java Interview Questions

How does serialization takes place in Java ? What is purpose of externalizable interface ? What are transient variables ? Why most of methods in Collections class declared as static ? What is difference between overloading and overriding ? What is hiding of members and methods - both static and instance one ? What is ThreadLocal class used for ? What is purpose of Classloader provided by Java ? What is effect of multiple classloaders on Singleton Class ? NoClassDefFoundError vs ClassNotFoundException ? What kind of exceptions we have in Java? What is run time and unchecked exceptions? How we can achieve exception free code? What we need to use among these Exception vs RuntimeException when we are creating custom exceptions? What we have to do when exception raise and when it will rise in case of checked? Hash set internal implementation? Array list how it works internally? How hash set works and what is the difference between hash map and hash set? Array list vs linked list? Comparable...

Java : ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException is an exception that occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath. NoClassDefFoundError is an error that occurs when a particular class is present at compile time, but was missing at run time. The difference from the Java API Specifications is as follows. For ClassNotFoundException : Thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader. The loadClass method in class ClassLoader. but no definition for the class with the specified name could be found. For NoClassDefFoundError : Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-fo...