Interesting. We can manipulate constants to get a value, but of course we know the original value never changes. My question is...
How does Java actually find the value we're looking for? Does it create an internal variable to store the value?
An example would be...
and the output is 4 and 1.
The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
So your question is what are the behind-the-scene details of executing this statement: System.out.println(myInt << 2); Is that right? I don't know the how, but I think that the gory details of how Java does what is does isn't as important as understanding the reason why the output is 4 when this statement is executed, and why the output from System.out.println(myInt); will still be 1. Maybe someone can explain the mechanics of Java, but it's way over my head!
Are you familiar with javap? I'm not a byte code expert, but when I run javap -c on your example, I see
So ... it looks like your theory is correct. It is stored internally.
Here is a question for you. How does the byte code change if you use primitives rather than Integer objects?
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Nov 26, 2006
If I'm understanding it right (I really need to pick up a good book on the compiler and byte-code)...
It seems to be doing much the same thing as before, except it's shortened by not creating a new Integer object. [ February 25, 2007: Message edited by: Nathan Leniz ]
Marilyn de Queiroz
Joined: Jul 22, 2000
Yes, and by not having to call intValue() on the Integer ( 13: invokevirtual #5; //Method java/lang/Integer.intValue)I ) That's the way I understand it also. I found a couple of web pages on this that were pretty good, but nothing really good.
So ... (for those who felt that the javap output was mind-boggling), the JVM stores the original value away in a safe place, then it changes the value and prints it (but doesn't assign it to any identifier). Afterwards, it gets the saved value and prints that. You would get a different result if you reassigned the new value to the same identifier.
[ February 25, 2007: Message edited by: Marilyn de Queiroz ]