String Constant Pool in Java

What is String Constant Pool in Java? It is a special memory area in Heap that contains references of string objects. There are two ways to create string object in Java. 1. Using string literal Example: String s = “java”; A string written inside double quotes is called string literal. Whenever we create string literal the JVM checks string constant pool. If the string is already present in pool then its reference is fetched. If the string is not present

Read more

Difference between String and StringBuffer in Java

Difference between String and StringBuffer or String vs StringBuffer is a very commonly asked java interview question. Both String and StringBuffer classes are used to store and manage sequence of characters but still there are some differences between them that I have explained in this tutorial. Difference between String and StringBuffer in Java Mutability String class is immutable. It means once an object is assigned a value it can’t be changed. Example:

In above case total two objects are

Read more

How to Encode or Decode URL Parameters in Java

Here you will learn how to encode or decode url parameters in java. When we submit a form by using GET method then the parameters are automatically encoded and appended to the url. In situation when we have to send data using url without submitting form then we have to append the parameters to url. The url may look like this. http://www.example.com/demo?parameter=value In this case the parameter value contains special characters like space, comma, etc. So we have to encode

Read more

4 Ways to Reverse String in Java

In this tutorial you will learn about different ways to reverse string in java with examples.   For example: Input: Hello World Output: dlroW olleH Ways to Reverse String in Java Method 1: This is the simplest method in which we iterator through given string from end character by character and append to new empty string.

  Output Original String: I love programming Reversed String: gnimmargorp evol I   Method 2: It is also an iterative method in which

Read more

2 Ways to Convert String to Character Array in Java

Here you will learn about different ways to convert string to character array in java. 1. Using String.toCharArray() Method We can easily convert string to character array using String.toCharArray() method. It can be done in following way.

  Output I   L o v e   J a v a    2. Writing Own Logic We can also writing our own logic. Each character of string is extracted using String.charAt() method and inserted into the character array.

  Please

Read more

4 Ways to Check String is Anagram in Java

In this tutorial I will tell you the four different ways to check string is anagram in Java or not. Two strings are anagram if they contains same characters in different order. For example word and odwr are anagrams.     Ways to Check String is Anagram in Java Method 1 In this method we sort the strings using Arrays.sort() method and then compare them using Arrays.equals() method. If strings are equal then they are anagram.

  Output Enter first

Read more

How to Split String in Java with Example

Here you will learn how to split string in java. We can split string in Java using split() method of java.lang.String class. It takes a regular expression as an argument and returns an array of strings. Syntax:

  Split String in Java Example Below program will split the string by space.

  Output I Love Java Programming   There are few special characters like ., ?, |, +, etc. and some escape characters like \n, \t, \b, etc.

Read more

Convert Image to Base64 String or Base64 String to Image in Java

In this tutorial you will learn how to convert or encode image to Base64 string and convert or decode Base64 string to image in Java.   What is Base64? Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.     Why we need to convert image to Base64 string? Let’s consider some real life scenarios. What we will do if we want to store

Read more