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

Strings and mutablelity

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will all of these cause a new string object to be made?

String name = �Dan�;
name = name + � Hansen�;
name = name.substring(3, 5);
name = name.toUpperCase();

Or how many of them will?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan you are right. All of them will create a new String on the heap.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However the methods substring and toUpperCase will not made the new String always.
 
Dan Hansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
However the methods substring and toUpperCase will not made the new String always.



Could you please explain why?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Dan Hansen"
String is allready Created and Avilable in the Pool
So it just gave Ref. of it not new Object.
 
Dan Hansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So "Dan Hansen" and "DAN HANSEN" is considered to be equal?
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan wrote...
Could you please explain why?


These two methods return the this object if it operation has no effect on the object on which it was invoked.
Please look into the API for more details.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is in reference to this article

here's the code...
class A
{

public static void main(String[] args)
{

String one = "hey";
String two = new String("hey");

one = two = null;

String three = new String();
//String three = new String("hey");
three.intern();

System.out.println("value="+three);

}
}
this prints "value="..

second part...

class A
{

public static void main(String[] args)
{

String one = "hey";
String two = new String("hey");

one = two = null;

//String three = new String();
String three = new String("hey");
three.intern();

System.out.println("value="+three);

}
}
this prints "value=hey"....

The article mentions that even if references to String Literal & String Object are assigned to null..the String Literal Pool still has a reference to the String literal on the heap making it noneligible for garbage collection..ever!
it says.."You can reuse String Literals with run-time Strings by utilizing the intern() method."

How can one reuse the above literal?or rather how would it be beneficial to reuse it?whats the advantage of letting a resource occupy memory eternally?
as it would help only if the string content was same?
Can anybody explain?
Thnx
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Dan Hansen]: So "Dan Hansen" and "DAN HANSEN" is considered to be equal?

No one said anything like that. The point was that IF a string is already all uppercase, then toUpperCase() will not return a new string; it will simply return the same all-uppercase string it started with.
 
mooooooo ..... tiny ad ....
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic