Wednesday, June 22, 2011

Method size limit in Java

Java cannot have a infinitely long method. The JVM limits the length of any method to 65535 bytes.

I would have not found it myself, I seldom write methods that are longer than 10-20 lines, I read it here.

A valid case can be while using long jsps. Okay, I do not write long jsps either, but if you have many (statically) included jsps, and you have several javascript files included in the jsps, you might encounter a compiler error.

65535 bytes is quite huge. I wrote a simple method printing out numbers to test the maximum length that it goes to, and discovered that it will take you 6546 such statements to exceed the size limit. Not possible in my world. This method would be completely unfathomable in both the meanings of the word.
public void unfathomableMethod(){
  int i = 0;
  System.out.println(i++);
  System.out.println(i++);
  System.out.println(i++);
System.out.println(i++);
  System.out.println(i++);
  System.out.println(i++);
 }
Nevertheless, there is a bug logged on the SDN: 4262078, on 12th Aug, 1999. That's right, sir! Almost 11 years ago. And I know why there has been no action towards this: if you need this, either you do not know the concept called loops, or you are doing some other really bad programming. Pathetic. On the verge of disgusting!

No comments:

Post a Comment