java 鄙视位移题_java 位移问题

先给你解释下代码中的概念吧:

public class ToBinary {    //java的一个.java文件只有一个public修饰符,ToBinary必须和文件名一致,

//即文件名必须为ToBinary.java

public static void main(String[] args) {    printBinary(256);    printBinary(257);   //print不是保留字,printBinary是方法调用,调用的是下面你写的static方法

printBinary(1);      }

/* 该方法将整型参数i以二进制的形式输出,方法没有返回值*/    static void printBinary(int i) {    System.out.print(i + "的2进制数表示为:\t");    for(int j = 31; j >=0; j--)    if(((1 << j) &  i) != 0)    //呵呵,移位不懂可以看看数字逻辑关于移位的解释,这里就不解释了

System.out.print("1");    else    System.out.print("0");    System.out.println();//换行    }}

另外写了点东西,希望对你学习类型转换有用:(以下代码可以直接copy,文件名为NumberClass.java即可!)

/**

* 数字的封装类

* 为了满足用户可能会需要以对象的方式操作基本类型,因此,

* Java为每种基本数据类型都定义了相应的封装类。

* byte --> Byte; short --> Short; int --> Integer

* long --> Long; float --> Float; double --> Double

* boolean --> Boolean; char --> Character

*/

public class NumberClass {

/** 基本类型到封装类型的转换:以基本类型的数据为参数new一个相应封装类的对象

* 封装类型到基本类型的转换:返回封装类型对象的相应的value值。 */

/**

* byte类型数字转换成Byte类型对象

*/

public static Byte byte2Byte(byte b){

//return Byte.valueOf(b);

return new Byte(b);

}

/**

* Byte类型对象转换成byte类型数字

*/

public static byte Byte2byte(Byte B){

if (B == null) {

return 0;

} else {

return B.byteValue();

}

}

/**

* int类型数字转换成Integer类型对象

*/

public static Integer int2Integer(int i){

// return Integer.valueOf(i);

return new Integer(i);

}

/**

* Integer类型对象转换成int类型数字

*/

public static int Integer2int(Integer integer){

if (integer == null) {

return 0;

} else {

return integer.intValue();

}

}

//其他基本类型与封装类型的相互转换都符合这个规则,这里就不一一列出了

public static void main(String[] args) {

int i = 5;

Integer I = int2Integer(i);

//将int类型转换成Integer之后,可以变成字符串

String iStr = I.toString();//将int类型转换成Integer之后,可以变成字符串

Integer a = new Integer(5);

Integer b = new Integer(10);

//Integer对象本身不能进行加减乘除的运算,必须使用它的int值进行运算

int sum = a.intValue() + b.intValue();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值