Java-2.5 ternary operators and string Operators
In this section, we will discuss the ternary operators and string operators.
1. condition? Result: result)
Why do we say this? In fact, I want to advise you not to use it too much because it reduces the readability of the Code.
See the following code:
package com.ray.ch01;public class Test {public static void main(String[] args) {int a = 1, b = 2;if (a > b) {System.out.println(a);} else {System.out.println(b);}System.out.println(a > b ? a : b);}}
The above are two code blocks with the same functions. Although the ternary expressions are more concise and clear than the if else above, if there are many such expressions in the program, the readability will be greatly reduced.
Therefore, it is not an issue to use it. Please master this degree and consider it for future maintenance personnel and yourself.
2. String operator =, + =
The basic type is added to the string. The basic type after the string is converted into a string and then added.
Let's take a look at a group of interesting code:
package com.ray.ch01;public class Test {public static void main(String[] args) {String a = 1 + 2 + ;String b = + 1 + 2;System.out.println(a);System.out.println(b);}}
Output:
3
12
This set of code proves our conclusion above.
Summary: This chapter focuses on the attention of ternary operators and string operators, and you should try to use them as few as possible.
This chapter is here. Thank you.