File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How this worked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How this worked?" Watch "How this worked?" New topic
Author

How this worked?

Nilesh Nadiyana
Greenhorn

Joined: Dec 24, 2002
Posts: 21
public class t9 {
public static void main(String[] args) {
m1();
}
static void m1() {
String s1 = "This is How you Remind Me!";
String s2 = "Rock and Roll";
System.out.println(s1.charAt((s1=s2).indexOf('a'))); // Line 1
System.out.println(s1); // Line 2
}
}
Why the output at Line 1 is "i" and then the output at Line 2 is "Rock and Roll" ?
TIA.
Nilesh.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
Variables will not change value within a statement. This means that s1 at s1.charAt is still the original declared s1. Even though the indexOf method is run on the desired string reference, s1 has not actually changed until the statement is complete.
So, the indexOf() method returns 5, and the charAt() method of the original s1 returns 'i'. After the statement is complete, the s1 reference is changed, and it points to the same as s2.
It's like transactions in a database. It's not committed to memory until the whole transaction is processed.


Darryl Failla
Sun Certified Java 2 Programmer
Nilesh Nadiyana
Greenhorn

Joined: Dec 24, 2002
Posts: 21
Thanks Darryl .
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
It's like transactions in a database. It's not committed to memory until the whole transaction is processed.
Can't we just talk about the stack here?
Any purists?


Normal is in the eye of the beholder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How this worked?