• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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: 562
1
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: 562
1
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: 64680
225
  • 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.
 
Bartender
Posts: 20842
125
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: 64680
225
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!