This section to talk about arithmetic operators
1. Automatic conversion of the result type.
Package Com.ray.ch01;public class Test {public static void main (string[] args) {int a = 2, b = 3;int c = A/b;int D = b/ A System.out.println (c); System.out.println (d);}}
Looking at the code above, the b/a result is 1.5, which is obviously a float type, but since d is int, it is converted directly to int, and the fractional part is removed directly during the conversion process.
2. The variable or constant inside the expression is automatically transformed, and is transformed to the right, to a type that can accommodate larger data.
Transformation direction: Byte->shot (char)->int->long>float->double
Package Com.ray.ch01;public class Test {public static void main (string[] args) {byte A = 2, b = 3;int c = A/b;int D = b /a;//a=c+d;//can only be converted upward, can not be converted downward, here error System.out.println (c); System.out.println (d);}}
From the code can see, d=b/a no problem, but A=c+d really error, this is the type conversion problem.
3. One dollar plus one yuan minus
One yuan minus: the value is reversed. For example-(+1) =-1,-(-1) =1
Unary Plus: Only the type is promoted to int, which does not work for data that is greater than the int type.
Here is the code example:
Package Com.ray.ch01;public class Test {public static void main (string[] args) {byte A = 2;double c = 2.1;int B = 1 *-a;/ /equivalent to int b = 1* (-a) Double D = 2 *-C; System.out.println (GetType (+a)); System.out.println (GetType (+d)); System.out.println (b); System.out.println (d);} public static String GetType (Object o) {return O.getclass (). toString ();}}
Output:
Class Java.lang.Integer
Class Java.lang.Double
-2
-4.2
From the above example we can see that 1*-a this in the compiler is set up, so we for readability, generally will be written 1* (-a)
Summary: In this chapter we discuss three points of note for arithmetic operators.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-2.2 arithmetic operators from the beginning