Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

String in Java

Posted on 2012-09-21
2
Medium Priority
?
366 Views
Last Modified: 2012-10-16
Hi

What is the difference between the following 2 statements:


      String Str1 = new String("Welcome to Tutorialspoint.com");

      System.out.println(Str1.length());

And

String Str1="Welcome to Tutorialspoint.com";

      System.out.println(Str1.length());
0
Comment
Question by:JRawabi
  • 2
2 Comments
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 800 total points
ID: 38421162
In the first case , a new object is created irrespective of whether the string is available in the string pool

Second case , a new string object is created only if the string "Welcome to Tutorialspoint.com" is not available in the string pool . If it's already available , then a new object is not allocated
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38421203
When it comes to Strings , Java has a concept of string internalizing , or in short string interns.
The JVM maintains a pool of strings . Whenever you assign a string literal(as in the second case) the string gets pooled . Next time someone wants to create another string with the same value , then the String object from the pool is used. It means new memory is not allocated , but the existing reference is used.

But when you use the new() operator , new memory is allocated to the string , instead of using an already existing reference from the pool

For this reason , String class has an intern() method , which  will send a string to the pool . All this is for optimizing string memory due to the fact that strings are immutable.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question