Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ternary operator error

Thread Tools

ternary operator error

Marco Schmidt
Posts: n/a

>Not really I think in this example....
>"b" + (a == 0 ? "c" : a)
>"c" is String
>a is int, gets implicitely boxed in Integer....
>Tenary expression has type of Object....
>"b" + Object is legal so everything OK....

OK, in this context the autoboxing feature could work. However, I
don't really know how this feature is implemented in the compiler. If
the fact that a variable X is an int produces an error, will the
compiler start again and treat X as an Integer?

Please reply in the newsgroup, not by email!
Java programming tips:
Other Java pages:
Reply With Quote
pete kirkham
Posts: n/a
Jon A. Cruz wrote:
> pete kirkham wrote:
>> the source code for String.valueOf is:

> No. Not "the" source. "A source"

Even so, but you were saying '~blah~ is better than ~wibble~' not saying
'~blah~'s better because some VM with undefined internals may implement
it in a more optimal manner than the reference implementation of ~wibble~'.

> Other JVM's (including many of IBM's, which are often top performers)
> use the fact that the String class can access the internals of the
> String class (while the Integer class can not) to optimize if needed.

I've taken a quick look at IBM's java site and can't find anything like
that. Where did you get this information? Why on earth don't they use
the same optimized code for both: there's such thing as 'private' in
machine code? Why do they change the calling pattern so as to break
mixed-use with other libraries?

> Remember, the language spec only said it behaves "as if"
> Integer.toString() is called, not that it must call it.

Hence the quote from the reference implementation, not the API

> > so for code where you've
>> decided that a is an integer the integer version is more efficient.

> Ahh.. But also...
> If you happen to change the declaration of the variable ('a' in this
> case) to some other type ..

Yes, if you haven't decided than a is an integer, then the other
approach is more flexible. Such changes should idealy be done by a tool
not the programmer.

> That also makes the code more OO friendly.

Personally, I think what would make the code more OO friendly would be
to turn type checking off where the result is being passed to a form
which doesn't care what the type is, as in the StringBuffer calls I
posted. The Java language tries to be both OO and strongly typed, and so
falls into the mud between.


Reply With Quote
pete kirkham
Posts: n/a
Jon A. Cruz wrote:
> Ok.
> It is better because:
> 1) It is clearer as to the intent. Most experts say to write for clarity
> first.

I'd say that writing "s" + (a==0 ? "foo" : a) is clearer to the intent.

> 2) It is an 'optimization' that usually doesn't make any noticeable
> difference, and is very commonly premature.

So use a tool to optimize: a lot of Java would greatly benefit from a
preprocessor/macro expander. If you put the optimization into a tool,
that expands a generic form, then you save time optimizing by hand.

> 3) It helps reduce bugs.
> 4) It makes the code hold up even if the type of the variable changes.
> 5) It allows the code to be copy-n-pasted and reused without having to
> be changed. Reduces rogue tile occurances.

They are identical in this respect.

> 6) On the fastest VM's, doing it that way can be faster. (although you
> should note point 2)

Is the IBM VM faster when running on OS X? I couldn't find any benchmarks

> ...
> There, is that better?

I think you missed the point, and I maybe would have been better to post
without the aside on the extra method indirections; the main gain I was
driving at was to allow a tool to expand the generic form where deciding
the type can be left until the expression is used (given that changing
the language seems a bit optimistic). OK, so if you're targeting one
particular vendor's JVM, then a small gain can be made by customizing
the expansion in a different way than what is better for the standard one.

> You seem to misunderstand. It's all in Java-land, not native land. Also
> it does nothing to break use, and does not introduce any other problems
> to do so.

Yes, not having had any reason to inspect the 1.1 implementation of the
IBM libraries, I misunderstood your use of internal, and assumed it was
internal to the JVM implementation not it's libraries.

> The reference implementation is just to be consulted on points on which
> the language spec does not cover. Since it does in fact cover this
> point, the reference implementation does not trump it.

Neither the language spec nor the API spec cover whether it's any
'better'. The reference implementation does indicate that it's
(marginally) better to expand one way rather than the other in terms of
calls, so I chose to do it that way, which AFAI can tell is better on
the JVMs I get to use.

> I disagree. If you need to change a variable declaration, it should not
> require a tool to assist you to do so. If that is the case, it sounds
> like your code is probably too fragile and unmaintainable.

In the absence of a customisable compiler, writing a tool that expands

<String> + (<bool> ? <type A> : <type B>)

into the StringBuffer calls I suggested would make it less fragile, not
more, for reasons you've already pointed to.

Such a tool should also be able to pick up on forms such as

String s = "foo";

if (a != 3) {
s += a;

s += "bar";

Which could use only the one StringBuffer instead of the two that javac
does (the one in the OS X JDK, maybe IBM's compiler already has this
covered, I don't know, they don't do OS X developer kits).


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call selection using ternary operator marco_segurini C++ 4 09-21-2004 01:37 PM
Need for the ternary operator glongword C Programming 6 05-17-2004 01:33 PM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM
Union, ternary operator, Macro, printf don't cooperate for me. Help? Paul E Johnson C Programming 2 10-17-2003 06:41 AM
union, ternary operator, and C. What a mess! Paul E Johnson C Programming 3 10-17-2003 03:44 AM