Java (3) puzzle 24-35

Source: Internet
Author: User

This is the third part of Java's obfuscation. It is about the loop puzzle. In fact, the majority of loop fans come from the judgment conditions of the loop, because the general problems of the loop are also here, the result is often an endless loop or one cycle is not a loop. In fact, this chapter is quite abnormal, but it also prompts us whether we have learned carefully.

 

Puzzle 24 enjoy every byte
 
Class bigdelight {public static void main (string [] ARGs) {for (byte B = byte. min_value; B <byte. max_value; B ++) {If (B = 0x90) system. out. print ("Joy! ");}}}

What will be printed in this example? Naturally, Joy is printed! But actually = is not equal on both sides. First, this is a comparison between byte and integer. When byte is extended to an integer type, the value range is-128 to 127, but the value of 0x90 is 144. Therefore, the comparison is certainly not equal. This is also a problem caused by symbol extension. There are two solutions. One is to replace 0x90 with byte, so that the value must be between the maximum and minimum values of byte. The second method is to use (B & 0xff) to implement unsigned extension. The hybrid type is really harmful.

 

Puzzle 25 relentless incremental operations
Public ClassIncrement {Public Static VoidMain (string [] ARGs ){IntJ = 0;For(IntI = 0; I <100; I ++) J= J ++; System. Out. println (j );}}

At first glance, it may be regarded as 100, but the actual printing is 0. in fact, this issue was previously mentioned in the book C and pointer. The difference between the auto-increment operation P ++ and ++ P is that the former is to perform auto-increment after the value first, the latter is auto-increment first and then the value. The explanation in Java is that the value of the first expression is before the auto-increment operation is executed, and the value of the second expression is after the auto-increment operation. The statement is different. In this example, j = J ++ J is always assigned as the value before auto-increment, so J is always 0.

 

Puzzle 26 in the loop
Public   Class  Intheloop {  Public   Static   Final   Int End = Integer. max_value;  Public   Static   Final   Int Start = end-100 ;  Public   Static   Void Main (string [] ARGs ){  Int Count = 0 ;  For ( Int I = start; I <= end; I ++ ) Count ++ ; System. Out. println (count );}} 

This example is very interesting. First, we use a loop control condition that is less than or equal to, not strictly controlled. This is a small security risk, but it is not the key in this example, the key is this sectionCodeLoop the wireless network. The problem lies in integer. max_value, which represents the largest integer. The semantic meaning is that there is no larger integer than him. Then, when I is changed to the largest integer, add 1. Due to overflow, it will be integer. min_value. So the loop will continue to be executed. Be sure to take care of the boundary value. In fact, this operation is caused by <=.

 

Puzzle 27 unpredictable I value
Public class shifty {public static void main (string [] ARGs) {int I = 0; while (-1 <I! = 0) I ++; system. Out. println (I );}}

This example can give you some knowledge understanding. We know that the int value in Java is 32 bits, so the above example should be able to change-1 to 0 through the left shift after 32 loops. but in fact, it is impossible for any integer to remove all digits by shift. This is related to the implementation mechanism of shift operators. For the three shift operators <<>>>>>>>, when the left operand is 32 bits, the right operand is only 5 bits lower, that is, the actual range of the operand is 0--31. When the left operand is 64 bits, the right operand only takes 6 bits, that is, 0--63. therefore, this example will never succeed. This is a puzzle caused by insufficient knowledge points. The solution is to clear-1 by moving some digits multiple times. Very good puzzle.

 

Puzzle 28 loop Operator
 
//Place your declaration for I hereWhile(I = I + 1){}

The following several puzzle statements are about writing code. This requirement is to write the I statement so that this loop can go through an infinite loop. In fact, this idea is to use I as a very large number, so that + 1 cannot affect its value within the expressive range. We know that floating point numbers cannot accurately represent specific values. They are approximate values. When a number reaches a certain range, + 1 does not affect its approximate values. This value can be double. positive_infinity. Or, like double I = 1.0/0.0 in the book, float and double cannot be used to represent values accurately.

 

Puzzle 29 The Bride of the loop
 
//Place your declaration for I hereWhile(I! =I ){}

The I statement is given so that the above loop can go through infinite loops. In fact, I believe that those who have read JavaScript grammar books should be able to reflect that Nan (not a number) is not equal to itself ). It is not equal to any number, including itself. His intention is only to judge numbers.

 

Puzzle 30
//Place your declaration for I hereWhile(I! = I + 0){}

The I statement is given so that the following loop goes through an infinite loop. At the beginning, my idea was to implement data expansion, but I found that there was no way ,. Therefore, I cannot be a number. You can think of it as an object, but the + operator does not overload the object, so it is not. At this time, we should think of the string. The string reloads the + operator. I believe everyone knows the answer and uses the constant change of the string to implement an infinite loop.

 

Puzzle 31
 
//Place your declaration for I hereWhile(I! = 0) I>>> = 1;

The loop operator is not finished yet! Turn the above cycle into an infinite loop. Let's take a look at it. >>> it indicates the unsigned right shift, that is, the right shift of all BITs, regardless of plus or minus, and the highest bits are filled with zero. Looking back at the previous example, I have to change to zero. I don't know. Do you still remember the puzzle here? The starting point is here, >>> = the Left and Right support different data types with different widths and widths. Therefore, we define short I =-1 and-1 as 0 xFFFF here. Therefore, the I will first be extended to 32 characters to 0 xffffffff, shifted to the right, and then changed to 0x7fffffff. At this time, I will be assigned to the short type for truncation, I became oxffff again when I lost the 8-bit high. so this loop is complete. Great.

 

Puzzle 32 loop cursing
 
//Place your declarations for I and j hereWhile(I <= J & J <= I & I! =J ){}

The process is not complete yet. Continue to make the above loop an infinite loop. To satisfy the above features, we must first ensure that> = these relations are not called back. We know that in the basic data type, >=is an objection, that is, if a> = B & B> = A ---> A = B. But = is not always used to compare values. In Java object comparison, this is used to compare specific object references. The answer is the basic data type encapsulated in Java. Integer I = new INTEGER (0)

 

Puzzle 33
 
//Place your declaration for I hereWhile(I! = 0 & I =-I ){}

This is a bit difficult. No number except 0 has this feature. So we need to start with the data representation. I have no idea at all. Read Books honestly. First, we know that 32 bits can represent 2 ^ 32 numbers, where 0 is special. Because the binary complement mechanism is used, it has a unique representation for 0 to negative, or 0.0, then it is strange that the number of positive and negative numbers expressed by 2 ^ 32-1 is definitely not equal. It indicates that the positive and negative numbers of a number cannot be complete. This number is integer. min_value, 0x80000000. After taking the negative value, it is 0x7fffffff + 1 or itself. The answer is displayed. What caused this? Eventually, overflow occurs.

 

Puzzle 34 was knocked down by count
 
Public class count {public static void main (string [] ARGs) {final int start = 2000000000; int COUNT = 0; For (float F = start; F <start + 50; f ++) Count ++; system. out. println (count );}}

We saw a terrible floating point, so the result was definitely not as simple as we thought. Start is a very large integer. We know that float can only represent the precision of 24. See 2000000000 = 2*(2*5 ).. * (2*5) There are a total of 10 2, so the low 10 bits of F are all 0. in binary format, 50 requires only 6 digits. so this leads to 2000000000 and 2000000050 representing the same in float, because 31 digits are converted to 24 digits, and the last seven digits are discarded, but the last seven or eight digits are all 0, so it is meaningless. We recommend that you do not use floating point numbers as control variables.

 

Puzzle35 minutes
 
Public class clock {public static void main (string [] ARGs) {int minutes = 0; For (int ms = 0; MS <60*60*1000; Ms ++) if (MS % 60*1000 = 0) minutes ++; system. out. println (minutes );}}

This is obviously, MS % 60*1000 operator order problem, in fact, good programming habits can avoid this error, that is, the 60*1000 number defined as a constant, just fine.

 

The loop puzzle is more about the loop variable. In fact, like the expression puzzle, several of them are related to the data type. These numbers are magical. I think it took a lot of time to think of things like 1/2 to 0. These puzzle should be a wake-up for myself.

 

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.