Understanding java-2.2 arithmetic operators from the beginning

Source: Internet
Author: User
Tags arithmetic operators

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

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.