C # operator binary arithmetic operator _c# Tutorial

Source: Internet
Author: User
Tags arithmetic logical operators rand first row
The assignment operator also has the same usage as the arithmetic operator, as mentioned in the Appendix: for example, to add 4 to X and then assign it to x, you can write x+=4.

Copy Code code as follows:

public class mathops{
public static void Main (String [] args) {
Random rand=new Random (47);
int i,j,k;
J=rand.nextinf (100) +1;
System.out.println ("J:" +j);
K=rand.nextint (100) +1;
System.out.println ("K:" +k);
I=j-k;
System.out.println ("J-k:" +i);
i=k/j;
System.out.println ("k/j:" +i);
I=k*j;
System.out.println ("K*j:" +i);
J%=k;
System.out.println ("i%=k:" +j);
float u,v,w;
V=rand.nextfloat ();
System.out.println ("V:" +v);
W=rand.nextfloat ();
System.out.println ("W:" +w);
U=v+w;
System.out.printlln ("v+w:" +u);
U=v-w;
System.out.println ("v-w:" +u);
U=v*w;
System.out,print ("V*w" +u);
u=v/w;
System.out.print ("v/w:" +u);
U+=v;
System.out.print ("U+=v:" +u);
U-=v;
System.out.print ("U-=v:" +u);
U*=v;
System.out.print ("U*=v:" +u);
U/=v;
System.out.print ("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
By random the object of the class, the program can generate many different types of random numbers. The procedure is simple, you just call Method Nextint () and Nextfloat () (or Nextlong () or nextdouble ()). The parameter passed to Nextint () sets the upper limit of the random number produced, and the line is 0, but the line is not what we want, because he will produce a 0 probability, so do +1 of the operation.
Unary operator compiler will automatically judge the function of +,-number
For example: X=a*-b compiler can compile its meaning B before-number is minus but in order to avoid confusing the reader, the best formulation is
x=a* (-B)
The unary minus sign is used to transform the symbol of the data, and the unary Plus is only to correspond to a unary minus sign, but its only function is to elevate the smaller type of operand to int;
arithmetic operator

Operator

Use

Description

+

OP1 + OP2

Return to OP1 and Op2 's and

-

Op1-op2

Returns the difference between OP1 and OP2

*

OP1 * OP2

Returns the product of OP1 and OP2

/

Op1/op2

Returns the quotient of OP1 divided by op2

%

Op1% OP2

Returns the remainder of OP1 divided by op2

Auto Increment and decrement
Two fairly good and fast operations (often referred to as "AutoIncrement" and "auto decrement" operations) in the calculation of incremental and descending operations in Java that are provided in a sizing operation. Among them, the descending operator is "--", the meaning of private matter reduced by one unit, the increment is "+ +" means to add a unit
++a equivalent to a=a+1 diminishing and also
Increment, the descending is divided into "prefix" and "suffix" type.
For prefixes: ++a,--A, the operation is performed first, and then the value is generated. And for the suffix type a++,a--, Mr will be a value in the execution of the operation.
Copy Code code as follows:

public class autoinc{
public static void Main (String [] args) {
int i=1;
System.out.println ("I:" + 1);
System.out.println ("++i:" + ++i);
System.out.println ("i++:" + i++);
System.out.println ("I:" +-I.);
System.out.println ("i--:" + i--);
}
}

Output
I:1
++i:2
I++:2
--i:1
I--:1
From the example above, you can read the difference between a prefix and a suffix.
The increment operator is precisely the name of C + + an explanation, "beyond the C step"
Relational operators
Relational operators are primarily generated by a Boolean Boolean result that compares the result of a value to return TRUE if it is true, False if False, and the relational operator includes <,>,<=,>=,==,!= (not equal). equal to and not equal to all basic data types, while other operators do not fit the operation of Boolean values because there is no relationship greater than or less than two between true and false
Copy Code code as follows:

public class equivalence{
public static void Main (String [] arg) {
Integer n1=new integer (47);
Integer n2=new integer (47);
System.out.println (N1==N2);
System.out.println (N1!=N2);
}
}

Output
False
True
The result may not be the same as you think, although the object content is the same, but the object's reference is different, and = = and! = The reference to the object, not the content, is compared.
What do you do if you compare the actual content of two objects? You need to use the Equals () method that all objects apply to. But this method does not apply to basic types, the basic type directly uses ==,! = Can
Copy Code code as follows:

public class equalmethod{
public static void Main (String [] args) {
Integer n1=new integer (47);
Interger n2=new Integer (47);
System.out.println (N1.equals (N2));
}
}

Output
True
But if the object you are referencing is a class that you created, the result will be different
Copy Code code as follows:

Class value{
int i;
}
public class equalsmethod2{
public static void Main (String [] args) {
Value v1=new value ();
Value v2=new value ();
v1.i=v2.i=100;
System.out.println (V1.equals (v2));
}
}

Output
False
The result is false, because the default of equals is actually a comparison reference. So unless you rewrite the Equals method in your new class, you can't achieve what we expect.
Relational operators

Operator

Use

Description

>

OP1 > OP2

Returns True when OP1 is greater than OP2

>=

OP1 >= OP2

Returns True when OP1 is greater than or equal to OP2

<

OP1 < OP2

Returns True when OP1 is less than OP2

<=

OP1 <= OP2

Returns True when OP1 is less than or equal to OP2

==

OP1 = = Op2

Returns True when OP1 equals greater than OP2

!=

OP1!= OP2

Returns True when OP1 is not equal to OP2

logical operators
logical operators with (&&), or (| |), NON (!) Can return a Boolean value based on the relationship between parameters
Copy Code code as follows:

public class bool{
public static void Main (String [] args) {
Random rand=new Random (47);
int I=rand.nextint (100);
int J=rand.nextint (100);
System.out.println ("i=" + i);
System.out.println ("j=" + j);
System.out.println ("I > J is" + (I>J));
System.out.println ("I < J is" + (I<J));
System.out.println ("I > =j is" + (I>=J));
System.out.println ("I <=j is" + (I<=J));
System.out.println ("I ==j is" + (I==J));
System.out.println ("I!=j is" + (I!=J));
System.out.println ("(I <10) && (j<10) is" + ((I <10) && (j<10));
System.out.println ("(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
A and or a non-operation can only be applied to a Boolean value, and if a Boolean is used where it should be a string value, the Boolean value is automatically 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

Returns True when both OP1 and OP2 are true, and if the value of OP1 is false, the right-hand operand is not calculated

||

OP1 | | Op2

Returns True when OP1 and OP2 have one is true; if OP1 value is true, the right-hand operand is not calculated

!

! Op

Returns True when the OP is false, or false when OP is True

&

OP1 & OP2

Operations OP1 and OP2; if both OP1 and OP2 are Boolean and equal to True, returns true, otherwise false; if both OP1 and OP2 are numbers, then the execution bit and the operation

|

OP1 | Op2

Operations OP1 and OP2; if both OP1 and OP2 are Boolean values and one is equal to True, returns true, otherwise false; if both OP1 and OP2 are numbers, the execution bit or action

^

OP1 ^ OP2

Operations OP1 and OP2; if OP1 and OP2 are different, that is, if one is true and the other is not, return true, otherwise return false; if both OP1 and OP2 are numbers, perform a bitwise XOR OR operation

Short Circuit
A short-circuit condition is encountered when using the logical operator. The remaining part of the expression is no longer evaluated when the value of the entire expression is clearly and unambiguously determined. Therefore, the rest of the entire logical expression may no longer be counted. The following example shows a short circuit phenomenon
Copy Code code as follows:

public class shortcircuit{
static Boolean test1 (int val) {
System.out.println ("Test1" ("+val+"));
SYSTEM.OUT.PRINTLN ("Result:" + (val<1));
Return val<1
}
static Boolean test2 (int val) {
System.out.println ("Test1" ("+val+"));
SYSTEM.OUT.PRINTLN ("Result:" + (VAL<2));
Return val<2
}
static Boolean test3 (int val) {
System.out.println ("Test1" ("+val+"));
SYSTEM.OUT.PRINTLN ("Result:" + (val<3));
Return val<3
}
public static void Main (String [] args) {
Boolean b=test1 (0) &&test2 (2) &&test3 (2);
System.out.println ("expression is" + B);
}
}

Output
Test1 (0)
Result:true
Test (2)
Result:false
Expression is False
Since 3 methods are invoked you will naturally feel that 3 methods should all be run, but the output is not actually the case, because the second Test produces a false result. Since this means that the entire expression must be false, there is no need to continue to compute the remaining expression, which is just a waste. "Short circuit" is coming from this. In fact, all logically logical expressions have a part that does not have to be counted, and that gains performance.
ternary operator
The ternary operator also becomes a conditional operator, and he appears to be more special because there are three operands, but he does belong to an operator.
Its form as
Boolean-exp?value0:value1
If the BOOLEAN-EXP expression evaluates to True, the VALUE0 is computed and the result is the resulting value of the operator. If the BOOLEAN-EXP expression evaluates to False, the value1 is computed, and likewise his result becomes the final value of the operator.
Of course it can also be replaced by If-else, but the ternary operator and If-else are completely different, operators will produce a value.
Copy Code code 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) {
System.out.println (Ternary (9));
System.out.println (Ternary (10));
System.out.println Standardifelse ((9));
System.out.println Standardifelse ((10));
}
}

Output
900
100
900
100
In contrast, ternary operators are much more compact and if-else easier to understand.
string operators + and + =
The Java + + + + operator can be used as a character connection in a particular context, in addition to the previously mentioned function. So they are also called string operators, and string operators have some interesting behaviors. If the expression starts with a string, all subsequent operands must be of a string type (the compiler automatically converts character sequences in double quotes into strings):
Copy Code code as follows:

public class stringoperators{
public static void Main (String [] args) {
int x=0,y=1,z=2;
String s= "X,y,z";
System.out.println (S+X+Y+Z);
System.out.println (x+ "" +s);
s+= "(summed) =";
System.out.println (S+ (x+y+z));
System.out.println ("" +x);
}
}

Output
X,y,z 012
0 x,y,z
X,y,z (summed) = 3
0
It should be noted that the first row output is 012 rather than sum 3, because the compiler automatically converts them to a string form, and the last part appends a string to s with + +, and uses parentheses to control the conversion of the compiler so that they can sum up smoothly.
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.