一、包装类的使用
* 1.java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征
* int Integer
* float Float
* double Double
* byte Byte
* long Long
* short Short
* char Character
* boolean Boolean
* 2.掌握:基本数据类型、包装类、String三者之间的相互转换
*
*/
public class WrapperTest {
@Test
public void test5(){
String str1 = "123";
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
@Test
public void test4(){
int num1 = 10;
String str1 = num1 + "";
float f1 = 12.3f;
String str2 = String.valueOf(f1);
System.out.println(str2);
Double d1 = new Double(12.45);
String str3 = String.valueOf(d1);
System.out.println(str3);
}
@Test
public void test3(){
int num2 = 10;
Integer in1 = num2;
boolean b1 = true;
Boolean b2 = b1;
System.out.println(in1.toString());
int num3 = in1;
}
public void method(Object object){
System.out.println(object);
}
@Test
public void test2(){
Integer in1 = new Integer(12);
int i = in1.intValue();
System.out.println(i);
Float f1 =new Float(1.1);
float f = f1.floatValue();
System.out.println(f + 1);
}
@Test
public void test1(){
int num1 = 10;
Integer in1 = new Integer(num1);
System.out.println(in1.toString());
Integer in2 = new Integer("123");
System.out.println(in2.toString());
Float f1 = new Float(12.3f);
Float f2 = new Float("12.3");
System.out.println(f1.toString());
System.out.println(f2.toString());
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("TrUe");
Boolean b4 = new Boolean("true123");
System.out.println(b4.toString());
Order order = new Order();
System.out.println(order.isMale);
System.out.println(order.isFemale);
}
}
class Order{
boolean isMale;
Boolean isFemale;
}
二、示例
public class InterviewTest {
@Test
public void test1() {
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);
}
@Test
public void test2() {
Object o2;
if (true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2);
}
@Test
public void test3() {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);
Integer m = 1;
Integer n = 1;
System.out.println(m == n);
Integer x = 128;
Integer y = 128;
System.out.println(x == y);
}
}