Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Ternary operator

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that this is a very basic question but still please tell me

Ternary operator is used as

(condition)?true case statement:false case statement;

So what is this below?Someone please explain

boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand?  You're declaring 3 booleans and setting them to false.  There is no ternary operator involved.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Venolia wrote:What don't you understand?  You're declaring 3 booleans and setting them to false.  There is no ternary operator involved.



In boolean p1isRight = false;

So this is just assigning value of false to variable p1isRight?

If yes thanks.
It's just that I've seen it in a book and was confused.

 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.

Edit (Hey, I can edit now!  whoo hoo!!).

That is partially correct.  You are declaring the boolean variable p1isRight and setting it to false.
 
Marshal
Posts: 65846
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but you are still not using the ternary operator, whose official name is the conditional operator. Look at this sort of code:-
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . but you are still not using the ternary operator, whose official name is the conditional operator. Look at this sort of code:-



No.I was not trying to use ternary operator there.

I was trying to determine if ternary operator is used in the line below I've seen in a book.



I WAS thinking isRight is some sort of keyword in the p1isRight and it was ternary operator statement shortcut .

Now I am clear that it's just plain old variable declaration.
 
Saloon Keeper
Posts: 21141
134
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ternary" is just a description for the type of operator.

Monadic operators have only one operand: i++

Binary operators have 2 operands: a + b

Ternary operators have 3 operands: (a < b)? a+1 : b-1

You can use the name "ternary" because there's only one ternary operator in Java, but it's a sloppy thing to do.


Java doesn't split words for meaning. However, there is a convention in JavaBeans that the read access method for a boolean property should have its name begin with "is" instead of "get", although "get" usually works as well.

So in common usage, you might have:



Note how the first letter of the property name is capitalized after the "is". That, too is part of the JavaBean conventions. These aren't inherent properties of the Java language, but the bean tools use these naming conventions to make it easier to construct generic access mechanisms.
 
Campbell Ritchie
Marshal
Posts: 65846
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . . I was trying to determine if ternary operator is used in the line below I've seen in a book. . . . Now I am clear that it's just plain old variable declaration.

Correct
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!