Dark Horse programmer-java basic learning Note 2

Source: Internet
Author: User

Dark Horse programmer-java basic learning Note 2
Dark Horse programmer-java basic learning notes 2 1. Notes Overview: keyword, identifier, comment, comment application, constant, hexadecimal origin, hexadecimal conversion, negative number hexadecimal, variable introduction, basic demonstration of variables, type enhancement & forced conversion, character type operation process, type operation details, Arithmetic Operators, value assignment operators. II. Introduction to common content: 1. Note content does not occupy memory. Even if you add more comments, the size of the hard disk occupied by the compiled class file remains unchanged. 2. Use forced conversion with caution. Precision is easy to lose and an error will be reported during compilation. Note that the compiler cannot pass when precision is involved. 3. the built-in java code table is a Unicode code table that can recognize Chinese and English. Iii. Typical examples and explanations: 1. analyze the causes of the output results after the following code is forcibly converted:

 
Package com. demo;/*** analyze the cause of the output after the forced conversion below */public class Demo1 {public static void main (String [] args) {byte B = 3; B = (byte) (B + 200); byte c =-3; c = (byte) (c-127); System. out. println (B); System. out. println (c) ;}}/*** symptom: Why is the output of 203 of the int type forcibly converted to the byte type: -53 * why is the-130 of the int type forcibly converted to the byte type? output: 126 ** principle: When the int type is converted to the byte type data, the system truncates the last 8 digits (the rightmost 8 digits) of the int value, * stores the value in the form of a complement code, and the highest digit is the symbol bit. Different operations are performed based on the symbol bit, because positive complement is itself. * If the maximum bit of the complement code starts with 0, it indicates that the intercepted value is a positive byte value, and the byte value is calculated normally in binary format. * If the maximum bit of the complement code starts with 1, it indicates that the truncated value is a negative byte value. * In addition to the symbol bit, the other parts first take the inverse and Add 1, then calculate the byte value corresponding to the binary, and add the negative number. ** 1. Why is the output of 203 of the int type forcibly converted to the byte type?-53 * because the binary value corresponding to 203 of the int type is 11001011 (the high value is omitted and the invalid value is 0, 8 digits. * During forced conversion, the system automatically intercepts the last 8 bits, that is, 11001011. Because the highest bit is 1, the calculation is based on the highest bit is 1. * The symbol bit is-* and the value * 1001011 is reversed: 0110100*0110100 plus-the corresponding value is 53. Then, the result is-53. ** 2. Why is the-130 of the int type forcibly converted to the byte type, and the output is 126 * because the binary value of the-130 of the int type is 11111111 11111111 11111111, the system automatically intercepts the last 8 bits, that is, 01111110. Because the maximum bit is 0, the maximum bit is 0. * Symbol bit: + (can be omitted without writing) * value: 1111110 is converted to a decimal value of 126. */
2. analyze the cause of the following code error and no error:
Package com. demo; public class Demo2 {public static void main (String [] args) {// 1. byte B = 3 + 7; byte b1 = 3; byte b2 = 7; // B = b1 + b2; System. out. println (B) ;}}/*** symptom: Why is byte B = 3 + 7? * Why cannot I compile B = b1 + b2? * Cause of error: * When the compiler compiles the program, byte B 3 + 7 does not report an error because * 3 and 7 are constants and the compiler knows that the result is 10, and within the byte range, * strong conversion is automatically performed, so no error is reported. * B = b1 + b2; both b1 and b2 are variables. The Compiler compiles the Program * in a row. It does not know what b1 and b2 are, when adding data of two byte types *, the data is first upgraded to the int type. Their sum is also int type, and its value may exceed the byte range *, because an error is reported. */
3. Analyze the following output results:
Package com. demo; public class Demo3 {public static void main (String [] args) {int x; int x1 = Integer. MAX_VALUE; // The maximum integer value int x2 = 3; x = x1 + x2; System. out. println (x) ;}}/*** symptom: the output result is-2147483647, which obviously overflows and the compiler does not report an error. * Note: because the two variables of the int type are added together, they are still of the int type. Although the result overflows, no error is returned. * Java overflows data of the int type (the same for other types) without error handling during compiling and running. * When the data overflows, it automatically intercepts the last 32 bits of the binary result and remains as valid data. */
4. Think about the following output results:
Package com. demo; public class Demo4 {public static void main (String [] args) {System. out. println (5% 5); System. out. println (-5% 2); System. out. println (5%-2);}/*** output: * 0-1 1 Description: negative number is negative for positive number modulo. The modulo result of negative number of positive number pairs is positive. */
5. analyze the differences between the following two methods:
Package com. demo; public class Demo5 {public static void main (String [] args) {short s = 3; s + = 4; // method 1 // s = s + 4; // method 2 System. out. println (s = + s) ;}}/*** why does the first method not report an error while the second method returns an error? * Compare s + = 4; with s = s + 4. ** Method 1: * when executing the s + = 4 Statement, the compiler performs forced type conversion by default during compilation, that is, the * int type value is automatically converted to the short type data. ** Method 2: * It indicates that when the s = s + 4 Statement is executed, the compiler does not forcibly convert it during compilation by default. Therefore, s is the short * type, and 4 is the int type. When the s + 4 operation is executed, s will automatically convert to the int type, and then perform the add operation, the final * result is also of the int type. If the value is assigned to the s variable of the short type, the precision is definitely lost. In this case, a forced type conversion is required. * s = (short) (s + 4 );*/
6. Think about the features of the following operations
Package com. demo; public class Demo6 {public static void main (String [] args) {int x = 10; // float y = 1.2; float y = 1.2f; y = y + 10; system. out. println (y) ;}}/*** error cause: * The float (4-bit) type variable is assigned because the decimal number is of the double (8-bit) type by default, of course, the accuracy may be lost, but the compiler's review will inevitably fail. * Solution: Add f to the value to let the compiler know that the constant following is of the float type. ** Symptom: y = y + 10. Why is no error reported during compilation and running? * When values of the int and float types are added, the int type is first converted to the float type and then added. * The final result is still of the float type, so no error is reported. */







 

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.