Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




Java - String

Say if you want to store your name or even a sentence in a variable. How would you do that?

The solution is the 'String' class in java.

There are two ways to declare a String :


1. String s = "Robert";

2. String s = new String("Robert");

Note : String in an immutable class. i.e. Once the values are assigned to a String object, it cannot be modified or changed.


Methods in String class

  1. String concat(String s)

concat(..) method is used to concatenate two Strings.

Say, we want to join String "Hello" and "World" and make it "HelloWorld".


public class Test{
  public static void main(String[] arg){
    String firstString = "Hello";
    String secondString = "World";
    String finalString = firstString.concat(secondString);

    System.out.println("The combined String is : "+finalString);
  }
}


Output :


   The combined String is :HelloWorld

  1. int length()

length(..) method is used to find the length of a String.


public class Test{
  public static void main(String[] arg){
    String firstString = "Hello";
    int length = firstString.length();

    System.out.println("The length of the String is : "+length);
  }
}

Output :


   The length of the String is : 5

  1. char charAt(int index)

charAt(..) method is used to return us the character of a String if we supply it with index value.

Say, if we want to know which character is present at 2nd index of the String "World". Assuming the count starts from '0'.


public class Test{
  public static void main(String[] arg){
    String firstString = "World";
    char pos = firstString.charAt(2);

    System.out.println("The character is : "+pos);
  }
}

Output :


   The character is : r

Note :Index starts counting from 0.

  1. String replace (char oldCharacter, char newCharacter)

replace(..) method is used to replace an existing character with a new character.


public class Test{
  public static void main(String[] arg){
    String firstString = "Hello";
    String newString=firstString.replace('l','g');

    System.out.println("The new String is : "+newString);
  }
}

Output :


   The new String is : Heggo

It replaces all 'l' with 'g'.


  1. String toLowerCase()

toLowerCase() replaces all uppercase letters to lowercase.


public class Test{
  public static void main(String[] arg){
    String firstString = "HeLLo";
    String lowerString=firstString.toLowerCase();

    System.out.println("The new lower case String is : "+lowerString);
  }
}

Output :


   The new lower case String is : hello

Converts all the letters to lowercase.


  1. String toUpperCase()

toUpperCase() replaces all lowercase letters to uppercase.


public class Test{
  public static void main(String[] arg){
    String firstString = "HeLLo";
    String upperString=firstString.toUpperCase();

    System.out.println("The new upper case String is : "+upperString);
  }
}

Output :


   The new upper case String is : HELLO

Converts all the letters to uppercase.


  1. String trim()

If there are any additional spaces in a String. trim() is used to delete those extra spaces.


public class Test{
  public static void main(String[] arg){
    String firstString = " Hello ";

    System.out.println("The trimmed String is : "+firstString.trim());
  }
}

Output :


   The trimmed String is : Hello

The trim() method removed all the additional spaces from the String "Hello".


  1. boolean equals (Object object)

equals(..) method in String is used to check if two objects are equal or not.

We will see the Example in the 'Decision Making' slide. Where we will learn about 'if' statement.


  1. String substring(int index) & String substring(int index,int length)

substring(..) method is used to fetch a chunk of letters from a String.

Say, if we want to get the String "llo" from "Hello".

Also, we want to get the String "el" from "Hello".


public class Test{
  public static void main(String[] arg){
    String firstString = "Hello";
    String subStr1 = firstString.substring(2);
    String subStr2 = firstString.substring(1,2);

    System.out.println("The substring is : "+subStr1);
    System.out.println("The substring is : "+subStr2);
  }
}

Output :


   The substring is : llo
   The substring is : el

How are String values managed in 'free pool' ?

So, when we create a String by directly assigning a value to it. i.e. When we define a String type variable of name 'firstString' by assigning a value 'Robert' to it,

String firstString = "Robert";

A memory space will be created for 'firstString' in so called 'free pool' and the value 'Robert' will be stored in it.

Now, if we define another String type variable 'secondString' and assign the same value 'Robert' to it,

String secondString = "Robert";

1. Java searches in the 'free pool' if the value 'Robert' is present of not.

2. If Robert is already present in the 'free pool'. A new memory space won't be created for 'secondString'.

3. Rather, 'secondString' will also be pointing to the same memory location which 'firstString' is pointing to.

java_String

How are String values managed in the Heap?

When we create a String data type using the 'new' operator,

String firstString = new String("Robert");

A memory space will be created for 'firstString' in so called 'heap' and the value 'Robert' will be stored in it.

Now, if we define another String type variable 'secondString' and assign the same value 'Robert' to it,

String secondString = new String("Robert");

Java creates a new memory space for 'secondString' and the value 'Robert' will be assigned to it.

java_String