arg1 = arg2 ? returnSomeThing: orSomeThingElse
[java] Alternate IF statement
ok, I''m making a database for my programming class and I want to be able to check a variable using the ? method in c++ but I don''t know how to do it. Actually, I''m not even sure you''re aloud to. The syntex in c++ is:
I think I''ve seen it in a java program somewhere, but I''m not sure. ANy help would be appreciated.
Works just like C++.
[edited by - wayfarerx on October 1, 2002 4:46:10 PM]
Object value = getSomeValue();String result = value == null ? "value-is-null" : "value-is-not-null";
[edited by - wayfarerx on October 1, 2002 4:46:10 PM]
I tried to do something pretty similar to that. I''m going
state[4]==1 ? search.Show(true):search.Show(false);
but I''m getting an invalid assignment error on the ==
???
state[4]==1 ? search.Show(true):search.Show(false);
but I''m getting an invalid assignment error on the ==
???
From http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293
Is that what you're doing? Also, wouldn't this be a little more concise in your scenerio?
[edited by - wayfarerx on October 1, 2002 5:04:06 PM]
quote:Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear (§14.8).
Is that what you're doing? Also, wouldn't this be a little more concise in your scenerio?
search.Show(state[4] == 1);
[edited by - wayfarerx on October 1, 2002 5:04:06 PM]
thanks a lot. Both of my methods were a void return so that way of doing it wouldn''t work. Thanks for the
search.Show(state[4]==1)
idea. Wow, can you say blond moment, gotta love missing something that easy.
search.Show(state[4]==1)
idea. Wow, can you say blond moment, gotta love missing something that easy.
I hate this syntax....it''s obfuscated and JAVA would be a better (cleaner) language without it. I don''t find much other cryptic stuff in JAVA, so why this sh**?
quote:Original post by Anonymous Poster
I hate this syntax....it''s obfuscated and JAVA would be a better (cleaner) language without it. I don''t find much other cryptic stuff in JAVA, so why this sh**?
What so obfuscated about that? Besides, if you don''t like it, you could do something else.
if(state[4]==1) search.show(true);else search.show(false);
I suppose its a matter of preference. I''d still prefer the original syntax because it is more concise.
Also, it makes code like this much nicer, and I would say safer because it helps prevent you forgetting to initialising a variable:
int x = (y % 2 == 0) ? 5 : 10;// Is nicer than:int x = 0;if (y % 2 == 0) x = 5;else x = 10;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement