Strings in Java

  • By
  • June 8, 2022
  • JAVA
Strings in Java

Strings in Java –

What is a string?

String is a group of characters which are denoted with the double quotes. As we know there no particular data type in C program for string so we used array to store the string values in C programming. Later when learn C++ programming there we got at separate data type where we can store string data and coming to java there we got a class with the name String where inside the class object we are store the string values into the arrays only. There is two ways to create object for java String class.

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

1.By string Literal.

Java String literal are created by using double quotes

Ex: String s=”Welcome”;

Each time when you create the literal, the JVM check the “string constant pool” first. If the string is already exists in the pool, a reference to the pooled instance is returned. If the string doesn’t exist in the pool, a new string instance is created and placed in the pool 

2.By new keyword –

When we use,

String S=new String();

In the above example, it is going to create two object and one reference variable.

Where String is a class name and S is reference variable by using new keyword we are going to create String object the object is stored in reference variable S. This is how the storing the sting values in java carried out.

Now we will move deep into String in java.

String : String is a basically an object that represent sequence of char values. An array of characters works same as java string.

Ex: char ch={‘J’,’A’,’V’,’A’,’P’,’R’,’O’,’G’,’R’,’A’,’M’,’M’,’I’,’N’,’G’};

String str=”JAVAPROGRAMMING”;

Java String provides lot of methods to perform on string value such as follows:

compare(), concat(),equals(),split(),length(),replace(),compareto(),intern(),substring() etc.

The diagram represents that String class implements the Serializable, Comparable, CharSequence. The CharSequence interface is used to represent the sequence of the characters. String, StringBuffer, Stringbuilder implements it and we can create java string by using any of these classes.

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Why string objects are called as immutable in java?

String is a unavoidable value in any programming application String reference are used store various attributes as usename, password, etc. In java String immutable is nothing but unmodifiable or unchangeable.

Once the string object created its data or status is not changed but new object is created 

Let’s try to understand the concept of the immutable

 

class Testimmutablestring

{  

      public static void main(String args[])

      {  

             String s=”Elon”;  

             s.concat(” Telsa”);//concat() method appends the string at the end  

             System.out.println(s);//will print Sachin because strings are immutable objects  

       }  

Output:

Sachin

Now you can see in the above code that I have created one string reference variable where the “Elon” value is stored after that by using concat method we are appending the “Telsa” value to the existing string reference after that we are trying to print the s reference variable  value now when we are doing this we expect that the output should be “Elon Telsa” but the output is only “Elon” that is because of immutable when concat and create new reference then we may get the expected output but if we try to print s which is reference variable then it will print Elon only why because it will not change by concanting the new value with it if we want print the value with appended value then I need to store it in new reference and print that reference then we get expected output that “Elon Telsa”.

class Testimmutablestring

{  

      public static void main(String args[])

      {  

             String s=”Elon”;  

             String s1=s.concat(” Telsa”);//concat() method appends the string at the end  

             System.out.println(s1);//will print Sachin because strings are immutable objects  

       }  

Output:

Elon Telsa 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

So this is a immutable concept in the java there are some drawbacks in the java String that if we want to change the existing string is not possible because of this immutable nature if we have requirement of changing the string then we have two other class StringBuilder and StringBuffer to know what are these two class and what is of these two class we will see it in next blog.I hope you are clear with the concept whatever we have seen in this blog.

Author:

Chetna Malagi

Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

  

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*