Sunday, 19 March 2017

Where is the object stored in java?

Whenever an object is created in java, it gets storage space in heap.

Heap is created when JVM starts. And it increases or decreases in size as per the requirement while the application runs.

At times when heap space becomes full or approaches to be full, the garbage collection starts.

Heap can be divided into two generations:

  • nursery
  • old space
To learn more, about nursery and old space follow this link



You may also want to know about:

Saturday, 18 March 2017

Today's Java Quiz

This quiz comprises of very basic level of java concept questions. See how much you score out of 10. 

Note : Answers to each question is given at the end.

1. What is base class for Exception?
  • inner class
  • Throwable
  • Error
  • run time exception

2. Which is related to garbage collection in java?
  • Final
  • Finally
  • Finalize
  • Try/Catch

3. String s = new String("Radar");
   How many objects are created in this case?
  • one
  • two
  • three
  • four

4. ArrayList is a
  • interface
  • Object
  • Class
  • none

5. Which of the following is synchronized?
  • Vector
  • ArrayList
  • HashSet

6. for(;;) will result in
  • 1
  • run time exception
  • infinite loop
  • compile time exception

7. A .class file in java consists of
  • plain code
  • class definition
  • group of objects
  • bytecode

8. Which one is a run time polymorphism?
  • method overloading
  • method overriding
  • inheritence
  • encapsulation

9. The wrapping up of data & function into a single unit(class) is called
  • abstraction
  • polymorphism
  • serialization
  • encapsulation

10. IOException is an example of:
  • runtime exception
  • compile time exception
  • error
  • none

Answers: 

1-Throwable    2-Finalize    3-two   4-class   5-vector   6- infinite loop   7-bytecode

8-method overriding   9-encapsulation   10-compile time exception


                                   Click here for more Quiz 

What will be execution process for this statement in java -- > for(;;){ //your code}

In a recent interview that my friend attended, he was given below code by interviewer

for(;;){ 
//your code


And then interviewer asked him to explain how this for loop will execute. He discussed this question with me as he failed to answer it. I am aware of this answer as luckily same question was asked by my college teacher during viva.

But any way if you don't know it lets explore its execution process now.

First lets understand basic structure of for loop in java. Lets consider given example,

for(int i=0; i<5;i++){
   System.Out.Print("Executed");
}

How above for loop works?

For the very first time when code enters the for loop, then initialization statement i=0 is executed once.
Then conditional statement i<5 is executed. If it executes to be true then body inside the for loop is executed. Otherwise loop gets terminated.
In our case it executes to be true as i = 0, so our code will enter inside loop and print : Executed
Now incremental statement i++ is executed and i increases by 1 (i.e i=1)
Again conditional statement is executed and 1<5 is true so body inside loop is executed.
Now incremental statement i++ is executed and i increases by 1 (i.e i=2)
Again conditional statement is executed.
This process follows till the conditional statement turns out to be false(i.e i=5)

Now consider,
for(;;)

Try to compare this with the code explained above.
Take it as,
for(   ;   ;   ) {
 // your code
}

In this case when code enters for loop first time then as there is no initialization statement so nothing is executed.
Next we move on to conditional statement which is also empty. Remember, that empty statement here will execute to be true. So body inside for will get executed.
Now moving on to incremental statement, we see it is also empty so nothing gets executed.
Then conditional statement is executed again which will evaluate to true. So body inside for will get executed again.
This process will follow till eternity.

Conclusion Drawn :

for(   ;   ;   ) or for(;;)  will enter into infinite loop.


Reason : No initialization statement, its conditional statement always evaluates to be true and this statement has no increment/decrement value



You may also like to read:

Friday, 17 March 2017

LinkedList class


LinkedList class belongs to java.util package.
It implements List, DequeCloneable and Serializable interface. LinkedList is the doubly-LinkedList implementation of the List and Deque interface.


Key feature of LinkedList:

1.      It is non-synchronized.
2.      LinkedList is preferred over ArrayList if operations like insertion or deletion needs to be done in bulk.
3.      LinkedList allows addition of null element.

Difference      

Now let’s look at the ways in which you can create LinkedList in java. Please click on each to learn the creation technique.

·         LinkedList<E> myList = new LinkedList <E>();   //<E> denotes Generics usage
·         LinkedList<E> myList = new LinkedList <E>(Collection c);

Simple Example:

package javaRadarLinkedList;

import java.util.LinkedList;

public class SimpleLinkedList {

      public static void main(String[] args) {
            //create LinkedList
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }


}


Now let’s look at different ways in which you can use the LinkedList that you created.

LinkedList operations

·         Search element in LinkedList

Traverse LinkedList


Convert

·         LinkedList to ArrayList


ArrayList or LinkedList. Which one to use?(Click to see the difference between two and decide)

Thursday, 16 March 2017

A program on split() method in java

Splitting a String is the most day to day required activity for a programmer. You must have a good grasp of the process of splitting a String.

We have two widely used possible ways of splitting a String in java.

  • split(), &
  • StringTokenizer()
Here we are going to show you example of the most preferred of the two methods. 

String : Learning|Java|Is|Fun
Delimmiter: |  

public class SplitString {

       public static void main(String[] args) {

              String s1 = "Learning|Java|Is|Fun";
             
              //String[] s2 = s1.split("\\|");
              String[] s2 = s1.split(Pattern.quote("|")); //you need to                                                                             importjava.util.regex.Pattern
              int i = 0;

              while (i < s2.length) {

                     System.out.println(s2[i]);
                     i++;

              }
             
       }
}


You may also want to know about:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.