Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Difference Between the String s = "String "; and String s = new String("String");

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi what is the Difference between the These Two Statements

String s = "String" ;
String s = new String("String ");
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class is the only class type that work out of a stack by default and String s = "string" is that, behaving almost as a primitive. It places this string value on the stack. Whereas, String s = new String("string"); creates a new place holder, a new object for this String. It would go to the Heap where all other objects go.

Regards
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first will create an instance of the String object in the heap with the value "String". A reference to this will be added to the string pool.

The second can potentially create two object on the heap. A String object as above (assuming the first line was not there) and a new String object as mandated by the sue of the "new" keyword.

Your code will not compile as you have a duplicate definition of the "s" member.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himanshu Kansal wrote:The String class is the only class type that work out of a stack by default and String s = "string" is that, behaving almost as a primitive. It places this string value on the stack. Whereas, String s = new String("string"); creates a new place holder, a new object for this String. It would go to the Heap where all other objects go.


This is not exactly right...

All objects are on the heap. Objects are never on the stack in Java.

Java has an optimization technique for strings, called the string pool. If you use the same literal string in your source code multiple times, then Java creates only one String object, which is shared between all the places where you use that string literal. For example, if you do this:

then there is just one String object, containing "Hello", and variables 'one' and 'two' are both referring to that same String object. Note that that String object is on the heap, just like any other object in Java.

To keep track of String objects that are the same, Java puts a reference to such String objects in the string pool.

When you explicitly use 'new' to create a String object, like this:

then this is what happens:

  • There is a String object that contains "Hello", which is created for the string literal in the source code.
  • A new String object is created and the content of the String object mentioned above is copied to it.


  • Note that this is unnecessary and inefficient. You should never use new String("...") in Java programs. If you see a fellow programmer doing that, explain to him or her that it's wrong!
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    String s1 = "string" is primitive type and place this on stack. String s2 = new String('string') creates new object in memory. This has different address than s1. s1==s2 will always be false. and s1.equal(s2) will be true as this compare content of string.
     
    Jason Irwin
    Ranch Hand
    Posts: 327
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    mmmm ppppp wrote:String s1 = "string" is primitive type and place this on stack.


    No it isn't, to both statements!

    String s2 = new String('string') creates new object in memory.


    That'll create an exception.

    I strongly suspect you'll be having a visit from a Bartender or Sheriff soon enough.
     
    Himanshu Kansal
    Ranch Hand
    Posts: 257
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jesper Young wrote:This is not exactly right...


    Yeah, I should have written "String-Pool" and not "stack" for the people here.

    The link here might help and clarify my statement.
    Regards
     
    Jesper de Jong
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    mmmm ppppp - welcome to JavaRanch. Please check your private messages for an administrative matter from JavaRanch.
     
    Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
    ScroogeXHTML - fast RTF to HTML and XHTML conversion
    https://coderanch.com/t/749563/ScroogeXHTML-fast-RTF-HTML-XHTML
    reply
      Bookmark Topic Watch Topic
    • New Topic