2016-06-29
+ addition operation
-Subtraction
* Multiplication
/Division
% modulus
+ + self-add operation
--Self-subtraction operation
+ = Addition Assignment value
-=
*=
/=
%=
1 Basic arithmetic operators
Packagecom.java1995;/*** Basic arithmetic operators *@authorAdministrator **/ Public classTest { Public Static voidMain (string[] args) {//declaring 2 variables of type int intI1=7; inti2=2; System.out.println ("Basic operator demo, integer type"); System.out.println ("I1=" +i1+ "i2=" +i2); System.out.println ("I1+i2=" + (i1+i2)); System.out.println ("I1-i2=" + (i1-i2)); System.out.println ("I1*i2=" + (i1*i2)); System.out.println ("I1/i2=" + ((Double) i1/i2)); System.out.println ("I1%i2=" + (i1%i2)); //declaring 2 variables of type double Doubled1=8.0; Doubled2=2.5; System.out.println ("Basic operator demo, floating-point type"); System.out.println ("D1=" +d1+ "d2=" +D2); System.out.println ("D1+d2=" + (d1+D2)); System.out.println ("D1-d2=" + (d1-D2)); System.out.println ("D1*d2=" + (d1*D2)); System.out.println ("D1/d2=" + (d1/D2)); }}
2-mode operator
% is the remainder that divides 2 integers
Modulo operations are not recommended for floating-point types, and may result in inaccurate results
For high-precision operations, you need to use the BigDecimal type
Packagecom.java1995;/*** modulo operator *@authorAdministrator **/ Public classTest2 { Public Static voidMain (string[] args) {intI1=5; inti2=2; Doubled1=2.4; Doubled2=1.1; System.out.println ("Int i1=" +i1+ "i2=" +i2); System.out.println ("Double d1=" +d1+ "d2=" +D2); System.out.println ("I1%i2=" + (i1%i2)); System.out.println ("i1%d1=" + (i1%D1)); System.out.println ("D1%d2=" + (d1%D2)); System.out.println ("D1%i2=" + (d1%i2)); }}
Resources
[1] Java Easy Start Classic tutorial "full version"
Java section 12th Basic arithmetic operators and modulo operators