LWN.net Logo

Compiled Java

Compiled Java

Posted Nov 5, 2009 12:47 UTC (Thu) by jengelh (guest, #33263)
In reply to: Compiled Java by man_ls
Parent article: Welte: Android Mythbusters (Matt Porter)

>it's raison d'être was to have "C++ without the warts",

One's warts are another's trademark. In C and C++ you can take the address (or reference, whatever) of an object and pass it around. In Java instead you have to create an extra Integer() class for the same job. So you have done away with the pointer wart in exchange for an overhead wart.


(Log in to post comments)

Compiled Java

Posted Nov 5, 2009 13:44 UTC (Thu) by skitching (subscriber, #36856) [Link]

Compilers for statically-typed languages can play many tricks; code performance can be very tricky to estimate.

For example, in Java the Integer class is final, ie cannot be subclassed.

Therefore, all method calls made via a reference of type Integer can be inlined.

In addition, if the compiler can determine that the integer's lifetime does not extend beyond the function in which it is created (ie in which "new" is called), then the data can be allocated on the stack.

And if the compiler can determine that an Integer is never cast to a parent type (Number or Object), then no Object instance needs to be created, ie just 4 bytes for the data are needed, like in c.

So it can be possible for a good java compiler to create identical code for java or c++ in many cases.

As always,
* actual profiling is best, and
* the tradeoff between safety, performance and developer time must be kept in mind.

There are many things about the Java syntax that annoy me, but the fact that java code can never overwrite memory it does not own is a very powerful thing.

Compiled Java

Posted Nov 6, 2009 22:48 UTC (Fri) by bcopeland (subscriber, #51750) [Link]

So it can be possible for a good java compiler to create identical code for java or c++ in many cases.

Unfortunately even Sun can't make a good java compiler :)

Compiled Java

Posted Nov 5, 2009 18:21 UTC (Thu) by khc (subscriber, #45209) [Link]

usually you want to pass primitive types by reference because your function needs to modify it. In Java, Integer is immutable so you can't use Integer to substitute for int in that case anyway.

Compiled Java

Posted Nov 5, 2009 20:59 UTC (Thu) by man_ls (subscriber, #15091) [Link]

As another commenter already said, creating an Integer() would not work. The reason you cannot pass a reference to an int so that it is modified by the function is that you are not expected to do that. And the solution is not to create a class with just an integer attribute and pass that -- the real solution is to not pass integers, but objects with behaviors. Integers are best kept as attributes of objects and handled within those objects. If you really have to change just an int it's best to have the method return a new int. Keeps your code simpler and reduces side effects -- and performance is just as good.

This kind of policy may force you to make things a different way, but it is a perfectly valid way -- it's idiomatic Java if you wish. Now, there are some idiotic policies in Java (like getters and setters, what a waste of time) but keeping methods from modifying their (primitive) parameters seems like a good design choice to me.

Compiled Java

Posted Nov 6, 2009 1:13 UTC (Fri) by alankila (subscriber, #47141) [Link]

In fairness, in many cases an inner class, anonymous or not, can be used. The inner class has access to the containing class's scope, and can thus be passed to wherever you want and the methods in that interface will then be able to modify the members of the containing class. This is pretty nice technique as it gets you something that feels like closures.

In a rare occasion someone might use a kludge like pass a new int[1] { value } so that the array's first element can be adjusted by the called code. Tricks like 1-element arrays are also sometimes used in Python, to circumvent Python's way of determining the scope of a variable from a first assignment in a block.

Copyright © 2010, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds