String in Java

Introduction

String is one of the most widely used classes in java programming by developers. Strings are immutable, so StringBuffer and StringBuilder classes come to make the string mutable (manipulations) easy way.

 

String

In Java, a string is an object which represents a sequence of characters. String is immutable, which means it can not be changed.

String uses the + operator to concatenate two strings and internally used the StringBuffer to perform this action.

 

How many ways do we create String Objects in java?

There are two ways we create a String Object in java.

  1. By using string literal
  2. By using new keyword

 

1. String literal

Java String literal is created by using double-quotes.

Syntax

String s="Hello";

When a new string literal is created, the JVM first checks in the “string constant pool” for the String with the same value. If the string already exists in the pool, then it returns the reference instance. If string is not available with the same value in “string constant pool”, a new string instance is created, and places that instance in the String pool.

 

Benefits of String literal in java?

String literal makes, Java more memory efficient because no new objects are created, if it already that object is available in the string constant pool.

 

1. new keyword

Syntax

String s=new String("Hello");

In the above syntax, JVM will create a new string object in heap memory and nd the literal “Hello” will be placed in the string constant pool.

 

Example of String class

public class Test {
public static void main(String args[]) {
String s1 = "TechnicalRound";// creating string by using java string literal
char ch[] = { 't', 'e', 'c', 'h', 'n', 'i', 'c', 'a', 'l' };
String s2 = new String(ch);// converting char array to string
String s3 = new String("Java");// creating java string by using new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}

Output:

TechnicalRound
technical
Java

 

What is Immutable String in Java?

Immutable means unchangeable, if once we create string object then its data or state can not be changed but a new string object is created.

 

Let’s try to understand with Example.

class Example {
public static void main(String args[]) {
String s = "Niraj";
s.concat(" Singh");// concat() method is appends the string at the end of existing object
System.out.println(s);// It will print Niraj
// because strings are immutable objects that's why it not appends
}
}

Output:

Niraj

In the above example, “Niraj” is not changed, but a new object is created with “NirajSingh” in String Constant Pool. It will print “Niraj”, because s variable is point to the “Niraj” object, that why spring is Immutable.

 

Let’s try to understand with another Example.

class Example {
public static void main(String args[]) {
String s = "Niraj";
s = s.concat(" Singh");
System.out.println(s);
}
}

Output:

Niraj Singh

In the above example, String creates an object “Niraj” and assigns to the s variable. And again we cancate “Singh”, and explicitly assign it to the reference variable s. In this case, s is printing “Niraj Singh”, because in the s variable we explicitly assign “Niraj Singh”. But still “Niraj” object is not modified.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

48 Replies to “String in Java”

  1. You made various good points there. I did a search on the subject matter and found nearly all people will have the same opinion with your blog. Kaitlynn Regen Lebna

  2. If some one wishes expert view regarding blogging and site-building after that i propose him/her to visit this webpage, Keep up the pleasant job. Elsinore Prinz Meriel

  3. I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey both interesting and useful guidelines by means of the blog and attract contribution from other individuals about this area of interest and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a dazzling job.

  4. I am glad for writing to make you know what a notable encounter my wife’s girl had studying your web site. She figured out numerous issues, not to mention what it’s like to have an amazing giving mood to have most people really easily know selected impossible subject areas. You really exceeded our own expectations. Many thanks for imparting the invaluable, safe, explanatory and even easy thoughts on the topic to Janet.

  5. A lot of thanks for all your valuable hard work on this web page. Kate loves getting into investigation and it’s really easy to understand why. I know all of the dynamic medium you make insightful solutions on this web blog and as well as foster participation from people on that idea so our girl is certainly discovering a lot of things. Have fun with the remaining portion of the new year. You are conducting a powerful job.

  6. I wanted to send you the very little remark to finally thank you the moment again over the spectacular techniques you have shared on this page. This has been simply surprisingly open-handed with you to offer unhampered what a few people could have advertised as an e-book in making some bucks for their own end, certainly considering that you could have done it if you considered necessary. The concepts additionally acted as a easy way to be aware that many people have a similar desire just as mine to figure out a great deal more concerning this condition. Certainly there are a lot more enjoyable instances ahead for folks who check out your blog.

  7. A lot of thanks for each of your hard work on this web page. Kim really likes getting into investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make invaluable steps on this web blog and as well as improve participation from people on that idea so our girl is certainly learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a pretty cool job.

  8. A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful strategies on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a really good job.

  9. My husband and i felt now excited Emmanuel could conclude his inquiry from your ideas he gained in your site. It’s not at all simplistic just to choose to be giving for free ideas that most people have been making money from. And we all do understand we have the website owner to be grateful to because of that. The explanations you’ve made, the easy site menu, the friendships you can give support to engender – it’s everything extraordinary, and it’s facilitating our son and our family do think that idea is cool, and that is highly important. Many thanks for all the pieces!

  10. I enjoy you because of all of your effort on this blog. Debby enjoys conducting internet research and it’s easy to see why. A number of us hear all about the compelling manner you create efficient guides by means of the blog and boost contribution from other ones about this article and my child is being taught a great deal. Enjoy the remaining portion of the year. You are always doing a really good job.

  11. I am also writing to make you know what a incredible encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people quite simply grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for giving the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

  12. I’m also commenting to let you be aware of of the excellent discovery my cousin’s child enjoyed checking your blog. She discovered a wide variety of details, most notably how it is like to have a marvelous coaching heart to get many more easily fully understand certain complicated matters. You actually did more than my desires. I appreciate you for delivering such great, healthy, educational and also cool guidance on that topic to Evelyn.

  13. I enjoy you because of all of the effort on this blog. Debby delights in conducting internet research and it’s easy to see why. A number of us hear all about the compelling form you create effective guides by means of the blog and boost contribution from other ones about this area of interest and my child has been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a fabulous job.

  14. My wife and i were very satisfied when Peter managed to finish up his survey while using the precious recommendations he received when using the site. It’s not at all simplistic just to be giving for free helpful tips that men and women could have been making money from. And we do know we have the blog owner to appreciate because of that. Most of the explanations you’ve made, the easy blog menu, the friendships you assist to engender – it’s everything excellent, and it’s assisting our son and our family do think that content is brilliant, and that’s extremely fundamental. Many thanks for all the pieces!

  15. Thanks for your whole work on this website. My mum takes pleasure in working on research and it is simple to grasp why. We notice all relating to the powerful ways you render very useful tips and tricks via your website and therefore strongly encourage response from website visitors on this theme while our own simple princess is without a doubt understanding so much. Take pleasure in the rest of the year. Your performing a very good job.

  16. My spouse and i have been really lucky that Jordan managed to finish off his research through your precious recommendations he had through the web pages. It is now and again perplexing to just find yourself handing out solutions which people today may have been selling. Therefore we know we need the writer to give thanks to for that. The most important illustrations you have made, the simple web site navigation, the relationships you will help to instill – it’s got most powerful, and it’s really leading our son in addition to the family reason why the situation is fun, which is really pressing. Thank you for the whole lot!

  17. I precisely desired to say thanks again. I’m not certain the things I could possibly have carried out in the absence of the actual creative concepts discussed by you concerning my concern. It absolutely was a depressing concern for me personally, but considering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating men and women by way of a web site. I am certain you’ve never come across all of us.

Leave a Reply

Your email address will not be published.