A summary of the mysteries of circulation

Source: Internet
Author: User
Tags arithmetic
1. Problem with type conversion

Question: Byte B = (byte) 0x90; how much is equal to? -112

Analysis:

(1) int occupies 32 digits and byte is 8 bits

(2) Whereas 0x90 may appear to be expressed in 8 digits, but Byte is a -128~127 because it is a signed number, and 0x90 is 144, if byte, because the highest bit is placed, so it is-112.

Summarize:

Avoid comparisons or assignments of mixed types.

public class puzzlerdemo24{public
	static void Main (String args[]) {
		byte by = (byte) 0x90;
		System.out.println (by);
		for (byte b=byte.min_value;b<byte.max_value;b++) {
			if (b = (byte) 0x90) {
				System.out.println ("joy!"); 
			}
	}
}


2. The confusion of the increment operation

Question: Int j = J++;j equals how much. Original value

Analysis:

(1) j + + execution process: The value of J plus one, but return the original value.

(2) The process of J = j + +:

int tmp = j;

j = j+1;

j = tmp;

So J is still the same value.

Summarize:

Do not assign multiple values to the same variable in an expression.

public class puzzledemo25{public
	static void Main (String args[]) {
		int j = 0;
		for (int i=0;i<100;i++) {
			j + +;
		}
		System.out.println (j); 
	}


discussion of 3.int

(1) The value range of int: -231~231-1, so the value range is asymmetric.

(2) The maximum value in the Integer.MAX_VALUE:int, if integer.max_value+1 is equal to Integer.min_value, so it is not possible for integers to exceed this value.

(3) The minimum value in the Integer.MIN_VALUE:int, his negative number or the original value, unchanged.

(4) int is bounded, unlike float, where there is no boundary.

Characteristics:

(1) Integer.max_value+1==integer.min_value;

(2) If a number is near the boundary of an integer, it is best to use a long value.

(3) for a signed integer type, the negative value is always one more than the positive value.

public class puzzledemo26{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); 
	}
}


4. Discussion of floating point numbers

Problem:

Double I =double.positive_infinity;

Whether the i==i+1 returns TRUE or false.

Analysis:

(1) There is a gap between large floating-point numbers and their successors, and this interval can be large, and infinity is infinitely larger than the subsequent interval.

(2) A floating-point number that is large enough to add 1 does not change his value because 1 does not compensate for the gap between two floating-point numbers.

(3) ULP: The space between two floating-point numbers. Java.lang.Math.ulp (double D) can calculate how much D is ulp.

Summarize:

(1) binary floating-point arithmetic is just an approximation of the actual arithmetic.

(2) When a small floating-point number is added to a large floating-point number, the value of the large floating-point number may not change.

Characteristics of floating point numbers:

1. The use of floating point numbers can represent infinity, infinitesimal. positive_infinity and negative_infinity

2.double.nan: Not a number, so he is not equal to any numerical value, including his own

public class puzzledemo28{public
	static void Main (String args[]) {
		double d = double.positive_infinity;
		SYSTEM.OUT.PRINTLN ("Infinity Floating-point value interval is:" + Math.ulp (d)); 
		System.out.println ("D is equal to d+1:" + (d==d+1)); 
	}

public class puzzledemo29{public
	static void Main (String args[]) {
		double i = Double.NaN;
		while (i!=i) {
			System.out.println ("Continue execution.") "); 
		}
		System.out.println ("Stop.") "); 
	}
}


5. Discussion of shift operators

First, the question: -1<<32 for how much. -1

Analysis:

(1)-1 is an int value, so there are 32 bits.

(2) When the left-hand operand is type int, the 5 bits of the right operand, that is, the right operand must be less than 32, and when the left operand is a long type, the 6 bits of the right operand are intercepted, that is, the right-hand operand must be less than 64.

(3) If the right-hand operand of the above example is 32, the left operand is unchanged.

(4) If the right operand of the above example exceeds 32, the right operand is required.

Second, the question: -1<<-1 for how much. -1<<31

Conclusion:

(1) The right shift operator always acts as the right shift, while the left shift operator always acts as a left shift.

(2) The negative shift length changes to a positive shift length by retaining only a low 5 digits.

(3) using the shift operator, it is not possible to remove all bits of a value at once.

public class puzzledemo27{public
	static void Main (String args[]) {
		System.out.println (" -1>>32:" + (-1) >>32)); 
		System.out.println (" -1>>>-1:" + ( -1>>>-1));	Note the difference between the logical right SHIFT and the arithmetic right shift
		System.out.println (" -1>>-1:" + ( -1>>-1)); 
		
	}
/*
-1>>32:-1
-1>>>-1:1
-1>>-1:-1
* *
6. The Impossible Facts

Question: Is there any possibility that I<=j&&i>=j&&i!=j returns TRUE. i = new Integer (0), j = new Integer (0)

Analysis:

(1) The numeric type is certainly not possible.

(2) I!=j: If I,j is a base data type, it represents a value comparison and, if it is a reference data type, an object reference comparison.

(3) jdk5.0 new automatic packaging and unpacking features.

Conclusion:

When two operands are all wrapped numeric types, the numeric comparison operator performs a value comparison, and the contract operator performs a reference identity comparison. 7. "+" overload

Question: I==i+0? Not necessarily

Analysis:

(1) for string type, + represents a connection, so if I is a string, it represents a strings connection.

Conclusion:

The "+" operator has two functions:

(1) Numerical calculation.

(2) string concatenation.

public class puzzledemo30{public
	static void main (string args[]) {
		String i = "a";
		while (i!=i+0) {
			System.out.println ("execute.") ");
		}
		System.out.println ("Stop.") "); 
	}
}


8. Compound operator's attack again

Question: The value of i>>>=1 I may not change. possible.

Analysis:

(1) >>>= resolution: A>>>=b equivalent to A= (TYPEA) (A>>>B);

(2) Examples:

Short i =-1 i = 0xFFFF;

int j = i; j = 0xFFFFFFFF;

j>>>=1; j = 0x7fffffff;

i = (short) J;        i = 0xFFFF; No change.

public class puzzledemo31{public
	static void Main (String args[]) {short
		i =-1;
		while (i!=0) {
			i>>>=1;
		}
		System.out.println ("Stop.") "); 
	}
}


9. The comparison operator and the sentence operator of the wrapper class (= = or. =)

For the operator of the contract

(1) For the base type, a value comparison is represented.

(2) For reference data types, represents object reference comparisons, including packing classes such as integers.

Summarize:

when the two operands are all wrapped numeric types, the numeric comparison operator is the value comparison performed, and the contract operator performs a reference identification comparison.

If one is a numeric type that is wrapped, and the other is a basic type, the contract operator performs a value comparison.

public class puzzledemo32{public
	static void Main (String args[]) {
		integer i = new integer (1);
		Integer j = new Integer (1);
		while (i<=j && i>=j && i!=j) {}
		System.out.println ("Stop.") "); 
	}
}


10. Loss of precision three types of widening basic type conversion

(1) Int-->float:

(2) Long-->float:

(3) Long-->double:

public class puzzledemo34{public
	static void Main (String args[]) {
		final int START = 2000000000;
		System.out.println ((float) START) (MATH.ULP); 
		int count = 0;
		for (float f=start;f<start+50;f++) {
			count++
		}
		System.out.println (count); 
	}
}


10. Beware the number of demons.

The definition of the magic number: In some program code, programmers often refer to numeric constants or strings that appear in code but are not interpreted as magic numbers or magic numbers or magic strings.

Recommendation: Replace all the magic numbers with a properly named constant.

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.