<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ritesh goel on Medium]]></title>
        <description><![CDATA[Stories by Ritesh goel on Medium]]></description>
        <link>https://medium.com/@riteshgoel_?source=rss-eba9f53af5bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HeIwIUDBm3Z2nZWz.</url>
            <title>Stories by Ritesh goel on Medium</title>
            <link>https://medium.com/@riteshgoel_?source=rss-eba9f53af5bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 20:11:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@riteshgoel_/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java Interview Questions]]></title>
            <link>https://medium.com/@riteshgoel_/java-interview-questions-48777ef58b3b?source=rss-eba9f53af5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/48777ef58b3b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-interview-questions]]></category>
            <category><![CDATA[garbage-collection]]></category>
            <dc:creator><![CDATA[Ritesh goel]]></dc:creator>
            <pubDate>Fri, 24 Apr 2020 05:30:14 GMT</pubDate>
            <atom:updated>2020-05-02T06:07:00.247Z</atom:updated>
            <content:encoded><![CDATA[<h3>Senior Java Developer Interview Questions</h3><p>Some important java interview question for interviews and good to know concepts in Java. Best way to approach this article would be to first, try to answer the question yourself, then jump to solution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iTyN0aGEXHffoJ4c7s1Dtg.png" /></figure><p><strong>Q1. What is the use of ‘<em>volatile</em>’ keyword in java?</strong></p><p>Essentially, volatile keyword in java indicates that the variable will be modified by multiple threads, telling the compiler not to cache the value of that variable locally in each thread. All the reads/writes will directly go to main memory.</p><p>There is a performance downside of using volatile, since all the operations are being done on main memory.</p><p>Detail reading: <a href="http://tutorials.jenkov.com/java-concurrency/volatile.html">http://tutorials.jenkov.com/java-concurrency/volatile.html</a></p><p><strong>Q2. Why can’t java collections have primitive types?</strong></p><p>It was a Java design decision, and one that some consider a mistake. Containers want Objects and primitives don’t derive from Object. In Java, it’s because of the way generics work. In Java, generics are a compile-time trick, that prevents you from putting an Image object into an ArrayList&lt;String&gt;. However, Java&#39;s generics are implemented with type erasure: the generic type information is lost during run-time. This was for compatibility reasons, because generics were added fairly late in Java&#39;s life. This means that, run-time, an ArrayList&lt;String&gt; is effectively an ArrayList&lt;Object&gt; (or better: just ArrayList that expects and returns Object in all of its methods) that automatically casts to String when you retrieve a value.</p><p>But since int doesn&#39;t derive from Object, you can&#39;t put it in an ArrayList that expects (at runtime) Object and you can&#39;t cast an Object to int either. This means that the primitive int must be wrapped into a type that does inherit from Object, like Integer.</p><p>For data structures like Map, which relies on hashcode() method of an object to find the key for that object will not be present in case of primitive types like int since they are don’t inherit from Object class.</p><p><strong>Q3. How does the garbage collector work in Java?</strong></p><p>Java has an attractive feature that enables developers to not explicitly manage the life cycle of objects. Objects are created when needed and automatically frees the objects when no longer in use. Garbage collections works in 3 simple steps:</p><ol><li><strong>Mark</strong>: Marks the memory spaces as eligible for gc, i.e. which objects are in use and which are not</li><li><strong>Sweep</strong>: This step removes the objects marked in mark phase</li><li><strong>Compaction</strong>: After sweep phase, the memory may become fragmented, it’s inefficient to keep track of these free spaces scattered around the memory, so at some point in time, this fragmented memory is compacted.</li></ol><p><strong>Q4. How is heap memory structured in Java?</strong></p><p>All Garbage collectors in Java, split the heap memory into 2 parts called generations:</p><ol><li>Old(Tenured) generation</li><li>Young generation (further divided into eden and survivor space)</li></ol><p>There are many short lived objects created, let’s say during a loop, we store some intermediate data into an immutable object like String or BigDecimal.</p><p>It works as follows:</p><ol><li>The objects are first allocated to young generation(eden space precisely) which is a small subset of entire heap.</li><li>When the young generation fills up, GC kicks in and stops all application threads and empty out the young generation. Objects that are no longer in use are discarded, and the objects still in use are moved to either survivor space or old generation, this is known as minor GC.</li><li>If an object stays long enough in survivor space, it’s moved to old gen.</li><li>Eventually, when old generation fills up, the JVM will need to find and clear the objects in it. This is where GC algorithms have big differences.</li></ol><p><strong>Q5. What are the different Garbage collection algorithms in Java?</strong></p><p>There are 4 types of GC in Java.</p><ol><li>Serial GC: Uses a single thread to process the heap, it’ll stop the application threads and compacts the old generation during this operation.</li><li>Throughput GC: Uses multiple threads, which makes the process much faster. It’s the default GC used. This also stops application threads during the process and can cause long pauses.</li><li>CMS GC: Application threads are stopped by CMS GC’s multiple threads to collect young generation. In case of old gen, instead of stopping the application threads in full GC, CMS uses one or more background thread to periodically scan old gen and discard unused objects. Young GC collection is also performed during this short duration only. Trade off here is high CPU usage. Also the background threads do not perform any compaction. It needs to be done separately when JVM is unable to allocate sufficient memory in heap.</li><li>G1 GC(used for large heaps &gt; 4gb): This GC divides the heap into chunks, each chunk either belongs to old gen or young gen. Moving of live objects still happens using multiple threads and stopping application threads. The old gen is processed by background threads and moving the unused object from one segment to a designated segment enabling partial compaction. It is less likely for full gc to occur in this approach.</li></ol><p><strong>Q6. What is <em>try-with-resource</em> in java and how is it different from <em>try-catch-finally</em>?</strong></p><p>try-with-resource is a exception handling mechanism that automatically closes the resources when you are done with them.</p><pre>try(FileInputStream input = new FileInputStream(&quot;file.txt&quot;)) {<br>    int data = input.read();<br>    while(data != -1){<br>        System.out.print((char) data);<br>        data = input.read();<br>    }<br>}</pre><p>It’s not just syntactic sugar, so where does it differ from <em>try-catch-finally</em>?</p><p>In case an exception occurs in try block, now try-with-resources will try to close the resource before propagating the exception, let’s say exception occurs while closing the resource too, here the first exception will be propagated up. This is opposite in case of try-catch-finally, in this case, the last exception that occurs will be propagated up.</p><p><strong>Q7. Difference between Abstraction and Encapsulation?</strong></p><ol><li>Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding).</li><li>While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing the interface to the user and hiding the details of implementation.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48777ef58b3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>