Solved

A String Question to Ponder

Posted on 2002-06-07
9
174 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
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
Comment Utility
"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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
>  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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:prain
Comment Utility
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 50 total points
Comment Utility
yes - that is
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
... as Venci75 clarified first of course...
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> 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
Comment Utility
>  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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now