Results 1 to 3 of 3
- 03-10-2011, 06:19 AM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
doubt regarding ternary operator (?)
In C++
I'm able to do something like this
When i tried the same thing in Java it's not possible. It wants some variable to hold the result even though the return type of function1 and function2 are void.Java Code:(some_condition)? function1():function2();
Is there any other way to do it?
- 03-10-2011, 07:07 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
It wants some variable to hold the result even though the return type of function1 and function2 are void.
It's not so much that you must assign the result to some variable, rather, "There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3)." (JLS 4.1 The Kinds of Types and Values).
Is there any other way to do it?
Yes:
Java Code:if(someCondition) { method1(); } else { method2(); }
- 03-10-2011, 08:15 AM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
Use of ternary
By wulfgarpro in forum New To JavaReplies: 1Last Post: 09-21-2010, 06:31 AM -
question about the operator ++
By dardar in forum New To JavaReplies: 10Last Post: 08-16-2010, 02:39 PM -
help with not equal to operator !=
By manowar689 in forum New To JavaReplies: 9Last Post: 06-15-2010, 12:10 AM -
Confused between the usage of printf and println... and ternary operator
By blessed07 in forum New To JavaReplies: 2Last Post: 02-27-2010, 07:10 AM -
bitwise not ~ operator
By kezkez in forum New To JavaReplies: 2Last Post: 02-10-2010, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks