aspose file tools*
The moose likes Java in General and the fly likes regarding ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regarding ternary operator" Watch "regarding ternary operator" New topic
Author

regarding ternary operator

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: Jan 10, 2002
Posts: 61653
    
  67

You first. What have you come up with on your own? What problems are you having with it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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

Joined: Apr 22, 2011
Posts: 202



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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 22, 2011
Posts: 202

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 22, 2011
Posts: 202



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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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

Joined: Apr 22, 2011
Posts: 202



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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 12, 2011
Posts: 808
Saral Saxena wrote:
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

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..


Do not wait to strike till the iron is hot; but make it hot by striking....
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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

Joined: Jan 10, 2002
Posts: 61653
    
  67

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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?


Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding ternary operator