• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in class file  RSS feed

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The class file generated as follows
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody except the people who wrote the compiler know why the compiler chose to use a byte instead of an int here. You will not get a definitive answer to this question.

Maybe it's an optimization, since the value 5 also fits in a byte.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$ javap -c tricky3
Compiled from "tricky3.java"
public class tricky3 extends java.lang.Object{
public tricky3();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: iconst_5
1: istore_1
2: return

}
Looking in the Java™ Virtual Machine Specification (scroll down for iconst_<i>), I see it says "push int constant." So where does your informant get byte from?
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had another look at iconst_5 and it says
Each of this family of instructions is equivalent to bipush <i> . . .
So I looked up bipush and it said (even more interesting)
Push byte
. . .
The immediate byte is sign-extended to an int value. That value is pushed onto the operand stack.
So it turns back to an int, even though it appears to be recorded as a byte.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!