1.将按位运算操作,尤其是左移和右移(有无符号)整理到自己的博客中。
重点说明清晰负数的运算过程。
public static void main(String[] args) { //运算符演示 int a = 3; int b = 6; //+ - 符号 System.out.println("+a = " + (+a)); //3 System.out.println("-b = " + (-b)); //-6 //+ - * / % 算数运算符 System.out.println("算数运算符:"); System.out.println("a + b =" + (a + b)); //9 System.out.println("a - b = " + (a - b)); //-3 System.out.println("a * b = " + (a * b)); //18 System.out.println("a / b = " + (a / b)); //0 System.out.println("a % b = " + (a % b)); //3 System.out.println("--------------------我是分割线-------------------"); //+= -= *= /= %=赋值运算符 System.out.println("赋值运算符:"); System.out.println("a += b = " + (a += b)); //9 System.out.println("a -= b = " + (a -=b)); //3 System.out.println("a *= b = " + (a *= b)); //18 System.out.println("a /= b = " + (a /= b)); //3 System.out.println("a %= b = " + (a %= b)); //3 System.out.println("--------------------我是分割线-------------------"); //== != > < >= <= 比较运算符 System.out.println("比较运算符:"); System.out.println("a == b = " + (a == b)); //false System.out.println("a != b = " + (a != b)); //true System.out.println("a > b = " + (a > b)); //false System.out.println("a < b = " + (a < b)); //true System.out.println("a >= b = " + (a >= b)); //false System.out.println("a <= b = " + (a <= b)); //true System.out.println("--------------------我是分割线-------------------"); // | & ^ ! || &&逻辑运算符 boolean b1 = false; boolean b2 = true; System.out.println("b1 | b2 = " + (b1 | b2)); //true System.out.println("b1 & b2 = " + (b1 & b2)); //false System.out.println("b1 ^ b2 =" + (b1 ^ b2)); //true System.out.println("!b1 =" + (!b1)); //true System.out.println("b1 || b2 = " + (b1 || b2)); //ture System.out.println("b1 && b2 = " + (b1 && b2)); //false System.out.println("--------------------我是分割线-------------------"); //<< >> >>> | & ^ ~位运算符 int x = -9; //1111 0111 int y = 5; //0000 0101 System.out.println("x << 2 = " + (x << 2)); //-36 /* 有符号左移过程 1111 0111 11|11 011100 */ System.out.println("x >> 2 = " + (x >> 2)); //-3 /* 有符号右移过程 1111 0111 111111 01|11 */ System.out.println("x >>> 2 = " + (x >>> 2)); //... ... ... /* 无符号右移过程 1111 1111 1111 1111 1111 1111 1111 0111 0001111 1111 1111 1111 1111 1111 1111 0 | 111 */ System.out.println("x | y = " + (x | y)); //-9 /* ... 1111 0111 ... 0000 0101 -------------- 1111 0111 与运算 全0为0 其余为1。 */ System.out.println("x & y = " + (x & y)); //5 /* ... 1111 0111 ... 0000 0101 -------------- 0000 0101 或运算 全1为1 其余为0。 */ System.out.println("x ^ y = " + (x ^ y)); //-14 /* ... 1111 0111 ... 0000 0101 -------------- 1111 0010 异或运算 0、1为1 其余为0。 */ System.out.println("~x = " + (~x)); //8 /* ... 1111 0111 -------------- 0000 1000 取反运算 0变1,1变0。 */ }
2.byte数到十六进制字符串表现形式程序设计原理和实现方式整理到博客中。
{ public static void main(String[] args) { byte b = 5; System.out.println(convert(b)); } public static String convert(byte b) { int low = b & 0x0F; int high = (b >> 4) & 0x0F; char[] arr = {‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,}; return "0x" + arr[high] + arr[low] ; }
3.定义函数,计算一位整型数组所有元素的和。
public static void main(String[] args) { int X = Sum(new int[]{100,100,100,105}); System.out.println(X); } public static int Sum(int[] arr){ int sum = 0; for (int i = 0;i < arr.length ;i++ ) { sum += arr[i]; } return sum; }
4.数组的拷贝。
public static void main(String[] args){ int[] arr1 = {1,2,3,4,5}; int[] arr2 = new int [arr1.length]; arrCopy(arr1,arr2); for (int i = 0;i < arr2.length ;i++ ) { System.out.println(arr2[i]); } } private static void arrCopy(int[] arr1,int[] arr2){ for (int i = 0;i < arr1.length ;i++ ) { arr2[i] = arr1[i]; } }
5.堆内存默认是
----------------------------------------
堆内存默认大小是本机内存的1/4。
时间: 2024-10-14 11:14:36