This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constants

 
Nathan Leniz
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nathan Leniz
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic