Java-2.5 ternary operators and string Operators

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.