进制转换 class Transfer { publicstatic void main(String[] args) { tobin(6); tohex(60); toba(60); } //二进制转换 publicstatic void tobin(int num) { trans(num,1,1); } //十六进制转换 publicstatic void tohex(int num) { trans(num,15,4); } //八进制转换 publicstatic void toba(int num) { trans(num,7,3); } //转换函数 publicstatic void trans(int num,int base,int offset) { if(num==0) { System.out.println(0); return; } char[]chs={'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; char[] arr=new char[32]; intpos=arr.length; while(num!=0) { inttemp=num&base; arr[--pos]=chs[temp]; num=num>>>offset; } for(int x=pos;x<arr.length ;x++ ) { System.out.print(arr[x]); } } } 体会:java基本数据类型默认值boolean false char null byte 0 short 0 int 0 long 0L float 0.0f double 0.0d
即:java中基本类型的默认值是0,引用类型会默认为null。
|