The three-mesh operator and the mysterious function of + = and-=

Source: Internet
Author: User

Absrtact: Here everyone is familiar with the two symbols, all know that the trinocular operator is a shortcut mechanism for If/else control statements (Boolean expressions)? Statement: Statements such as if/else, like the execution, but three mesh operation also has a hidden data type conversion function, + = and-= is also not only add or subtract after the assignment, let us use a few examples to illustrate their hidden function.

I. Three-mesh operator:

Trinocular operator: When there is inconsistent data type, it will show the type of conversion, that is, it is loaded under the function of no conversion, not fit on the conversion;

Cases:

Package Com.lov.lianxitext;public class Valhold {public static void main (string[] args) {int a = 1; System.out.println (true?) ' B ': 65535);//' B ' is a character type, and 65535 is int type System.out.println (true?) ' B ': 65536); System.out.println (false? A: ' B ');}}

If we do not know its hidden function, only with three mesh operation, everyone will think the output is 98 98 98! But will that be the case?

Because ' B ' is a character type 65535 is an int, the character size is 2 bytes equal to the size of short, that is, the maximum positive integer that can be loaded is 65535, when more than, ' B ' will convert the data type to int output B ASCLL code 98

Two. + = and-=:

The addition and subtraction assignment symbol also has the conversion function of the data type;

Cases:

Package Com.lovo.javaoo2;public class TXET {public static void main (string[] args) {char[]array={' H ', ' e ', ' l ', ' l ', ' o ', '! '} ; changecase (array);} public static void ChangeCase (Char[]array) {for (int i = 0; i < Array.Length; i++) {if (array[i]>= ' A ' &&array[i ]<= ' Z ') {array[i]+=32;//①}else if (array[i]>= ' a ' &&array[i]<= ' Z ') {array[i]= (char) (array[i]-32);// ②}}}}

As the above code ① and ② comparison, we can see that the addition of the assignment has a type of conversion function.

The three-mesh operator and the mysterious function of + = and-=

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.