Home > Java > javaTutorial > body text

How to use Java arithmetic operators

PHPz
Release: 2023-04-23 22:01:13
forward
1599 people have browsed it

1. , -, *, / are our commonly used arithmetic operators, representing addition, subtraction, multiplication and division.

In addition, we can also use remainder: %

System.out.println(1 + 2);
System.out.println(2 - 1);
System.out.println(1 * 2);
System.out.println(1 / 2);
System.out.println(1 % 2);
Copy after login

2. Division does not output 1/2=0.5. Why?

Since 1 and 2 are both integers, the expected result is 0.5 floating point, and Java performs integer division.

Below we declare that one of the parameters is a floating point type, allowing the compiler to automatically convert the type.

// 打印->0.5
System.out.println(1 / 2.0);
Copy after login

The above is the detailed content of How to use Java arithmetic operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template