• 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 […]

  • 19 May

    Java static import example

    If any class which is not in same package, we need to import it. If we import that class we can directly access static variables and methods with the class name. If you use static import,  you do not need to use class name any more. Lets understand with the help of example Without using […]

  • 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 […]

  • 18 May

    How to remove all white spaces from String in java

    In this post, we will see how to remove all white spaces from String in java. There are multiple ways to remove spaces from String. Using ReplaceAll Using Iteration Using ReplaceAll : You can simply call ReplaceAll method to remove white spaces as shown below. [crayon-661e46e6c561c826763844/] Using iteration : You can iterate over String using […]

  • 17 May

    How to check if one String is rotation of another String in java

    In this post, we will see how to check if one String is rotation of another or not. For example: [crayon-661e46e6c5b26222021529/] Approach: Lets say you need to check whether str1 and str2 is rotation of one another or not. Create a new String with str3= str1 + str1 Check if str3 contains str2 or not. […]

  • 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-661e46e6c5c5f650194485/] 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-661e46e6c5d07600660604/] 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) […]

  • 15 May

    Java String compareToIgnoreCase example

    String’s compareToIgnoreCase method is similar to compareTo method. Only difference with compareTo is that it ignores cases.It also compares Strings lexigraphically. Both String get converted to unicode value and then compares. If you call str1. compareToIgnoreCase(str2) then if it returns positive number : str1 is greater than str2 0: str1 is equal to str2 negative number : str1 is […]

  • 14 May

    Java String equalsIgnoreCase example

    String’s equalsIgnoreCase method is similar to equals method but it ignores case. So if Strings have different cases, then also it will considered as equals. For example: [crayon-661e46e6c625f819669570/] str1.equalsIgnoreCase(str2) will return true. Method Signature: [crayon-661e46e6c6262380730894/] String equalsIgnoreCase Example: [crayon-661e46e6c6263473197063/] When you run above program, you will get below output: [crayon-661e46e6c6264700316742/]