-3

ı want to use short form for if statement. How can ı write if statement at one line? and how can ı compare them ı know there is same question at here. but my statement do not have else so ı could not do it without else statement.

public int compareTo(Uyum u) {
            if (uyum < u.uyum)
                return -1;
            if (uyum > u.uyum)
                return 1;
            return 0;
        }
user2583040
  • 83
  • 10

6 Answers6

4

You can use a ternary operator :

 return uyum < u.uyum ? -1 
      : uyum > u.uyum ? 1 
      : 0;
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    Sorry for messing with your code, but I just wanted to demonstrate that this idiom can be very readable and intuitive, no matter how many conditions there are. – Marko Topolnik Jul 25 '13 at 12:57
  • @markoTopolnik Thanks for making the code readable , always open to suggestions from experienced people :) – AllTooSir Jul 25 '13 at 13:09
  • Actually, the last :0 should have one more level of indentation, to be completely readable, no? – mdm Jul 25 '13 at 13:17
  • @mdm No, all the colons should be aligned. Think of if-else if-else if. The same thing happens there. – Marko Topolnik Jul 25 '13 at 13:18
  • @MarkoTopolnik: Ok, but that makes it quite less readable, IMO. In this case it's ok, because conceptually the three conditions are complementary and cover all the spectrum, but what if that was not the case? just being picky, I guess :) – mdm Jul 25 '13 at 13:21
  • 1
    @mdm So do you indent the final `else` in an `if-else if` cascade? That would be exceedingly strange and against the conventions, but it would be a direct analog to indenting the final line in the code above. – Marko Topolnik Jul 25 '13 at 13:26
  • I see your point now. Admittedly, I do not indent in case of else-if's. I'll think about it next time I use the ternary operator :) – mdm Jul 25 '13 at 13:36
2

You perhaps want:

return a < b ? -1 : (a > b ? 1 : 0);
Ingo
  • 36,037
  • 5
  • 53
  • 100
1

you may simply write this:

public int compareTo(Uyum u) {

return uyum - u.uyum;

}

  • 1
    That will not return the correct answer. The results should be one of either `-1`, `0` or `1`. – Felix Glas Jul 25 '13 at 12:53
  • 1
    @snipes83 Actually, it will. Check the documentation on `Comparable`. – Marko Topolnik Jul 25 '13 at 12:55
  • @MarkoTopolnik How do you know that OP is implementing `Comparable`? :) OP doesn't say. – Felix Glas Jul 25 '13 at 12:56
  • @snipes83 Let's say it's a wild guess at my part :) – Marko Topolnik Jul 25 '13 at 12:58
  • Also, there are limit cases even if he's implementing `Comparable` - what happens if one is `Integer.MAX_VALUE` and the other `Integer.MIN_VALUE` ? Better to use `Integer.compare(int a, int b)` if running Java 1.7, or use the ternary operator otherwise. – Nicolas Rinaudo Jul 25 '13 at 12:59
  • @NicolasRinaudo A simple cast of either argument to `long` would fix it, too --- unless they are already `long`s :) – Marko Topolnik Jul 25 '13 at 13:04
  • I thought this had a (small) [overhead](http://stackoverflow.com/questions/2170872/does-java-casting-introduce-overhead-why) though, why do that when you have perfectly readable and correct alternatives? – Nicolas Rinaudo Jul 25 '13 at 13:06
  • @NicolasRinaudo Clearly, all this discussion is just chitchat, so why not throw in every possible alternative anybody can think of :) – Marko Topolnik Jul 25 '13 at 13:15
0

Inline if statement (question mark) are normally only desired if you have two states. Readability of your code is very important. In your case you have 3 states (0, 1 and -1); I would not recommend using the inline if statement.

Example inline if statement:

String value = variable==null?"defaultvalue":variable;

This if statement notation is called ternary operator

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
0

It seems to me that u.uyum is an int, in which case, why is the following not satisfactory?

return Integer.compare(uyum, u.uyum)

If you really must use a ternary operator, you can do as everybody else is suggesting:

return uyum > u.uyum ? 1 : uyum == u.uyum ? 0 : -1

This tends to be frowned upon though, as it's not terribly legible.

Nicolas Rinaudo
  • 6,068
  • 28
  • 41
  • How can you say it's an Int? It could be short, char, double, even bool. Hence `((Comparable) uyum)`, it will box to the correct type. – Ingo Jul 25 '13 at 12:59
  • Wouldn't that cause compilation warnings (`unsafe call to compareTo(T) yadda yadda yadda`) though? – Nicolas Rinaudo Jul 25 '13 at 13:02
  • Do you mean casting to `Comparable`? That seemed somewhat fishy to me, so I gave it a go and it doesn't compile. Or am I missing something? – Nicolas Rinaudo Jul 25 '13 at 13:08
  • @NicolasRinaudo I have withdrawn that comment. However, it must be possible to find a notation that covers all primitives! – Ingo Jul 25 '13 at 13:10
  • @MarkoTopolnik Yes, but wasn't the whole (laudable) point of the initial comment to avoid assuming `uyum` was an `Integer`? – Nicolas Rinaudo Jul 25 '13 at 13:10
  • @Ingo I'm sure you're right, I was hoping you knew it and would teach it to me :) – Nicolas Rinaudo Jul 25 '13 at 13:12
  • 1
    @MarkoTopolnik I'm sure it is, but the whole point of this derailed comment thread was to pretend we didn't know the type of `uyum` and to find a way to perform the comparison nevertheless. If we know the type of `uyum`, there's no point to @Ingo and I's discussion, nor is there any fun to be had. We like fun. Don't you? – Nicolas Rinaudo Jul 25 '13 at 13:31
  • @NicolasRinaudo Ooops, missed that, you are absolutely right :) – Marko Topolnik Jul 25 '13 at 13:33
  • 1
    So, in the spirit of fun check out this beauty: `public int compareTo(Uyum u) { return compare(uyum, u.uyum); } static > int compare(T left, T right) { return left.compareTo(right); } ` You can make `Uyum#uyum` any numeric type you want. – Marko Topolnik Jul 25 '13 at 13:40
  • You, sir, are a true artist. – Nicolas Rinaudo Jul 25 '13 at 13:42
0

return (uyum-u.uyum)==0?0:((uyum-u.uyum)<0?-1:1)

Shashi
  • 12,487
  • 17
  • 65
  • 111
  • What if uyum is **boolean**? – Ingo Jul 25 '13 at 13:15
  • @MarkoTopolnik Is that true! Java won't compare boolean! Now I understand better why people hate Java. – Ingo Jul 25 '13 at 13:19
  • @Ingo Among all the things why people hate Java I have never, ever heard of the "uncomparable boolean" complaint. I have also never had a use case where that would be useful. – Marko Topolnik Jul 25 '13 at 13:22
  • You see one here: Abstracting over the (primitive) type that is in some class. Consider: I could more or less generically sort the following `class Person { int gender; String name, int age; }` Now someone comes on and points correctly out that `int gender;` should be better `boolean female;` And now, with that better design, the sortability is lost? – Ingo Jul 25 '13 at 13:30