SoFunction
Updated on 2025-03-06

C# operator two Arithmetic operator

The assignment operator also has the usage of the combination of the arithmetic operator as mentioned in the appendix before. The usage is: For example, if you want to add x to 4 and then assign it to x, you can write it as x+=4.

Copy the codeThe code is as follows:

public class MathOps{
public static void main(String [] args){
Random rand=new Random(47);
int i,j,k;
j=(100)+1;
("j:"+j);
k=(100)+1;
("k:"+k);
i=j-k;
("j-k:"+i);
i=k/j;
("k/j:"+i);
i=k*j;
("k*j:"+i);
j%=k;
("i%=k:"+j);
float u,v,w;
v=();
("v:"+v);
w=();
("w:"+w);
u=v+w;
("v+w:"+u);
u=v-w;
("v-w:"+u);
u=v*w;
,print("v*w"+u);
u=v/w;
("v/w:"+u);
u+=v;
("u+=v:"+u);
u-=v;
("u-=v:"+u);
u*=v;
("u*=v:"+u);
u/=v;
("u/=v:"+u);
}
}

Output:
j:59;
k:56;
j+k:115;
j-k:3;
k/j:0;
k*j:3304;
k%j:56;
j%=k:3;
v:0.5309454;
w:0.0534122
v+w:0.5843576
v-w:0.47753322
v*w:0.028358962
v/w:9.94025
u+=v:10.471473
u-=v:9.940527
u*=v:5.2778773
u/=v:9.940527
Through objects of the Random class, programs can generate many different types of random numbers. The method is very simple, you only need to call the methods nextInt() and nextFloat() (or nextLong() or nextDouble()). The parameter passed to nextInt() sets the upper limit of the generated random number, and its offline line is 0, but this offline line is not what we want, because it will generate the possibility of dividing 0, so the +1 operation is done.
The compiler will automatically determine the role of the + and - signs in the unary operator.
For example: x=a*-b; The compiler can compile its meaning before b - the number is a negative sign, but to avoid confusion among readers, the best way to write it is
x=a*(-b)
The unary minus sign is used to transform the symbol of the data, while the unary plus sign is only to correspond to the unary minus sign, but its only function is to promote smaller types of operands to int;
Arithmetic operators

Operator

Use

Description

+

op1 + op2

Returns the sum of op1 and op2

-

op1 - op2

Returns the difference between op1 and op2

*

op1 * op2

Returns the product of op1 and op2

/

op1 / op2

Return the quotient of op1 divided by op2

%

op1 % op2

Return the remainder of op1 divided by op2

Automatic increment and decrement
There are two very good and fast operations in increment and decrement operations provided in Java (usually called "auto-increment" and "auto-decrement" operations). Among them, the decreasing operator is "--", which means to reduce one unit of private matters, and increment is "++" means to increase one unit
++a is equivalent to a=a+1 and the same is true for decreasing
Increment and decreasing are divided into "prefix" and "suffix".
For prefix: ++a,--a, the operation will be performed first and then the value will be generated. For the suffix a++, a--, the value will be generated and the operation will be performed.
Copy the codeThe code is as follows:

public class AutoInc{
public static void main(String [] args){
int i=1;
("i:"+1);
("++i:"+ ++i);
("i++:"+ i++);
("—i:"+ --i);
("i--:" + i--);
}
}

Output
i:1
++i:2
i++:2
--i:1
i--:1
From the above example, you can read the difference between prefix and suffix.
Increment operator is an explanation of the name c++, "one step beyond c"
Relational operators
The relationship operators mainly generate a boolean Boolean result. The result between the comparison values ​​returns true if true and false, return false. The relationship operators include <,>,<=,>=,=,!= (not equal), equal to and not equal to suitable for all basic data types, while other operators are not suitable for operations on boolean values, because there is no greater or less relationship between true and false.
Copy the codeThe code is as follows:

public class Equivalence{
public static void main(String [] arg){
Integer n1=new Integer(47);
Integer n2=new Integer(47);
(n1==n2);
(n1!=n2);
}
}

Output
false
true
The result may be different from what you think. Here, although the object content is the same, the object references are different, and == and! = Comparison is the reference of the object, not the content.
What should I do if the actual content of the two objects is the same? The equals() method that applies to all objects is required. But this method does not apply to basic types, and the basic types are directly used ==,! = Just
Copy the codeThe code is as follows:

public class EqualMethod{
public static void main(String [] args){
Integer n1=new Integer(47);
Interger n2=new Integer(47);
((n2));
}
}

Output
true
But if the referenced object is a class created by yourself, the result will be different
Copy the codeThe code is as follows:

class Value{
int i;
}
public class EqualsMethod2{
public static void main(String [] args){
Value v1=new Value();
Value v2=new Value();
==100;
((v2));
}
}

Output
false
The result is false again, because the default of equals is actually a relatively citation. Therefore, unless we rewrite the equals method in our new class, it will not achieve the expected effect.
Relational operators

Operator

Use

Description

>

op1 > op2

Return true when op1 is greater than op2

>=

op1 >= op2

Return true when op1 is greater than or equal to op2

<

op1 < op2

Return true when op1 is less than op2

<=

op1 <= op2

Return true when op1 is less than or equal to op2

==

op1 == op2

Return true when op1 is equal to greater than op2

!=

op1 != op2

Return true when op1 does not equal op2

Logical operators
Logical operators and (&&), or (||), non (!) can return boolean values ​​according to the relationship between parameters.
Copy the codeThe code is as follows:

public class Bool{
public static void main(String [] args){
Random rand=new Random(47);
int i=(100);
int j=(100);
("i=" + i);
("j=" + j);
("i > j is" +( i>j));
("i < j is" +( i<j));
("i > =j is" +( i>=j));
("i <=j is" +( i<=j));
("i ==j is" +( i==j));
("i !=j is" +( i!=j));
("(i <10) && (j<10) is" +((i <10) && (j<10)));
("(i <10) || (j<10) is" +((i <10) ||(j<10)));
}
}

Output
i=58
j=55
i>j is true
i<j is false
i>= j is true
i<=j is false
i==j is false
i!=j is true
(i <10) && (j<10) is false
(i <10) ||(j<10) isfalse
The versus or non-operation can only be applied to boolean values. If boolean is used where it should be a String value, the boolean value will automatically be converted to the appropriate form.
It should be noted that the comparison of floating point numbers in the program is very strict.
Conditional operator

Operator

Use

Description

&&

op1 && op2

Return true when op1 and op2 are true; if the value of op1 is false, the operand on the right is not calculated.

||

op1 || op2

When one of op1 and op2 is true, it returns true; if the value of op1 is true, the operand on the right is not calculated.

!

! op

Return true when op is false; return false when op is true.

&

op1 & op2

Operation op1 and op2; if op1 and op2 are both boolean values ​​and are equal to true, then return true, otherwise return false; if op1 and op2 are both numbers, then perform bits and operations.

|

op1 | op2

Operation op1 and op2; if op1 and op2 are both boolean values ​​and one equals true, then return true, otherwise false; if op1 and op2 are both numbers, then perform bits or operations

^

op1 ^ op2

Operation op1 and op2; if op1 and op2 are different, that is, if one is true and the other is not, then return true, otherwise false; if op1 and op2 are both numbers, then perform the bit-exor operation

Short circuit
When using logic operators, a short circuit will be encountered. Once the value of the entire expression can be determined clearly and accurately, the rest of the expression will no longer be calculated. Therefore, the part behind the entire logical expression may not be calculated anymore. The following example shows the short circuit phenomenon
Copy the codeThe code is as follows:

public class ShortCircuit{
static Boolean test1(int val){
(“test1(“+val+")");
(“result:"+(val<1));
return val<1
}
static Boolean test2(int val){
(“test1(“+val+")");
(“result:"+(val<2));
return val<2
}
static Boolean test3(int val){
(“test1(“+val+")");
(“result:"+(val<3));
return val<3
}
public static void main(String [] args){
Boolean b=test1(0)&&test2(2)&&test3(2);
(“expression is “ + b);
}
}

Output
test1(0)
result:true
test(2)
result:false
expression is false
Since you call 3 methods, you will naturally feel that all 3 methods should be run, but in fact the output is not the case, because a false result was generated in the second test. Since this means that the entire expression is definitely false, there is no need to continue calculating the remaining expressions, it is just a waste. "Short circuit" comes from this once. In fact, logically all logical expressions do not have to be calculated, which will achieve performance improvements.
Triple operator
The ternary operator also becomes a conditional operator. It seems special because there are three operands, but it does belong to a type of operator.
Its form is
boolean-exp?value0 :value1
If the boolean-exp expression result is true, value0 is calculated, and this calculation result is the final value generated by the operator. If the boolean-exp expression result is false, value1 is calculated, and the same way, its result will become the last value of the operator.
Of course it can also be replaced by if-else, but the ternary operator is completely different from if-else, and the operator will produce a value.
Copy the codeThe code is as follows:

public class TernaryIfElse{
static int ternary(int i){
return i<10?i*100:i*10;
}
static int standardIfElse(int i){
if(i<10)
return i*100;
else
return i*10;
}
public static void main(String [] args){
(ternary(9));
(ternary(10));
standardIfElse( (9));
standardIfElse( (10));
}
}

Output
900
100
900
100
In comparison, ternary operators are much more compact, while if-else is easier to understand
String operators + and +=
In addition to the functions mentioned earlier, the + and += operators in Java can be used as character concatenation in a special context. So they are also called string operators, and string operators have some interesting behaviors. If the expression starts with a string, then all subsequent operands must be string-type (the compiler will automatically pass the characters in double quotes into strings):
Copy the codeThe code is as follows:

public class StringOperators{
public static void main(String [] args){
int x=0,y=1,z=2;
String s="x,y,z";
(s+x+y+z);
(x+""+s);
s+="(summed) =";
(s+(x+y+z));
(“"+x);
}
}

Output
x,y,z 012
0 x,y,z
x,y,z (summed) = 3
0
It should be noted that the first line outputs 012 instead of sum 3, because the compiler automatically converts them into string form, and finally uses += to append a string to s, and uses brackets to control the compiler's conversion, so that they can sum it smoothly.