This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding ternary operator

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks ,

I have condition in my program...



Could you please help me in converting the above code as per ternary operator..!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You first. What have you come up with on your own? What problems are you having with it?
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You first. What have you come up with on your own? What problems are you having with it?


Hi Bear,

I have come up with following solution , but Please tell me , is it proper way..!!




This is i thnink is not correct , please advise me how to update it in right fashion..!! thanks in advance..!!
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct way to refactor those five lines of code you originally posted is like this:

Using a ternary operator like you're trying to do will just make the code even more unreadable. So why would you want to do that?
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Paul,
Paul Clapham wrote:The correct way to refactor those five lines of code you originally posted is like this:


could you please explain it in detail, as I am still not able to grasp it..!!

the other thing...

Paul Clapham wrote:
Using a ternary operator like you're trying to do will just make the code even more unreadable. So why would you want to do that?


Just to explore that how ternary operator behaves..!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:Hi folks ,

I have condition in my program...



Could you please help me in converting the above code as per ternary operator..!!


Isn't this the second or third time you've asked this question?
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Saral Saxena wrote:Hi folks ,

I have condition in my program...



Could you please help me in converting the above code as per ternary operator..!!


Isn't this the second or third time you've asked this question?


Hi Jeff,

Just to explore the role of ternary operator...!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:

could you please explain it in detail, as I am still not able to grasp it..!!
...
Just to explore that how ternary operator behaves..!!


YET AGAIN...

You need to show some effort, do some research yourself, and ask a more specific question about the specific parts that you're having trouble with.

Just coming to a fourm and saying, "Explain concept X to me," is not an effective way to learn, and it's not what forums like this one are for.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Jeff,

I have come up with my solution as you can go through the threads, and before posting on forum , i do googling and gone through books also..!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
Hi Jeff,

Just to explore the role of ternary operator...!!


What does that even mean? Exactly what part of "exploring the ternary operator" can you not accomplish by googling for examples and tutorials and writing a bunch of code to see how it works?

After you have done that, if some particular aspect is still giving you difficulty, post a question about that and provide details. You have a habit of saying "I don't understand broad concept X", and that is not an effective use of your time or ours.

Please put in a bit more though and effort before posting. Thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote: i do googling and gone through books also..!!


But there's no evidence of that and no indication of how far you got and where you got stuck. What we should see here is, "I read this thing and that thing, and I understand hits part, but this other part is still confusing me." Your questions read like you're using this site as your google search, as if you're starting from square one.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:

Hi Paul,
Paul Clapham wrote:The correct way to refactor those five lines of code you originally posted is like this:


could you please explain it in detail, as I am still not able to grasp it..!!


A detailed explanation of one line of code? Okay...

The isRewardsEnabled() method of the RewardsSupport class, or object, or whatever it is, returns a boolean value. Since you ask for a detailed explanation, we'll include the information that a boolean value can only be true or false. So this line of code takes that boolean value and returns it to the caller of whatever method the code is embedded in.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Paul Clapham wrote:
A detailed explanation of one line of code? Okay...

The isRewardsEnabled() method of the RewardsSupport class, or object, or whatever it is, returns a boolean value. Since you ask for a detailed explanation, we'll include the information that a boolean value can only be true or false. So this line of code takes that boolean value and returns it to the caller of whatever method the code is embedded in.


Hi Paul,

Thanks a lot for explaining it...the complete method follows as...



so paul in this case it it is enables then we must pass return RewardsSupport.isRewardsEnabled(); but what about the other case..!!in which it is false...!! could you please explain in detail block as I have done above here, that will be a great help..!! thanks in advance..!!
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:writing a bunch of code to see how it works

This is key.

It goes like this: 1. Read something about the ternary operator in a book, a tutorial, a blog, wherever. 2. Write a short program of half a dozen lines that does something similar with the ternary operator. 3. Write another little program that does something a little different with the ternary operator. 4. The programs you write while learning don't have to accomplish a significant task. In fact it can be better if they do not. The aim is succinctly to achieve, through writing code, fluency with whatever tool or concept you are struggling with. 5. Sprinkle system.out statements liberally in your learning code to illustrate what is happening every step of the way. 6. The crucial step: write a couple dozen little programs like this whenever you are learning a new concept. 7. Repeat step 6 with minor variations exploring different scenarios until your doubts are resolved.

There is nothing anyone here could say about the ternary operator that wouldn't be brought home much more forcefully by trying it out in your own code as I have outlined. You'll never become fluent in Java until you do this.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:

so paul in this case it it is enables then we must pass return RewardsSupport.isRewardsEnabled(); but what about the other case..!!in which it is false...!! could you please explain in detail block as I have done above here, that will be a great help..!! thanks in advance..!!


Hey,

Really I don't understand what you want to ask or are you kidding, finally you are returning back the value of RewardsSupport.isRewardsEnabled(), if this is true then return true if this is false then return false. even method itself doesn't make much sense if you can directly use RewardsSupport.isRewardsEnabled() where you are calling this method.

Also, why you are passing int b as the parameter to the method while you are not using it anywhere..
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ternary operators allow you to simplify writing simple conditional statements. For instance say you need the greatest value of 2 integers:

With ternary operators you can simplify this code to:

That's all there is to it.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:Really I don't understand what you want to ask or are you kidding, finally you are returning back the value of RewardsSupport.isRewardsEnabled(), if this is true then return true if this is false then return false. even method itself doesn't make much sense if you can directly use RewardsSupport.isRewardsEnabled() where you are calling this method.


Saral, this is the "detailed explanation" you were asking for. But really there are only so many times you can ask for a "detailed explanation" of more and more trivial points before you start looking foolish.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:Ternary operators allow you to simplify writing simple conditional statements.

Not exactly accurate. You don't conditionalize statements with the ternary operator but values.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
Jeff Verdegan wrote:
Saral Saxena wrote:Hi folks ,
Could you please help me in converting the above code as per ternary operator..!!


Isn't this the second or third time you've asked this question?


Hi Jeff,

Just to explore the role of ternary operator...!!


Are you not over exploring?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic