In many interviews we may asked to substring() a string without using substring function from String class. Below are the simple code how we can write our own substring functions similar to String class functions.

We may seen lot of arrays like Integer, Character, Float and Double or even String array. In this tutorial we will see about converting Character array to String in Java code. We can achieve this by 2 ways like given below,

publicclassCharToString{publicstaticvoidmain(String[] args){char[] myArray =newchar[]{'j','a','v','a',' ','d','i','s','c','o','v','e','r'};//By 2 ways we can convert char[] to String and lets see both ways

This is one of the simple interview question that need to calculate String length without using length() function. There are lot of ways to find the find the length like converting String to Character array and by iterating it. Lets see simple java code to calculate string length.

We all know that String class is a immutable and values once stored will not be changed. So what is String replace and how its working in java? basically whenever we change are replace the value in String new String Object will be created.And also have to know that every-time it won't create String Object, only when string change happen in string value then it will create new String Object otherwise it will return same String Object. Totally there are 4 types of replace methods are there in Java,public String replace(char oldChar, char newChar)- Used to replace matching character in a String and returns the new String Object. public String replace(CharSequence target, CharSequence replacement)- Used to replace set of character sequence in a String and returns the new String Object. public String replaceAll(String regex, String replacement)- Used to replace all matching pattern in String using regular expression or either simple string and returns the new String Object. public String replaceFirst(String regex, String replacement)- This method is simplar to above method but only used to replace first matching pattern in String using regular expression or either simple string and returns the new String Object.Important:

As we seen above if there is not actual replace then these all methods will return same String Object instead of new String Object.

All these methods are Case Sensitive and will replace only with matching case characters.

First lets see simple example for calling replace method and there's no actual replace, so we need to get same String Object.

publicclassStringReplaceTest{publicstaticvoidmain(String[] args){
String source ="Hello Java";
String str2 = source.replace("hi","hello");//By using == operator we can test is it same Objectif(source == str2){
System.out.println("First Compare - Both are same String Object");}else{
System.out.println("First Compare - Both are NOT same String Object");}/************************************************/
String str3 = source.replace("Hello","Hello");//By using == operator we can test is it same Objectif(source == str3){
System.out.println("Second Compare - Both are same String Object");}else{
System.out.println("Second Compare - Both are NOT same String Object");}}}

OUTPUT:

First Compare - Both are same String Object
Second Compare - Both are NOT same String Object

In above example we are trying to replace string "hi" with string"hello" where "hi" is not present in source string. So replace method will return same String Object instead of new String in first compare. In second replace we trying to replace "Hello" with same same string "Hello" where "Hello" present in source string. So replace method will return new String Object.

This is one of the easy interview programming question under 3 years experience. Lets assume given a lengthy string and asked you to find each character frequency. For example the given string is "hello java discover". In this string 'h'=1 , 'o'=2 etc., need to find each character occurred counts need to printed. We can achieve this by many ways like converting to character array and then manipulating each character or in looping count each character occurrence and store in any Collection object etc., In our below example we will use HashMap to store characters as key and their counts in value part.

We all know String class in Java is immutable and the value once stored in String Object can't be modified. Also we have discussed about this in our earlier tutorials like difference between String, StringBuffer and StringBuilder and how to create user defined immutable class in Java. We will see how "==" equals operator works on String Object. We know that when we apply "==" equals operator on Object it will check only the Object reference rather than values stored in the Object. So using "==" on string Object will result incorrect output and advised to use equals() method which internally overrides hashcode() which will check the values in the Objects using hashing algorithm and gives the correct output.In this tutorial we will see about how "==" equals operator works internally on string Object with different scenarios.

From above code we can clearly see first 3 string objects are having different references. But 4th "Str4" String in java file contains same value of 1st String "str1" but JDK has created StringBuilder class instead of String with different reference. Hence we are getting false when we compare "str1" and "str4" with "==" operator.