?
Solved

A String Question to Ponder

Posted on 2002-06-07
9
Medium Priority
?
185 Views
Last Modified: 2010-03-31
Ok. Experts, try this one....

I have a String Constant.

  String str1 = "Hey baby!";

So, what happens here is that there is a String in a memory location (say at Address 5000) and str1 is now pointing (Referring) to that address (meaning str1 has value 5000).

If I do a change to this string....

  str1 = "Hello Baby!";

now str1 is NOT pointing to the same address. i.e.str1 contains no 5000 but something else. i.e. "Hello Baby!" is a new string constant. In fact the original string "Hey Baby!" is now garbage collected by the gc.

Now if I allocate a string like this...

  String str2 = new String("Hey baby!");

Then a new memory location (an object) is created and the value "Hey Baby!" is placed in there. So str2 is now referring to a memory location say 8000). THIS NOT A STRING CONSTANT. THIS IS A STRING OBJECT according many books. If I do a change to this string...

  str2 = "Hello Baby!";

Then the same thing happened in str1 also happens here. In other words, the String Object str1 is now garbaged collected and str2 is now pointing to a new String object with the value "Hello Baby!".

So in both cases exact same result happens. If that is the case what's the use of having a String Constant?. Isn't a String object acts exactly as a String Constant?
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7061978
all string are objects - independently if they are constants or not.
to declare a constant - use this:
public static final Strig strConstant = "const_value";

this string object will never be garbage collected and the strConstant cannot be changed to point to another object
0
 

Author Comment

by:prain
ID: 7062006
I agree.

But then why Java documentation, books  etc.... use the word "String Constant" to a declaration like...

String str1 = "Hello!";

Further is I declare anoter with the same value...

String str2 = "Hello!";

Why do both str1 and str2 have the same address?. Is'nt is because "Hello" is a constant?.

0
 
LVL 9

Expert Comment

by:Venci75
ID: 7062026
"Hello!" is the string constant, str1 and str2 are only references to this constant. the reason for having same addresses for the both references is because java is doing some optimizations. As you can see - you don't have a way to modify a string object directly. If you want to perform such operation (for example str1 += " Second hello.";) then the result will be a new string object.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 35

Expert Comment

by:girionis
ID: 7062043
>  Why do both str1 and str2 have the same address?. Is'nt is because "Hello" is a constant?.

  This done in Java in order to preserve memory and for optimization reasons (at least from Java2 onwards).

  Note that if you create two String objects using

  String s1 = new String("hello");
  String s2 = new String("hello");

  then the

  s1 == s2 will yield false while the

  String s1 = "hello";
  String s2 = "hello";

  s1 == s2 will yield true;

  In java new object are created using two ways, either by using the "new" operator or either by loading a class dynamically (for example Class.forName("<class name>")). By assigning the value "hello" (or any other value) to multiple Strings without using the "new" operator the JVM tries to preserve memory space, that's why you get variables that point to the same memory adress.

  Hope it helps.
0
 

Author Comment

by:prain
ID: 7062045
I agree with all you say...

So. In otherwords, all Java strings including objects can be called as string "Constants".

Because even if you define a string like this...

  String str1 = new String("Hello");

What you say in last reply paragraph2 applies. Right?.

So in my opinion, a String constant and a constant String
are two different things. In a contant String, your make it final, so that the variable cannot be used for any other purposes. But if a String variable is used to point to a String Constant, that variable can be used to point to another String constant (or an object).

0
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 7062054
yes - that is
0
 
LVL 35

Expert Comment

by:girionis
ID: 7062060
... as Venci75 clarified first of course...
0
 
LVL 35

Expert Comment

by:girionis
ID: 7062084
> So. In otherwords, all Java strings including objects can be called as string "Constants".

  Yes because in Java the object String is immutable, that is, once allocated it cannot be changed. Any change to the original String results in another, new String. This has performance and memory issues, this is why it is better to use StringBuffer for loads of String operations. And this si why it is called a String constant, because every change in the String results in a new one and the original is eligible for gc.

  As you said of course (and as Venci75 pointed out first) a constant String is one that is declared using the "final" keyword.

  Hope it helps.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7062093
>  And this si why it is called
a String constant, because every change in the String results in a new one and the original is eligible
for gc.

  To add here... So the original one is constant, it cannot be changed.

  P.S. Whoah.. things move really fast with EE. :-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

762 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