This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Constants" Watch "Constants" New topic
Author

Constants

Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
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".
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
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!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
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
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.

One by Peter Haggar (who was a JavaRanch bartender at one time) and a Tech article by Sun in addition to Sun's javap page.

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constants
 
Similar Threads
instanceof Operator!!
Local Inner Classes
Garbage Collection
Wrapper Class '=='
Garbage collection