package com.wj.demo04;
public class TestInteger {
public static void main(String[] args) {
fun11();
}
private static void fun11() {
//自动装箱 自动调用了ValueOf,缓存
Integer a=20;
Integer b=20;
//Value 不在-128 127之间 new Integer
Integer a3=2000;
Integer b3=2000;
Integer a1=new Integer(20);
Integer b1=new Integer(20);
Integer a2=new Integer(2000);
Integer b2=new Integer(2000);
int a4=20;
int b4=20;
int a5=2000;
int b5=2000;
System.out.println(a==b);//true
System.out.println(a3==b3);//false
System.out.println("--------------------");
System.out.println(a1==b1);//false
System.out.println(a2==b2);//false
System.out.println(a4==b4);//true
System.out.println(a5==b5);//true
}
//最长用就是将字符串类型转成基本或者包装类型 ,
private static void fun10() {
String str="123";
//字符串转成基本类型[把字符串类型转成数字类型]
int x=Integer.parseInt(str);
Integer y=Integer.parseInt(str);
//Integer y=Integer.valueOf(str);
}
//字符串转成包装类型
private static void fun8() {
Integer i=Integer.valueOf("10");
System.out.println(i);
}
//字符串转成基本类型
private static void fun7() {
boolean boo1=Boolean.parseBoolean("true");
boolean boo2=Boolean.parseBoolean("tRue");
boolean boo3=Boolean.parseBoolean("HHHHH");
boolean boo4=Boolean.parseBoolean("false");
System.out.println(boo1);
System.out.println(boo2);
System.out.println(boo3);
System.out.println(boo4);
int i=Integer.parseInt("10");
System.out.println(i);
}
//基本类型转成字符串类型
private static void fun6() {
String x=Integer.toString(10);
System.out.println(x);
}
private static void fun5() {
Integer i=new Integer(10);
//把包装类型的对象赋值基本类型,自动拆箱
// int a=i.intValue();
int a=i;
//直接将基本类型的数据赋值给了包装类型,自动装箱
// Intege j=Integer.valueOf(10);
Integer j=10;
}
private static void fun4() {
//把基本类型转成包装类型
Integer in=Integer.valueOf(11);
System.out.println(in);
}
//将包装类型转成基本类型
private static void fun3() {
//i就是包装类型
Integer i=new Integer(10);
//将包装类型 对象i 转换成基本类型x
int x=i.intValue();
}
private static void fun2() {
Integer i1=new Integer(11);
Integer i2=new Integer("12");
System.out.println(i2);
//s3不可以直接转成数字类型
Integer i3=new Integer("s3");
System.out.println(i3);
}
//使用Integer类中的一些常量
public static void fun1() {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.SIZE);//int类型有多少位
}
}
Java 小白 基本类型转化成 包装类型 与字符串类型
最新推荐文章于 2024-07-26 03:17:44 发布