Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Ternary Operator | Deep Java Structure
Java Extended
course content

Зміст курсу

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

Ternary Operator

How can we shorten an if-else statement?

The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:

It looks unclear. Let me explain what is what.

First, we set a condition, for example, 10 > 5, and then we put a question mark ? after it. If the condition evaluates to true, we go to the true block, for example, System.out.println("That's true");. If the condition evaluates to false, we go to the false block, where we might have something like System.out.println("That's not true");.

Let's analyze a more practical example:

java

Main

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }

Note

We can place a boolean value within a regular System.out.println() because in this case, the toString() method of the Boolean class will be applied to the boolean object. The same situation applies to other data types.

You may have noticed that we used the ternary operator inside the System.out.println(); statement. That's the main purpose of using the ternary operator - you can use it within output statements to significantly reduce the amount of code.

We can also use the ternary operator when initializing or returning values. You'll learn more about returning values in the next section when you will study methods.

Example of using ternary operator when initializing value:

java

Main

12345678910
package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:

java

Main

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

The result is the same, but you can see how much space we save by using the ternary operator.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

Ternary Operator

How can we shorten an if-else statement?

The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:

It looks unclear. Let me explain what is what.

First, we set a condition, for example, 10 > 5, and then we put a question mark ? after it. If the condition evaluates to true, we go to the true block, for example, System.out.println("That's true");. If the condition evaluates to false, we go to the false block, where we might have something like System.out.println("That's not true");.

Let's analyze a more practical example:

java

Main

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }

Note

We can place a boolean value within a regular System.out.println() because in this case, the toString() method of the Boolean class will be applied to the boolean object. The same situation applies to other data types.

You may have noticed that we used the ternary operator inside the System.out.println(); statement. That's the main purpose of using the ternary operator - you can use it within output statements to significantly reduce the amount of code.

We can also use the ternary operator when initializing or returning values. You'll learn more about returning values in the next section when you will study methods.

Example of using ternary operator when initializing value:

java

Main

12345678910
package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:

java

Main

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

The result is the same, but you can see how much space we save by using the ternary operator.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

Ternary Operator

How can we shorten an if-else statement?

The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:

It looks unclear. Let me explain what is what.

First, we set a condition, for example, 10 > 5, and then we put a question mark ? after it. If the condition evaluates to true, we go to the true block, for example, System.out.println("That's true");. If the condition evaluates to false, we go to the false block, where we might have something like System.out.println("That's not true");.

Let's analyze a more practical example:

java

Main

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }

Note

We can place a boolean value within a regular System.out.println() because in this case, the toString() method of the Boolean class will be applied to the boolean object. The same situation applies to other data types.

You may have noticed that we used the ternary operator inside the System.out.println(); statement. That's the main purpose of using the ternary operator - you can use it within output statements to significantly reduce the amount of code.

We can also use the ternary operator when initializing or returning values. You'll learn more about returning values in the next section when you will study methods.

Example of using ternary operator when initializing value:

java

Main

12345678910
package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:

java

Main

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

The result is the same, but you can see how much space we save by using the ternary operator.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Все було зрозуміло?

How can we shorten an if-else statement?

The if-statement may not always look elegant, but Java provides a faster and more convenient way to check conditions. It is called the ternary operator, and it has the following syntax:

It looks unclear. Let me explain what is what.

First, we set a condition, for example, 10 > 5, and then we put a question mark ? after it. If the condition evaluates to true, we go to the true block, for example, System.out.println("That's true");. If the condition evaluates to false, we go to the false block, where we might have something like System.out.println("That's not true");.

Let's analyze a more practical example:

java

Main

12345678
package com.example; public class Main { public static void main(String[] args) { System.out.println(10 > 5 ? "That's true" : "That's false"); System.out.println(5 > 6 ? "That's true" : "That's false"); } }

Note

We can place a boolean value within a regular System.out.println() because in this case, the toString() method of the Boolean class will be applied to the boolean object. The same situation applies to other data types.

You may have noticed that we used the ternary operator inside the System.out.println(); statement. That's the main purpose of using the ternary operator - you can use it within output statements to significantly reduce the amount of code.

We can also use the ternary operator when initializing or returning values. You'll learn more about returning values in the next section when you will study methods.

Example of using ternary operator when initializing value:

java

Main

12345678910
package com.example; public class Main { public static void main(String[] args) { final int a = 10 > 2 ? 5 : 2; final int b = 10 < 2 ? 5 : 2; System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

Below is an example code where the ternary operator is replaced with a regular if-statement to help you better understand how it works:

java

Main

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 0; if (10 > 2) { a = 5; } else { a = 2; } if (10 < 2) { b = 5; } else { b = 2; } System.out.println("Variable 'a' has value: " + a); System.out.println("Variable 'b' has value: " + b); } }

The result is the same, but you can see how much space we save by using the ternary operator.

Завдання

You have a String variable. Write a code that prints true to the screen if the string contains the letter k, or false if the string does not contain this letter. Use the ternary operator to practice with it.

Секція 1. Розділ 6
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt