Basically in Java when you 'change' an existing String you effectively
create a new String object/literal as required, and then the existing
identifier is updated to refer to the new String object.
Strings are treated differently depending on how they've been created:
String s1 = new String("the first one"); // creates a new instance of
a String object (stored on the heap)
String s2 = "the second one"; //creates a String literal (stored in
the String literal pool maintained by the JVM)
String s3 = "the second one"; //creates a reference to the existing
String literal in the String pool
The thing is that you can't change a String in the String literal
pool, so:
s2 = "something else"
will NOT change the String literal referenced by s3, and will add
another String literal "something else" to the String pool.
Also if we were to define a new String object:
String s4 = new String("the first one");
then it would not be the same object as s1 even though the objects
have identical contents, so you could 'change' either of s1 or s4 and
not affect the other.
If the identifiers all refer to the same String object:
s3 = s2 = s1;
and then you 'changed' one of those String objects, then the other
identifiers DO NOT change because of what I described in the first
paragraph above.
On 28/01/2010, at 4:05 AM, Steven wrote:
As a follow-up question to my post:
Subject: Lab-1034: String Comparison with Equals?
Date: Jan, 27th, 2010
----------------------
http://www.javaworld.com/javaqa/2002-09/01-qa-0906-strings.html
I've seen written in several places that JAVA Strings are
"Immutable" eg. Constant, unchanging. And that if you declare
several identical String Variables, without using NEW, Java points
all the variables at the same object.
One would then suppose:
1) If three variables are all references to a single object,
changing the value of one of them would change all of them.
2) Of course, Strings are Immutable, so attempting to change it's
value should throw a compile-time error.
However the source-code below, a remix of lab-1004 (keyboard input
with SWING) - does exactly this. I initialize all my string vars
identically, and then repeatedly change them all as if they were
independent variables. And it WORKS FINE!
So what is going on here???
My Best Guess:
Suppose Java initialize all the string-vars to a single object.
- Then each time a specific-variable "changes", does it auto-
magically creates a NEW String-Object with the new value?
- And further, is it updating the object-reference for that
variable, without affecting other variables?
- If this variable is the ONLY pointer to a specific String Object,
and it "changes", does JAVA destroy the old object automatically
when the new one is created?
1) Unless I'm totally off-base, it seems JAVA is doing a lot of
memory-management in the background whether I want it too or not.
2) At this point, it seems JAVA is a bit caviler with the concepts
of "Immutable" and "pointers". What if I wanted those strings to
truly be Constants? Or if I intended all of those pointers to point
to the same object, perpetually?
I'm certainly not going to defend C++ String Handling, but since C++
is my frame-of-reference, I'm a bit confused here.
Thanks,
Steven
//-----------------------------------------------------------
import javax.swing.JOptionPane;
/**
* JavaPassion - Lab 1004 - Exercise
* MyGetInputFromKeyboardJOptionPaneProject3
* eg. Uses SWING!
* @author Steven G. Peterson
* @since January 25, 2010
*/
public class InputFromKeyboardJOptionPane {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String name = "default";
String name2 = "default";
String msg = "default";
String age = "default";
int ageInt = 0;
// Get Name and Age
name = JOptionPane.showInputDialog("Please Enter Your Name");
age = JOptionPane.showInputDialog("Please Enter Your Age");
// Check our values
msg = "name: " + name + "\n" +
"name2: " + name2 + "\n" +
"age: " + age + "\n";
JOptionPane.showMessageDialog(null, msg);
// Build Final Message
ageInt = Integer.parseInt(age);
if (ageInt > 100) {
name2 = "Grandpa";
msg = "Hello " + name2 + " " + name + ", "+ age + " is OLD!";
} else {
name2 = "Grasshopper";
msg = "Hello " + name2 + " " + name + ", " + age + " is
YOUNG!";
}
// Print it!
JOptionPane.showMessageDialog(null, msg);
}
}
//-----------------------------------------------------------
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en