3

How do I simplify this code with a conditional operator in Java? This is from dr. Liang's Introduction to Java.

public class Test {
    public static void main(String[] args) {
        int i = 3;
        int j = 4;
        int k = max(i, j);
        System.out.println("The max int is " + k);
    }

    public static int max(int num1, int num2) {
        int result;

        if (num1 > num2) {
            result = num1;
        }

        else {
            result = num2;
        }

        return result;

    }
}

4 Answers 4

5

You can turn things into a one-liner using the conditinal ternary operator:

return (num1 > num2) ? num1 : num2;

But just for the record: it would be better to keep that max method around; simply for the sake of readability. It helps to have that functionality in its own method.

But of course: you want to read about java.lang.Math.max() which already provides exactly that functionality (not only for int, but all the other numerical primitive types as well)!

So, the real answer is: yes, use a method for that check; but don't create your own version; use the one that is already coming with Java by default. In other words: do not re-invent the wheel. Even when it is just a small wheel like this one.

Sign up to request clarification or add additional context in comments.

4 Comments

Sure, learning is always "different", but I guess you also learned that one theme "do not re-invent the wheel" today.
Sure, good point. Then lets reword that: one should know about existing wheels; and make a conscious decision about re-using existing wheels, or creating new ones. Both choices come with different (dis)advantages that should be taken into account when making decisions in the real world.
Whereas in IT, we have have far too many people that build their own wheels; very often ending up very much deficient compared to those that would be already available to us.
You are very welcome. I used IT to express the fact that yes, there is a broad spectrum of people creating software; and in the real world, and on stackoverflow, you are dealing with all kinds of people. Great discussion; have a good night. And for the record: I am already in bed, and I better shutdown my laptop now, too.
4

While you could use the conditional operator ? : to implement this

public static int max(int num1, int num2) {
    return num1 > num2 ? num1 : num2;
}

But, I would prefer Math.max(int, int) like

public static int max(int num1, int num2) {
    return Math.max(num1, num2);
}

You might also choose to use varargs and support an arbitrary number of int(s). Using an IntStream you could do

public static int max(int... nums) {
    return IntStream.of(nums).max().getAsInt();
}

1 Comment

Yes, I understand Math.max would be a better choice, of course. That is just for learning purposes (it was on the chapter book's on methods).
3

You can use a conditional ternary operator.

int k = (i > j) ? i : j;

This expression evaluates to:

If i is greather than j, assign i to k. If not, assign j to k.

Comments

2

This would be one way to modify your example to use the ternary operator.

You should generally keep the values in the operator short otherwise they get hard to read.

public class Test {
    public static void main(String[] args) {
        int i = 3;
        int j = 4;
        int k = max(i, j);
        System.out.println("The max int is " + k);
    }

    public static int max(int num1, int num2) {
        int result;

        result = (num1 > num2) ? num1 : num2;
        return result;

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.