Javase Review Diary: Java operator \ relational operator \ logical operator \ assignment operator \ string connector \ Ternary operator

Source: Internet
Author: User


//Java operator \ relational operator \ logical operator \ assignment operator \ string connector \ ternary operator
/ *
*A Java operator
*
*What are the Java operators?
*              +, -, *, /, %, ++, --
*Here we need to pay attention to the difference between a + + and + + a;
* /
/ *
public class Javase{
public static void main(String[] args){
Int a = 10;
Int b = 20;
Int c = 30;
System.out.println( a+b );//a+b=30;
System.out.println( b-a );//b-a=10;
System.out.println( a*b );//a*b=200;
System.out.println( b/a );//b/a=2;
System. Out. Println (B% a); / / here% is the co operator, B / a = 2..... 0; so the result is 0;
System. Out. Println (C% B); / / C / b = 1... 10; so the result is 10;
A + +; / / equivalent to a = a + 1;
System. Out. Println (a); / / the result is 11;
A --; / / equivalent to a = A-1;
System. Out. Println (a); / / the result is 10, because a = 11;
}
}
* /
//------------------------------------------------------------------------------
/ *
*Two Java relational operators
*
*What are Java relational operators?
*          >, >=, <, <=, ==, !==
*As for the priority problem in multiple operations, it is suggested to enclose it in parentheses to avoid calculation mistakes in mind;
*
* /
/ *
public class Javase{
public static void main(String[] args){
Int a = 10;
Int b = 10;
Boolean C = (a = = b); / / Boolean only has true and false values, so here is a judgment to determine whether a is equal to B. if the judgment is true, output true, otherwise output false
System. Out. Println (a > b); / / it is obvious that a and B are equal, so the result here will be false. You can judge later
System.out.println( a>=b );
System.out.println( a<b );
System.out.println( a<=b );
System.out.println( a==b );
System.out.println( a != b );
}
}
* /
//------------------------------------------------------------------------------
/ *
*Three Java logical operators
*This is the same system as the door in the university curriculum (NAND gate, or non gate, etc.)
*Judge:
*True is true if &amp; bit and both sides are true
*If the| bit or one side is true, the result is true
*! bits are not reversed
*True if ^ bit is different or logical values on both sides are not equal
*
*Logical comparison:
*&amp; &amp; a sign is true only when it is true on both sides. One mistake is false
*If one of the two sides of the sign is true, it is true
*!
* ^
*
*Take a simple example:
*(1 = 2 &amp; 1 = 1) -- > the left side is false, the right side is true, but the identifier is bitwise AND. If one is false, both are true, then it is true. All the results are false
*(1 = 2 | 1 = 1) ---- > it is the same or the same, the judge is bit or, if one is true, it is true, and the result is true
*
* /
/ *
public class Javase{
public static void main(String[] args){
System.out.println(5>3 &amp; 5>4);
System.out.println(5>3 &amp;&amp; 5>4);
System. Out. Println (3 > 5 &amp; 6 > 7); / / the second step will still operate
System. Out. Println (3 > 5 &amp; &amp; 6 > 7); / / if one is false, it is false. In the second step, it will not operate
System. Out. Println (5 > 3 | 5 > 9); / / still works
System. Out. Println (5 > 3 | 5 > 9); / / if one is true, it is true, so it will not be operated later
System.out.println(5>11 | 5>10);
System. Out. Println (5 > 11 | 5 > 10); / / later
System.out.println(!false);
System.out.println(true ^ false);//true
System.out.println(false ^ true);//true
System.out.println(true ^ true);//false
System.out.println(false ^ false);//false
}
}
* /
//------------------------------------------------------------------------------ 
/ *
*Four assignment operators
*
*What are the assignment operators
*= (basic assignment operator)
*+ = plus equals
*- = minus equals
** = multiply by
*/ = divide by
*% = remainder equal to
*
*How to use it:
*For example: I + = 10; I = I + 10; that is, I and 10 are added and then assigned to I;
*
*
*Here, to solve the problem left before, I + +; and + + I; difference, the following example
* /
/ *
public class Javase{
public static void main(String[] args){
Int i = 10;
//i = i++;
//System. Out. Println (I); / / the output is 10;
I = + + I;
System. Out. Println (I); / / the output is 11;
//Why is that?
//First, I = I + +; I = 10; so the result is 10;
//And I = + + I; first + + 1, then assign to I, so the result is 11;
}
}
* /
//Let's talk about the assignment operator directly. Let's take the example above===========================================
/ *
public class Operator_04{
public static void main (String [] args)
{
//Basic assignment budget match, try byte = 127
Int a = 10;
/ / + =
A + = 10;
System.out.println(a);
/ / = =
A = 5;
System.out.println(a);
/ / * =
A * = 5;
System.out.println(a);
/ / / =
A / = 15;
System.out.println("a = " + a);
System.out.println( 10/15 );
/ /% =
A% = 2;
System.out.println(a);
//-----------------------------------
//Internal automatic type conversion
Byte I = 10;
//Two operations; since the next 1 is an integer, it will be automatically converted to an integer, and the result will be an integer; however, the previous I variable is the specified byte type, and assigning an integer to the byte type will definitely result in an error
//i = I + 1;
//Setting a is a one-step operation; however, data type conversion will be carried out in advance to convert the data type of cost class; therefore, precision will be lost
I + = 119;
System.out.println("hello");
System.out.println(i);
}
}
* /
//------------------------------------------------------------------------------
/ *
*Five string connector
*
*String connector:+
*To be honest, it's wasteful to make a chapter of this character alone. It's easy to see the function of this character in code and running results. I won't talk about it. I'll just say a few things worth noting:
*String connector "+": a string connector, even if it is a numeric addition operator;
*He will perform automatic data type conversion (Boolean does not participate in the conversion)
*The result is also a string type
*
* /
/ *
public class Operator_05{
public static void main (String [] args)
{
//String connector
System.out.println("PI=" + 3.14);
Int a = 10;
Int b = 12;
System.out.println("a+b = " + (a+b));   //a+b=22
System. Out. Println ("a + B =" + A + b); / / A + B = 1012 from left to right, calculation by calculation, data type by conversion
System. Out. Println (a + "+" + B + "=" + (a + b)); / / 10 + 12 = 22 parentheses to improve the sex operation level, first operation
}
}
* /
//------------------------------------------------------------------------------
/ *
*Sextuple operator
*
*What is a ternary operator?
*Conditional discriminant true executed statement: false executed statement
*The above is the writing method of ternary operator. I don't think there's anything to say. Let's go straight to the code
* /
public class Operator_06{
public static void main (String [] args)
{
boolean sex = false;
//10 / / this is not a statement; separate values, separate
Char c = (sex? 'male': 'female';
System.out.println(c);
boolean isSuccess = true;
System. Out. Println (issuccess? 's': "failed");
}
} 





Javase Review Diary: Java operator \ relational operator \ logical operator \ assignment operator \ string connector \ Ternary operator


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.