Java-Preliminary Understanding-Chapter II-Arithmetic operators 1

Source: Internet
Author: User
Tags arithmetic operators

I. Overview operators

Some of the previous explanations are errors, in constants and variables that piece, around the ground is to assign constants to the variable, here is talking about the operation, the previous is not considered an operation. The main function of data is to compute. Here we will discuss the various operators used in the operation, mainly the arithmetic operators.

At this stage of learning, I learned two points: the computer operation is step by step, and after each step, there are rules. The default data type is always in effect.

Two. Arithmetic operators

The default data types in Java operations, which play an important role in arithmetic operations, will be different from the arithmetic in everyday life. The results are as follows:

According to daily habits, the result should be 6370, but the DOS display is 6000. The reason is that every step of the calculation, the default type of Java is in effect. After 6370/1000, the result should be 6.37, but in Java, the default result is int, which translates the result to 6,6 multiplied by 1000 and eventually to 6000.

In addition to arithmetic there is a modulo operation (take-up), the symbol is%, for example System.out.println (5%2);

here is a bit strange : in the output statement (), whether the output is a whole, or the results of the calculation? How to determine this.

Modulo operations are also extended: there are negative numbers involved in the modulo operation. If it is -5%2, then the result is-1; if 5%-2, then the result is 1. in the modulo operation, the result is positive or negative depending on the left side of the remainder, why?

When does the modulo operation work? Any number in modulo 2, not 0 is 1, which is used in the switching algorithm. (Switch algorithm is only two cases)

In addition to the modulo operation, there is also the connector +, which means that + can represent either a plus or a connector. In the front we also throw out this question, in the end how to determine whether the output is a whole, or the result of the operation? Here we have the results. The criterion is that if it is added directly, such as 3+2, then it is the output, if it is "2", then the output is a whole 32. Any data and strings are linked together, and the output is whole. There are + and strings, so the output is a string. For example, "5+5=" +5+5, the output of the result is 5+5=55, where the calculation, in fact, is divided into two parts. First "5+5=" +5 for the operation, the result is "5+5=5", then the operation "5+5=5" +5, the final result is 5+5=55. This shows that the calculation is a step-by-step. You can also deform it here, "5+5=" + (5+5); this adds a (), with a priority.

There is also a variant, int a=4, b=5; System.out.println (); If you want to output 4 and 5 separately, what should I do with this parenthesis? One of my own ideas is a + "B", so the output is 4b, indicating that, although in {}, B is a defined variable, but in (), "B" is the equivalent of a string, if the B equivalent variable, you can not add "" number. The correct wording is (A + "," +b "), the result of the output is 4, 5. The principle is that as long as there is a string, the whole is a string. → The string's connector is applied.

Java-Preliminary Understanding-Chapter II-Arithmetic operators 1

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.