Java Basics Tips

Source: Internet
Author: User

first, the problem of mold

1. Negative modulus integer, The result is negative, example: -5%2 =-1;

2. Positive modulus negative number, The result is positive, example: 5% (-2) = 1;

3 negative modulus negative number, The result is negative, example: 5% (-2) =-1;

4.0 modulo any number result is 0, example: 0%5 = 0;

Ii. issues of self-increase and reduction

1.

Iii. Coercion Type conversion

1.system.out.println (' a ' +0), output = 97;

2. Any data that appears as integers, by default, is of type int

Such as: byte a= 4; 4 is an int type, but because during compilation, the compiler automatically determines whether 4 is within the byte range, and if it automatically discards the first three bytes, it is equivalent to automatically forcing the type conversion and converting int integer 4 to byte type.

3. Byte a=4,b=3,c;

c=a+b, compile error, because A and B although is two byte variable, but in the a+b summation, the compiler will automatically convert the byte type data to int type, So the result of A+B will be an int type, and an int type will be assigned to byte, it will be an Error. Because two types of bytes are different, int is 4 bytes, byte is a byte, the character is 2 bytes char ch = ' You ' is right, because a kanji is two bytes.

Summary: 1. The two variables are added, the type is promoted first, then the operation, and then the result of the operation is assigned to a Value.

2. Add the two constants, calculate the constant value first, and then determine if the type range is satisfied, and then assign the Value.

Iv. self-increase/reduction Issues

1. Byte i=0,j;

i=i++; output I equals 0; first, the system allocates a temporary buffer, assuming that it is temp, first temp=i=0, then I self-increment to 1, and then i=temp, so I results in 0 instead of 1.

i=++i; output I equals 1; first, i increment to 1, then the system allocates a temporary buffer, assuming temp,temp=i=1, then i=temp, so I results in 1 instead of 0.

five, Assignment operation

1.byte i=3,j;

i+=1; and i = i+1 difference, 1) i = i + 1; error, and i+=1; no, because I +=1 is equivalent to i = 1, the system will determine whether 1 is in the byte range, is a calculation performed, and i = i+1; it is two times Calculated. There are different problems with Bytes.

2) + = automatically converts the byte type, while the left side of = is automatically type-promoted, which becomes int. So one pass, one cannot pass.

3) i + = j; also, There is no error, because the compiler internally for the + = default will have a byte conversion, will be J conversion to byte Type.

six, Minor Problems:

1. ' a ' +0 = 97, The compiler automatically turns the character into a numeric value of type int, that is, ' a ' becomes an integer 97.

2. ' 7 '-' 0 ' = 7

3. Negative numbers become binary numbers, that is, the absolute value of negative numbers of binary number is reversed plus 1, negative number of the binary highest bit is 1, two positive is 0;

Java Basics Trivia

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.