Java基础--基本数据类型转换

public static void main(String[] args) {

		// 基本数据类型---〉引用数据类型 使用包装类的构造方法
		Integer i = new Integer(100);
		Double d = new Double(100);

		System.out.println(i);// 100
		System.out.println(d);// 100.0

		// 引用数据类型---〉基本数据类型 intValue()方法 去引用类型的各种值
		// int java.lang.Number.intValue()
		System.out.println(i.intValue());
		System.out.println(i.floatValue());// 100.0
		System.out.println(i.doubleValue());// 100.0
		System.out.println(i.longValue());
		System.out.println(i.byteValue());
		System.out.println(i.shortValue());

		// 字符串---〉基本类型变量
		// public static int parseInt(String s, int radix)
		double d1 = Double.parseDouble("1323");
		double d2 = Double.valueOf("123").doubleValue();

		try {
			int i1 = Integer.parseInt("12.132");
		} catch (NumberFormatException a) {
			System.out.println("字符串格式不正确");
		}

		// 基础数据类型---〉字符串
		// public static String toString(int i)
		String str = Integer.toString(12);
		String str1 = 12 + "";

		// 基础数据类型包装类---〉字符串
		String str2 = i.toString();
		String str3 = new Double(21.32).toString();// 字符串21.32

		// 字符串---〉基础数据类型包装类
		Integer ii = new Integer("123");
		try {
			Integer ii1 = new Integer("123.1");//输出:字符串格式不正确
		} catch (NumberFormatException a) {
			System.out.println("字符串格式不正确");
		}

		//进制转化
		System.out.println(Integer.toBinaryString(123) + "B");//1111011B
		System.out.println(Integer.toHexString(123) + "H");//7bH
		System.out.println(Integer.toOctalString(123) + "O");//173O
		
		/*
		 * 引入了自动拆装箱的语法,也就是在进行基本数据类型和对应的包装类转换时,系统将自动进行,将大大方便程序员的代码书写
		 * int类型会自动转换为Integer类型 int i = 12; Integer in = i;
		 * Integer类型会自动转换为int类型 int n = in;
		 */
		//String---->int,Integer
		String strr = "2132";
		Integer ii2 = new Integer(strr);
		int i2 = Integer.parseInt(strr);
		int ij = Integer.valueOf(strr).intValue();
		
		
		
		//int,Integer---->String
		int j = 12;
		String strr1 = Integer.toString(j);
		Integer jj = new Integer(12);
		String strr2 = jj.toString();
		
		//int--->Integer,String
		int c = 12;
		Integer ci = new Integer(c);
		String si = Integer.toString(c);
		
		//Integer,String--->int
		Integer vb = new Integer(12);
		String ss= "12";
		int  js = vb.intValue();
		int js1 = Integer.parseInt(ss);
		int js11 = Integer.valueOf(ss).intValue();
		
		//Integer--->int,String
		Integer dd = new Integer(21);
		int ik = dd.intValue();
		String ssc = dd.toString();
		
		//int,String--->Integer
		int g = 12;
		String sj = "21";
		Integer jj1 = new Integer(g);
		Integer jj2 = new Integer(sj);
		
		
		//int <---> Integer
		//已进行过处理
		Integer k = 12;
		int l = new Integer(12);
		
		
		//int <-->Integer
		int a = 123;
		Integer b = 32 ;
		a = b.intValue();
		b = new Integer(a);
		
		//int <-->string
		int a1 = 32;
		String cc = "232";
		cc = Integer.toString(a1);
		a1 = Integer.parseInt(cc);
		a1 = Integer.valueOf(a1).intValue();
		
		//string   <-->Integer
		String s1 = "32";
		Integer b1 = 342;
		s1 = b1.toString();
		b1 = new Integer(s1);
		

	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值