Can a Java method have more than 255 local variables?

The iload Java opcode is used to push a value from the local variable array (LVA) onto the operand stack.

This opcode takes a single byte as a parameter (en.wikipedia.org/wiki/Java_bytecode_instruction_listings), which serves as an index for the LVA.

If one element of the LVA is always the reference to the object (I guess this isn't the case for static methods, but lets ignore those for now), and if exactly one byte is used as an index (256 possible values), then how can a method have access to more than 255 different local variables?

Answers


Yes, it is possible for Java methods to have more than 255 local variables. The wide opcode instruction can be used to modify an iload or aload instruction to use a 16-bit index instead of an 8-bit index, meaning that you can have up to 65536 different local variables in a function, as long as the wide opcode ends up getting used.

That said, note that Java local variables do not necessarily correspond one-to-one with JVM local variables. It's possible that the Java compiler could look at your code, notice that space for locals can be reused in some contexts, then map multiple Java locals to the same JVM local variables.

Hope this helps!


Need Your Help

How can I merge many SQLite databases?

database sqlite

If I have a large number of SQLite databases, all with the same schema, what is the best way to merge them together in order to perform a query on all databases?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.