8.Java three mesh operator and comma operator

Source: Internet
Author: User
I. Three-mesh operator

Ternary operators generally use very little, because it is very poor readability in the program section, so I recommend not to use the ternary operator often, but very little use does not mean not to use, so still have to master its usage, ternary operator expression is as follows:
boolean expression? Value 0: value 1
Its operation is to return a value of 0 if the result of the Boolean expression is true, and return a value of 1 if the result of the Boolean expression is false, for example, the following program segment.
public class data21{
public static void Main (string[] args) {
int a=10;
int b=20;
SYSTEM.OUT.PRINTLN ("The result of this ternary expression is:" + ((a>b)? ') A ': ' B '));
}
}

Analysis of the above program segment: Because "a" is less than "B", so "a>b" the result of the relational operator is "false", since it is "false", then choose the value 1, that is, the result of this ternary operator is "B".

Two. Comma operator

In Java programming, the comma operator is generally used to separate several conditions from each other, for example, each element in an array is separated by a comma from other elements.
public class var{
public static void Main (string[] args) {
int a=1,b=2,c=3;
System.out.println (A+B+C);
}
}

Output Result:
6

8.Java three mesh operator and comma operator

Related Article

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.