Java section 12th Basic arithmetic operators and modulo operators

Source: Internet
Author: User
Tags arithmetic operators

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

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.