ZMedia Purwodadi

Java Strings | technical-arbaab

Table of Contents

 Java Strings


Strings are basically non primitive data types which are used to store sequence of characters which will work same as the character array for ex-


String name = "technical-arbaab";


It will work same as-


char name2[] = {'t','e','c','h','n','i','c','a','l','-','a','r','b','a','a','b'};


Methods to define a string

  • by String literal
  • by new keyword (creating the instance)


String literal-

String name = "Jack";


by new keyword-

String s = new String("Jack");


program 01- "To demonstrate the working of string in Java"

Source code-

 public class stringsworking {  
      public static void main(String[] args)  
      {  
           String name1 = "Jack";  
           String name2= new String("Martha");  
           String numbers = "123456";  
           Character string[] = {'j','a','c','k'};  
           System.out.println("by string literal the string is"+name1);  
           System.out.println("by new keyword the string is"+name2);  
           System.out.println("my number string is"+numbers);  
           System.out.print("my character array is ");  
           for(int i=0;i<string.length;i++)  
           {  
                System.out.print(string[i]);  
           }  
      }  
 }  

Output:

 by string literal the string isJack  
 by new keyword the string isMartha  
 my number string is123456  
 my character array is jack  

Whenever we declare a string by string literal it becomes immutable(unchangeable) and it will create an object in the string constant pool inside the heap and when you declare another string which consists of the same value as of previous object, the new object is not created in the string constant pool it will refer to the previous object contains the same value hence it is also a memory efficient method to define a string for ex-


String s1="Welcome";
String s2="Welcome";//It doesn't create a new instance

Image by - Google | Image by - javatpoint.com

In Java, String is a class which implements Char Sequence interface and along with string two more classes also implements Char Sequence interface String Buffer and String Builder so it means we can create string from these two classes also but keep in mind that by creating an object of string class we got a immutable string and by creating the object of String Builder and String Buffer class we got a mutable string so whenever you want to create a mutable string you will use String Builder or String Buffer for ex-

Image by - Google | Image by - javatpoint.com

 StringBuffer s1 = new StringBuffer("arbaab");  
 StringBuilder s2 = new StringBuilder("Jack");  
 System.out.println(s1);  
 System.out.println(s2);   

Output: 

 arbaab  
 Jack  

It is recommended to use String Builder in place of String Buffer because String Builder is faster and preferred over String Buffer for the single-threaded program if thread safety is needed, then String Buffer is used.

Inside the String class there are various methods to perform some operation on string like -compareTo() equals() length() toUpperCase() toLowerCase split() replace() contains() substring() etc. To know about each of the method in detail click on it and see. 

How to take user input of type string buffer and string builder using scanner class

for taking user input of type string builder and string buffer class by scanner method you just need to do nothing but to follow this syntax-

 Scanner s = new Scanner(System.in);  
 StringBuilder input = new StringBuilder();  
 input.append(s.nextLine());  

program 02- "Take a name from user of type String Builder and check whether it is equal to "technical-arbaab" or not if it is then print "okay code is good" else print "sorry code is bad".

Source code-

 public class string04 {  
      @SuppressWarnings("unlikely-arg-type")  
      public static void main(String[] args)  
      {  
           @SuppressWarnings("resource")  
           Scanner sc = new Scanner(System.in);  
           StringBuilder s1 = new StringBuilder();  
           System.out.println("enter a name");  
           s1.append(sc.nextLine());  
           if(s1.toString().equals("technical-arbaab"))  
           {  
                System.out.println("okay code is good");  
           }  
           else  
           {  
                System.out.println("sorry code is bad");  
           }  
      }  
 }  

Output:

 enter a name  
 technical-ripon  
 sorry code is bad  

let me clear you one thing here, we have taken input of type String Builder and before comparing it with the desired string by equals() method we have converted it into string type by using the method tostring() and then check it's equality with the desired string by the help of equals() method because we cannot access equals() method by the String Builder object as it is inside the String class and similarly you can do the same for String Buffer class. (hope it is clear to you).



Arbaab khan
Arbaab khan FEAR has two meanings "forget everything and run" "Face everything and rise" The choice is yours Welcome u in our tech blog

Post a Comment