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
Solved

A String Question to Ponder

Posted on 2002-06-07
9
176 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 75
MySQL  on Tomcat 8 67
jsp insert to database example 2 43
jboss 7.1 start up error 1 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

792 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