JAva演算
8303 ワード
// :( , 1), ( ,-1 10000001)。
// : , 。 。
//java byte short int long ,char
// :0 、1 。
// , 。 -5 = (10000101) = (01111010)
// : (^) + 1 , , (-)。
// : , , (^) + 1 。
//byte :-128 to 127, -128 to -1, 0 to 127, 2 n-1 。
//-128 10000000,^(10000000) = 01111111,01111111 + 1 = 10000000( ), 128 = -128。
//java :5 - 3 5 + (-3),00000101 + 11111101 = 00000010 ( ) = 2
//(byte)0x90 -> (byte)00000000000000000000000010010000 -> 10010000(byte ) -> -(10010000^ + 1) = -(01110000) = -112
System.out.println((byte)0x90); //-112
//Java 2 , 。 (int、long、byte short),
// , 。
//-128 = 10000000, -(-128) -> -(10000000) -> 10000000^ + 1 -> 10000000 = -128
i = Integer.MIN_VALUE;
System.out.println(i != 0 && i == -i); //true
// *=、/=、%=、+=、-=、<<=、>>=、>>>=、 &=、^= |= , int,
// 。
// :(short)-1 = 0xffff -> 0xffffffff -> 1 , 0 -> 0x7fffffff -> (short)0x7fffffff -> (short)0xffff = (short)-1
short i = -1;
while (i != 0) {
i >>>= 1;
}
// : int float、 long float、 long double。
// : ulp, “ (unit in the last place)” 。
// float double , ulp , 。
System.out.println((float)2000000000 == (float)2000000050); //true
//byte short char int , 。 。
// : int 128 byte , 。 byte bValue = 128;
// :int 2147483648 int , 。 long lValue = 2147483648;
byte bValue = 127;
long lValue = 2147483648L;
// int Integer.MAX_VALUE 。 int 。
//int 。 , 。
// , 。( byte、char、short、int long。)
// i Integer.MAX_VALUE, , Integer.MIN_VALUE。
//
for (int i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i++)
// :
for (long i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i++)
// 。
// :
// , 。
// T,T byte、short char, int ,
// T , T。
// , , 。
char x = 'X';
int i = 0;
System.out.println(true ? 88 : x); //X
System.out.println(true ? x : 65535); //X
System.out.println(true ? x : 65536); //88
System.out.println(false ? i : x); //88
System.out.println(true ? x : i); //88
// 。
// byte、short char 。
// , 。
// E1 op= E2 E1 = (T)((E1)op(E2)), T E1 。
int i1 = 123456;
short s = 0;
s += i1;
System.out.println(s); //-7616
// , 。
// , 。
// 。 , 。
// , 。
System.out.println(Long.toHexString(0x100000000L + 0xcafebabe)); //cafebabe
// long , L, l。
// l 。
// 1 , , l 。
// :System.out.println(12345+5432L);
System.out.println(12345+5432l); //17777
// , —— 。
// ,
// 。 , long 。
// int, int long , , 。
// : final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY); //5
// , float double。
// 。
// , double 。
// , int、long BigDecimal。
// BigDecimal(String) , BigDecimal(double)。
// “ ” 。
System.out.println(2.00 - 1.10); //0.8999999999999999
// , 。
// , i false
public static boolean isOdd(int i){
return i % 2 == 1;
}
//
public static boolean isOddCorrect(int i){
return i % 2 != 0;
}
// 、 。
// , == (binary numeric promotion)。
// (widening primitive conversion)。
// , :
// int long float , long double , 。
// == 。
long x = Long.MAX_VALUE;
double y = (double) Long.MAX_VALUE;
long z = Long.MAX_VALUE - 1;
System.out.print((x == y) + " " + (y == z) + " ");
System.out.println(x == z); //true true false
// , Java 。
// 。
//Java ,int long (-) 。
// -2147483648 Java , int 2147483648 。
// ( ) , -(2147483648), 。
int i = -2147483648;
// ,
//int i = -(2147483648);
//long j = -(9223372036854774808L);
// 0; 。
// , 。
System.out.println(012); //10
//Math.abs return (a < 0) ? -a : a;
// Integer.MIN_VALUE Long.MIN_VALUE , 。
// -Integer.MIN_VALUE = Integer.MIN_VALUE
System.out.println(Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE); //true
// , Integer.MAX_VALUE。
// int 。
Random rnd = new Random();
Integer[ ] arr = new Integer[100];
for (int i = 0; i < arr.length; i++)
arr[i] = rnd.nextInt();
// : 1/4 ,
/*
Comparator<Integer> cmp = new Comparator<Integer>() {
@Override public int compare(Integer i1, Integer i2) {
return i2 - i1;
}
};*/
//
Comparator<Integer> cmp = new Comparator<Integer>() {
@Override public int compare(Integer i1, Integer i2) {
int val1 = i1.intValue();
int val2 = i2.intValue();
return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));
// return (i1 < i2 ? -1 : (i1 > i2) 1 : 0);
}
};
Arrays.sort(arr, cmp);
// 5.0 , (autoboxing) (auto-unboxing) Java 。
//( :Byte、Character、Short、Integer、Long、Float Double。)
//Java (== !=) , ID , 。 5.0 。
// , , 。 5.0 , 。
// (>、 >=、 <、 <=):
// , :
// , 。
Integer obj1 = new Integer(0);
Integer obj2 = new Integer(0);
System.out.println(obj1 <= obj2 && obj1 >= obj2 && obj1 != obj2); //true