• 19 May

    Java transient keyword with example

    Transient variable is the variable whose value is not serialized during serialization. You will get default value for these variable when you deserialize it. Lets say you have Country class and you don’t want to Serialize population attribute as it will change with time, so you can declare population attribute as transient and it won’t […]

  • 18 May

    ArrayList in java

    ArrayList in java is most common Collections data structure along with HashMap which we use very often. 1. Why to choose ArrayList vs Array: Array is fixed length data structure If array is full , you can not add element to it, where as ArrayList in java can dynamically grow and shrink as per our […]

  • 17 May

    Can we call run() method directly to start a new thread

    No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won’t create a new thread and it will be in same stack as main. Lets understand with the help of example: [crayon-6633a76b39072176304457/] When you […]

  • 16 May

    Can we start a thread twice in java

    No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException Lets understand with the help of example: [crayon-6633a76b39132909835902/] When you run above program , you will get below output: Thread is runningException in thread “main” java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:705) at org.arpit.java2blog.StartThreadAgainMain.main(StartThreadAgainMain.java:16) […]

  • 07 May

    Why String is immutable in java

    String class is immutable in java. If you take dictionary meaning of immutable, it means unable to be changed or unchanging over time, so String is unchangeable or unmodifiable in java. Let’s understand with example. [crayon-6633a76b391b4855438541/] As you can see, value of str1 did not change. It created another String object with value “Hellojava2blog” but […]

  • 29 April

    HashMap in java

    HashMap is hash table based implementation of Map interface. It stores entry in key-value pairs. It maps keys to values. It is one of the most used Collection. Java HashMap HashMap implements Map an interface that maps a key to value. It is not synchronized and is not thread-safe. Duplicate keys are not allowed One […]

  • 29 April

    TreeMap in java with examples

    TreeMap class implements Map similar to HashMap. Some important points about TreeMap: TreeMap implements Map interface and extends HashMap class. TreeMap is implemented using Red black tree based NavigableMap. TreeMap is ordered collection and store its elements in natural ordering of keys. Key which you would like to put in TreeMap must implement Comaparable interface or you can […]

  • 12 September

    Difference between Iterator and ListIterator in java

    In this post, we will see difference between Iterator and ListIterator in java. They are both used for traversal but it is good to point out differences between them. Iterator vs ListIterator: Parameter Iterator ListIterator Traversal Iterator can be used to traverse List,set, Queue ListIterator can be used to traverse only List. Traversal direction Iterator can […]

  • 02 August

    How to find length of string in java without using length() method

    One of the interview question is “How to find length of string in java without using length() method.” There are many ways to find length of String.  Some of them are : Using toCharArray is simplest solution. Using toCharArray Logic Convert string to char array using toCharArray method Iterate over char array and incrementing length […]