Core java
- 24 May
Variable arguments or varargs methods in java
Java variable arguments was introduced in java 5 and it allows methods to take as many number of arguments you want. Syntax: We use three dots (…) also known as ellipsis to define variable arguments. [crayon-6743dc74029f9081273034/] There are some points which you need to keep in mind while using varargs methods: You can have only […]
- 24 May
Java String compareTo example
String’s compareTo method compares two String lexicographically. Both String get converted to unicode value and then compares. If you call str1.compareTo(str2) then if it returns positive number : str1 is greater than str2 0: str1 is equal to str2 negative number : str1 is smaller than str2 Java String compareTo example [crayon-6743dc7404224262848898/] When you run […]
- 24 May
How to convert String to Byte array in java
Sometimes, we need to convert String to byte array. We can use getBytes() method of String to do it, Method syntax: [crayon-6743dc7404319190884504/] String getBytes Example: [crayon-6743dc740431c553039691/] When you run above program, you will get below output: [crayon-6743dc740431d407979675/] There is one more overloaded method for getBytes Method syntax: [crayon-6743dc740431f937282451/] It encodes String to specified charset format. […]
- 22 May
Difference between PATH and CLASSPATH in java
In this post , we will see differences between PATH and CLASSPATH in java. Let me provide simple definition about PATH and CLASSPATH. PATH : This is environment variable which operating system uses to locate executable such as javac, java, javah,jar etc. For example: bin directory of jdk has all the executable such as javac,java […]
- 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-6743dc7405a91952851015/] 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-6743dc7405bc0180200823/] 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. […]