A summary of the mystery of expression

Source: Internet
Author: User
first, odd-numbered expression problem

Question: We often think that i%2==1 can judge whether it is odd.

Analysis:

(1) modulo operator (%): When the remainder operation returns a non-zero result, this result must have the same symbol as the left operator.

Example: 3%2=1;    -3%2=-1; Corresponding to the previous definition, because the left operator is-3, the result should also be negative.

(2) Preliminary results: To determine whether it is odd by i%2==0.

(3) Bit operation: bit operation is much faster than general arithmetic operation. The lowest digit of the average even binary is 0, regardless of the original code or complement.

Final result: (i&1). =0

Routines:

Import Java.util.Scanner;
public class puzzledemo01{public
	static void Main (String args[]) {
		Scanner in =  new Scanner (system.in);
		System.out.print ("Enter an integer:"); 
		int a = In.nextint ();
		System.out.println ("");
		System.out.println ("I%2==1-->" +isodd1 (a));
		Long begin1 = System.nanotime (); 
		System.out.println ("I%2!=0-->" +isodd2 (a));
		Long begin2 = System.nanotime (); 
		System.out.println ("I&1!=0-->" +isodd3 (a));
		Long begin3 = System.nanotime (); 
		System.out.println ("IsOdd2 Time:" + (begin2-begin1) + "nano");
		System.out.println ("IsOdd3 Time:" + (begin3-begin2) + "nano");
		
		
	}
	public static Boolean isOdd1 (int a) {return
		a%2==1
	}
	public static Boolean isOdd2 (int a) {return
		a%2!=0
	}
	public static Boolean isOdd3 (int a) {return
		(a&1)!=0
	}}
/*
Test results:
Enter an integer: -3

i%2==1-->false
i%2!=0-->true
i&1!=0
-->true ISODD2 operation time: 378819 Nano
isOdd3 time: 368762 Nano/


the problem of precision of two or two binary floating-point operation

Question: What is the result of the 2.00-1.10?

Analysis:

(1) It seems to be clear that it is 0.90, but in fact it outputs 0.899999. Reason: 1.10 cannot be expressed as a double in precision.

(2) Double and float are therefore not suitable for accurate calculations.

(3) Java.math.BigDecimal is used for accurate calculation.

(4) A constructor with a bigdecimal (String) instead of a bigdecimal (double), because the latter is unpredictable.

Final Result:

New BigDecimal ("2.00"). Subtract (New BigDecimal ("1.10"));

Import Java.math.BigDecimal;
public class puzzledemo02{public
	static void Main (String args[]) {
		System.out.println ("******* General floating-point operation: * * * **"); 
		System.out.println (2.00-1.10);	0.8999999999999999
		BigDecimal d1 = new BigDecimal ("2.00");
		BigDecimal D2 = new BigDecimal ("1.10");
		BigDecimal result = D1.subtract (D2);
		System.out.println ("******* after using full precision: ********"); 
		SYSTEM.OUT.PRINTLN (result);		0.90

	}
}


third, default type problem

Question: What is the result of long a = 24*60*60*1000*1000?

Analysis:

(1) By default, the type of an integer is int, and if it is long, l should be added.

(2) The above operation will be the multiplication of int first, and then the result of multiplication is converted to a long type, which does not avoid overflow.

(3) int is 4 bytes, so it can represent -231-1~231, and the above operation is far beyond this range, so overflow.

Final Result:

Long a = 24l*60*60*1000*1000;

public class puzzledemo03{public
	static void Main (String args[]) {
		long micros_per_day_original = 24 * 60 * 60 * 1 * 1000;
		
		Long millis_per_day_original = * * 1000;
		SYSTEM.OUT.PRINTLN ("Initial result:"
						+micros_per_day_original/millis_per_day_original);//5
		Long micros_per_day = 24L * * 1000 * 1000;
		Long Millis_per_day = 24L * * 1000;
		SYSTEM.OUT.PRINTLN ("Corrected result:"
						+micros_per_day/millis_per_day);	1000
		System.out.println ("Results of overflow   --> *  1000 * 1000=" +micros_per_day_original); 500654080
		System.out.println ("No overflow result--> 24L * *  1000 * 1000=" +micros_per_day);  86400000000
		
	}
}


Four, the long integer representation question L or L.

Because in general 1 and L are similar, for example:


Final Result:

(1) Use L when representing a long type of number.

(2) You cannot represent a variable by using L (lowercase l).

public class puzzledemo04{public
	static void Main (String args[]) {
		System.out.println ("12345+5432l=" + (12345+ 5432L));
		System.out.println ("12345+54321=" + (12345+54321)); 
		System.out.println ("12345+5432l=" + (12345+5432l)); 
	}
/*
12345+5432l=17777
12345+54321=66666
12345+5432l=17777
* *

five, symbol extension and 0 extension

Question 1:long a = 0xcafebabe; how much is the hexadecimal of a?

Analysis:

(1) When the signed number is extended, a symbolic extension is required. For unsigned numbers (char), the 0 extension is used.

(2) 16 and octal int type when the highest bit is 1 o'clock, is a negative number, symbol extension is high fill 1.

Final Result:

The result is 0xffffffffcafebabe rather than 0x00000000cafebabe;

public class puzzledemo05{public
	static void Main (String args[]) {
		int a = 0xcafebabe;
		Long B = A;
		System.out.println (A + "-->" + integer.tohexstring (a));
		System.out.println (b + "-->" + long.tohexstring (b)); 
		Long C = 0x100000000l;
		System.out.println ("0x100000000l+0xcafebabe=" +long.tohexstring (c+b)); 
		Long d = 0xcafebabeL;
		System.out.println ("0x100000000l+0xcafebabel=" +long.tohexstring (c+d)); 
	}
/*
-889275714--> cafebabe
-889275714--> ffffffffcafebabe
0x100000000l+0xcafebabe=cafebabe
0x100000000l+0xcafebabel=1cafebabe
*


Question 2:int a = (int) (char)-1; The result is how much.

Analysis:

(1)-1 is of type int, converted to char, only need to truncate, get 0xffff.

(2) because char is unsigned, so 0 expands, then a=0x0000ffff, or 65535.

Final Result:

a=65535.

public class puzzledemo06{public
	static void Main (String args[]) {
		System.out.println ((int) (char) (byte)-1);
		int i =-1;
		char C = (char) i;
		Int J = c & 0xFFFF;		unsigned extended
		int j2 = c;				unsigned extended
		int k = (short) c;		Signed extended
		System.out.println ("unsigned extension 1:" +j);	
		SYSTEM.OUT.PRINTLN ("Unsigned extension 2:" +j2); 
		SYSTEM.OUT.PRINTLN ("Signed Extension 1:" +k); 
	}



Summarize:

(1) We need to annotate the extension problem of type conversion.

(2) The unsigned extension can also be done through a bitmask.

Like what:

Short S =-1;

int i = s&0xffff;  Since the 0xffff is an int type, s first expands the symbol to int, and then it is cleared with the high position. VI. rules for conditional expressions. :

Problem:

int i = 0;

True? The result of ' X ': i;

A? B:c

1. If B and C have the same type, then the types of B and C are returned.

2. If the type of an operand is t (Byte,int,char, etc.), and the other is a constant expression, the return type is T.

3. Otherwise, the type of the operand is promoted by binary numbers, and the types that are returned are the type B and C that are promoted by the binary system.

For example:

int i = 0;

True? ' X ': I; This is by Theorem 2, you can conclude that the result is an int type, so the result is the ASCII code of ' X ': 120;

Conclusion: Try to make the type B and c the same.

public class puzzledemo08{public
	static void Main (String args[]) {
		char x = ' x ';
		int i = 0;
		System.out.println (true? x:0);	X is the char type, 0 is a constant, so the char type
		System.out.println (False i:x) is returned;	I is an int variable, and ' x ' is a constant, so it returns int
		System.out.println (true?). ' x ': i);	I is an int variable, and ' x ' is a constant, so it returns int
		final int j = 1;
		System.out.println (true?x:j);		X is a char type, and J is a constant, so return the char type
	}

?: Differences in jdk1.4 and jdk1.5:

In 1.4, when the second operand and the third operand are reference types, the conditional operator requires a subtype that must be another.

In 5.0, when the second third operand is a reference type, the result returned is a two-type minimum public superclass.

Seven, x+=i and X=x+i are equal. No.

X+=i equivalence to x= (Typex) (x+i) shows that there is an implicit type conversion in the middle.

If x is the short type and I is the int type, then x+=i is equivalent to x= (short) (x+i);

Instead of x=x+i, there is no implicit conversion, so if the type of x is smaller than the width of type I, a x=x+i compilation error occurs and x+=i is not wrong.

public class puzzledemo09{public
	static void Main (String args[]) {short
	    x=0;
		int i=123456;
		x = i;		Legal
		System.out.println (x);
		x=0;
		x = (short) (x + i);	X=x+i illegal
		System.out.println (x); 
	}
X+=i is  equivalent to x= (Typex) (x+i);


But there is a limit to the compound assignment operator:

(1) Both the left operand and the right operand must be of the base data type or wrapper class

(2) If the type of the left-hand operand is a string, there is no limit to the right-hand operand.

(3) The left-hand operand cannot be another reference data type.

But X=x+i has no such restriction.

public class puzzledemo10{public
	static void main (string args[]) {
		string i = "abc";
		Object x = "CBA";
		x+=i;	Non-lawful
		x=x+i;	Legal
		System.out.println (x); 
	}
}


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.