Java main class structure: operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

Assignment operators

int a=100

The assignment operator, denoted by the symbol "=", is a two-dollar operator whose function is to pay the workshop operation the value contained in the room operand.

A is defined as an int type and is assigned a value of 100, which completes the assignment operation.

public class Demo {
public static void Main (string[] args) {
int a,b,c;//a,b,c are defined as int types
a=15;
c=b=a+4;
System.out.println (the value of "C" is "+c");
System.out.println (the value of "B" is "+b");
}
}

The result is: The value of C is 19
The value of B is 19

Arithmetic operators: As the name implies, arithmetic operations are the same as + 、-、 * (multiply),/(except),% (take surplus)

The format is:

public class Arith {
public static void Main (string[] args) {
Float number1=45.56f;//float float type and assign value to float type
int number2=152;//declares an int integer and assigns a value
System.out.println ("and for" + (Number1+number2));
System.out.println ("Difference to" + (Number2-number1));
System.out.println ("Multiply by" + (Number2*number1));
System.out.println ("Divide to" + (Number2/number1));
System.out.println ("Take remainder" + (number2%number1));
}

The result of the operation is: and 197.56
Difference is 106.44
Multiply by 6925.12
Divide to 3.3362598
Take the remainder 15.319996

Self-increment and self-subtraction

Condition: Must be an integer or floating-point variable, and then automatically grow by an integer or floating-point number 1

public class Increase_ self-increment {
public static void Main (string[] args) {
int b;
int a = 0;
B=++a;
System.out.println (b);
}
}

The result of the operation is: 1

Self-subtraction: There will be automatic subtraction during Operation 1 For example: The result of the 0 operation is-1

public class Reduction_ auto minus {
public static void Main (string[] args) {
int b;
int a = 0;
B=--a;
System.out.println (b);
}

}

The result of the operation is:-1

The difference from C language

The difference between the self-increment and the self-increment in C in the public class Java {
public static void Main (string[] args) {
int j=0;
for (int i=0;i<100;i++) {
j=j++;
}
System.out.println (j);
}
}

Operation Result: 0

    1. The mechanism of the Java intermediate cache variable, in Java, when performing a self-increment operation, assigns a temporary
    2. Variable, if it is prefix plus (++i), will be added to the temporary variable after 1, if it is suffix plus (i++), it will
    3. Assign a value to a temporary variable and add 1. The end result of the operation is not the variable itself, but the temporary variable assigned to it.
    4. So the above j=j++:
      1. Temp=j;
      2. j=j+1;
      3. J=temp;

Intermediate caching mechanism:

public class Java_ Intermediate cache variable mechanism {
public static void Main (string[] args) {
Method_1 (); static method of calling Method_1 () from inside the Main method
Method_2 (); static method of calling Method_2 () from inside the Main method
}

private static void Method_2 () {
int j = 0; Defines j as int integer and assigns a value of 0

         for (int i=0;i<100;i++) {       //traverse decimal   
             j = J ++; Decimal                          
        }         
         System.out.println (" Method_1---"+j);    //Output Method 1 decimal after addition
  
 }

private static void Method_1 () {
int j = 0;
for (int i=0;i<100;i++) {
j = ++j; The location is slightly changed
}
System.out.println ("Method_2---" +j);

}
}

Comparison operators: As the name implies is the comparison size or equal or greater than or not equal to the right

public class Compare {
public static void Main (string[] args) {
int number1=4;
int number2=6;
SYSTEM.OUT.PRINTLN (The return value of the Number1>number2 is: "+ (NUMBER1>NUMBER2));//The comparison size Number1>number2 greatly false.
SYSTEM.OUT.PRINTLN (The return value of the Number1==number2 is: "+ (Number1==number2));//numerber1 and number2 are equal. is False
SYSTEM.OUT.PRINTLN (The return value of the Number1!=number2 is: "+ (Number1!=number2));//numerber1 and number2 are not equal. is True
SYSTEM.OUT.PRINTLN (The return value of "Number1>=number2:" + (Number1>=number2));//number1 is larger than number2 or equal to False
}
}

The result is: Number1>number2 The return value is: false
The return value of Number1==number2 is: false
The return value of Number1!=number2 is: true
The return value of Number1>=number2 is: false

Logical operator: The logical judgment is true or false when the operation is performed. Whether a short circuit is judged and operated on a Boolean type

logical operators
operator meaning usage combined direction
&&,& logic with (what and what logic) Opl&op2 from left to right
//|| Logic with (what or what logic) opl| | Op2 from left to right
//! Logical non (not what)! Op from right to left
Perform a logical operation (result of the operation)
Expression 1 expression 2 expression 1&& expression 2 expression 1| | Expression 2! Expression 1
True (True) True (True) True (True) True (True) False (false)
public class Calculation {
public static void Main (string[] args) {
int a=2;//declaring variable int type a
int b=5;//declares that the variable int type is
Declares a Boolean variable that holds the return value after the Apply logical operator ' && '
Boolean result= (A>b && (a!=b));
Declares a Boolean variable that holds the Apply logical operator "| |" The return value after
Boolean result2= ((a>b) | | (a!=b));
SYSTEM.OUT.PRINTLN (result);
System.out.println (RESULT2);
}
}

The result of the operation is: false
True

Bitwise operators: Rules according to binary decimal hexadecimal and 32 are divided into: with operators, non-operators, or operators, XOR operators

With operator:

But the logical operators are logical operations on two relational operators,
The bitwise operators are mainly for the bits of the two binary digits for the logical operation.
public class And_operator_ and operator {
public static void Main (string[] args) {
int a=129;//defines an int type named A and assigns a value of 129
int b=128;//defines the int type named B and assigns a value of 128
System.out.println ("A and B" with the result is: "+ (A&B));//logic for A and b
}
The value of "a" is 129, and the conversion to binary is 10000001,
The value of "B" is 128, and the conversion to binary is 10000000.
According to the operation Law of the operator, only two bits are 1,
The result is 1, but
To know that the result is 10000000, or 128.

Non-operator:
Non-operators are denoted by the symbol "~"
public class Not_operator_ non-operator {
public static void Main (string[] args)
{
int a=2;
SYSTEM.OUT.PRINTLN ("A non-result is:" + (~a));
}
}
A is assigned a value of 2,a=2, according to the binary formulation 2 10 has the law of non-operation to complement the result is 3

Or operator:

public class Or_operator_ or operator {
Or operator
Or operator with the symbol "|" Shows that its operation law is as follows:
Two bits as long as there is a 1, then the result is 1, otherwise 0
public static void Main (string[] args)
{
int a=129;
int b=128;
System.out.println ("A and B" or the result is: "+ (a|b));//a or B operation result
}
Run results
A and B or the result is: 129
The following analysis of this program segment:
The value of a is 129, and the conversion to binary is 10000001,
And the value of B is 128,
The conversion to binary is 10000000, according to the operation Law of the OR operator,
Only two bits have one is 1, the result is 1, you can know the result is 10000001,
That is, 129.
}

Xor operator

The XOR operator is denoted by the symbol "^", and its Operation law is:
Two operand bits, the same result is 0,
public class Xor_operator_ Xor operator {
public static void Main (string[] args)
{
int a=15;
int b=2;
System.out.println ("A and B xor the result is:" + (a^b));
}
The result of A and B xor is: 13
Analysis of the above program segment: The value of a is 15, converted to binary 1111,
And the value of B is 2, converted to binary 0010,
According to the Operation Law of XOR, it can be concluded that the result is 1101 or 13.
}

Ternary operators:

Boolean b=20<45;? Ture:false;

Similar to If .... Else statements can be used as criteria for judging

Java main class structure: operators

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.