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