• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

About String

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between these two statements

String str="prasad";
String str = new str("prasad");
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class represents character strings. All string literals in Java programs, such as "prasad", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "prasad";

This creats a string literal, sets it's contents to "prasad" and pases the reference to that literal back to your variable [str].

This however:

String str = new str("prasad");

creates a string literal and sets contents to "prasad" (just like above). But a new string is created and the reference to that literal is passed to it. THEN that new string's referenc is passed to your variable [str].

Since TWO string literals are created in the second example, use the first one in your programs.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jefff willis:

This however:

String str = new str("prasad");

creates...



Actually, of course, this line doesn't even compile. It would be

String str = new String("prasad");

But I think everybody here knows that and this was just a typo.
 
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explaination lies in understanding the difference between the comparison of an object reference and an object value. This little program explains it in code. Java will set name1 and name2 to the same object reference. Therefore, the first "if" statement evaluates to true, because the "==" is testing that the *references* are the same. However, name3 creates a brand new object with its own object reference. Therefore, the second "if" statement fails because the *references* are different. The third "if" statement evaluates to true because that statement is checking the contents of the String variables, not the references.

The output is as follows...

same
NOT same
equals

Here's the program ...



[added code tags - Ilja]
[ January 29, 2005: Message edited by: Ilja Preuss ]
 
I'm doing laundry! Look how clean this tiny ad is:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic