Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java bytecode - representation of types smaller than int

Tags:

java

jvm

bytecode

In one of the projects at my university I am working directly with Java bytecode.

After browsing the list of instructions available for the JVM (http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) I saw that there is no such thing as [b|c|s]store, only istore for storing integers in a local variable. Does it mean that if in my program I write:

short a;
int b;

I am not saving any memory, because every local variable entry occupies 4 bytes?

I was always under the impression that using short or byte types will save some memory at runtime.

like image 875
Andna Avatar asked Jun 13 '13 18:06

Andna


People also ask

Should I use byte instead of int?

Looking at the benchmark results in @meriton's answer, it appears that using short and byte instead of int incurs a performance penalty for multiplication. Indeed, if you consider the operations in isolation, the penalty is significant.

What is the size of bytecode in Java?

Each can be independently sized from 0 to 65535 values, where each value is 32 bits.

How does Java bytecode look like?

class files consist of a bunch of bytecodes. Bytecode is to Java what assembler is to C++. Each bytecode is a number no larger than a byte and has a mnemonic. The numbers and their mnemonic are what you have listed in your question.

How is Java bytecode executed?

Bytecode is essentially the machine level language which runs on the Java Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per method of the class. Whenever that method is called during the execution of a program, the bytecode for that method gets invoked.


1 Answers

This is explained in section 2.11.1 of the JVMS:

Note that most instructions [...] do not have forms for the integral types byte, char, and short. None have forms for the boolean type. A compiler encodes loads of literal values of types byte and short using Java Virtual Machine instructions that sign-extend those values to values of type int at compile-time or run-time. [...] Thus, most operations on values of actual types boolean, byte, char, and short are correctly performed by instructions operating on values of computational type int.

It is justified thus:

Given the Java Virtual Machine's one-byte opcode size, encoding types into opcodes places pressure on the design of its instruction set. If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a byte. Instead, the instruction set of the Java Virtual Machine provides a reduced level of type support for certain operations. In other words, the instruction set is intentionally not orthogonal. Separate instructions can be used to convert between unsupported and supported data types as necessary.

However, whilst this applies to load/store of stack variables, it doesn't apply to load/store into primitive arrays; there are opcodes for all primitive types.

like image 152
Oliver Charlesworth Avatar answered Oct 14 '22 05:10

Oliver Charlesworth