Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:21:36+00:00 2026-05-15T06:21:36+00:00

What is the difference between these two following statements? String s = text; String

  • 0

What is the difference between these two following statements?

String s = "text";

String s = new String("text");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:21:37+00:00Added an answer on May 15, 2026 at 6:21 am

    new String("text");
    explicitly creates a new and referentially distinct instance of a String object; String s = "text"; may reuse an instance from the string constant pool if one is available.

    You very rarely would ever want to use the new String(anotherString) constructor. From the API:

    String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable.

    Related questions

    • Java Strings: “String s = new String(”silly“);”
    • Strings are objects in Java, so why don’t we use ‘new’ to create them?

    What referential distinction means

    Examine the following snippet:

        String s1 = "foobar";
        String s2 = "foobar";
    
        System.out.println(s1 == s2);      // true
    
        s2 = new String("foobar");
        System.out.println(s1 == s2);      // false
        System.out.println(s1.equals(s2)); // true
    

    == on two reference types is a reference identity comparison. Two objects that are equals are not necessarily ==. It is usually wrong to use == on reference types; most of the time equals need to be used instead.

    Nonetheless, if for whatever reason you need to create two equals but not == string, you can use the new String(anotherString) constructor. It needs to be said again, however, that this is very peculiar, and is rarely the intention.

    References

    • JLS 15.21.3 Reference Equality Operators == and !=
    • class Object – boolean Object(equals)

    Related issues

    • Java String.equals versus ==
    • How do I compare strings in Java?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between these two statements, given the following language?? Ben likes
Is there a difference between the following two statements: mysql> EXPLAIN SELECT IF(arms IS
I have following two arrays. I want the difference between these two arrays. That
What is the main difference between these following two ways to give a method
Is there any performance difference between the following two statements? from item in collection
Where is the difference between the following two Statements? print surname= ,$myVal, \n; and
What is the difference between using the two following statements? It appears to me
In the following table definition, what is the difference between these two column definitions,
according to rails' built-in validation functions, what is the difference between these following two
Is there any difference between following two ways of creating an object. Student s1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.