zoukankan      html  css  js  c++  java
  • String 基本数据类型 包装类 三者的转换


    public class WrapperTest {

    //String类--->基本数据类型、包装类:调用包装类Xxx的parseXxx()
    @Test
    public void test5(){
    String s1 = "123";
    // int num1 = (int)s1;
    // int num2 = (Integer)s1; 为什么这么转?不明白 int 和String无 字符类关系

    int num3 = Integer.parseInt(s1); String转基本数据类型 包装类
    System.out.println(num3 + 1);
    }

    //基本数据类型、包装类---->String类:调用String的重载的方法:valueOf(xxx x)
    @Test
    public void test4(){
    int num1 = 123;
    Integer i1 = 123;
    //方式1:
    String s1 = num1 + "";
    String s2 = i1 + "";

    System.out.println(s1);//"123"
    System.out.println(s2);//"123"
    //方式2:
    String s3 = String.valueOf(num1);包装类,基本数据类型转 Stirng
    System.out.println(s3);
    }

    //jdk 5.0 新特性:自动装箱 与 自动拆箱
    @Test
    public void test3(){
    int num1 = 10;
    Integer i1 = num1;//自动装箱
    System.out.println(i1.toString());

    int num2 = i1;//自动拆箱
    System.out.println(num2);


    method(num1);
    }


    public void method(Object i){//public void method(Integer i){
    System.out.println("***********" + i);
    }


    //包装类---->对应的基本数据类型:调用包装类Xxx的xxxValue()
    @Test
    public void test2(){
    Integer i1 = new Integer(123);
    int num = i1.intValue();
    System.out.println(num);

    Boolean b = new Boolean(true);
    boolean b1 = b.booleanValue();
    System.out.println(b1);

    }

    //基本数据类型---->对应的包装类:调用包装类的构造器
    @Test
    public void test1(){
    ;
    //Integer:
    // System.out.println(num1.toString());
    int num1 = 10
    Integer i1 = new Integer(num1);
    i1 = new Integer("123");
    System.out.println(i1.toString());

    //Float:
    Float f1 = new Float(123.3F);
    f1 = new Float("12.3F");
    System.out.println(f1);

    //注意:使用String参数的构造器,要小心出现:NumberFormatException的异常
    // Integer i2 = new Integer("123a");
    // System.out.println(i2);


    //Boolean:
    Boolean b1 = new Boolean(true);
    System.out.println(b1);

    b1 = new Boolean("TRue"); //ture
    System.out.println(b1);
    //只要形参的字符串,在忽略大小写的情况下不是"true",那么都返回false.
    b1 = new Boolean("true123");
    System.out.println(b1);

    System.out.println();
    AA a = new AA();
    System.out.println(a.isFlag);//false--->null

    }
    }

    class AA{
    // boolean isFlag;//只能取值为true / false
    Boolean isFlag;//默认取值为null
    }

  • 相关阅读:
    python爬虫--requests模块
    相关基础概念
    python--用python操作Git
    celery异步,延时任务, 周期任务
    Ansible剧本中的角色—playbook中的roles
    Ansible中的剧本 ansible--playbook
    ansible模块总结2-- file, fetch, yum, pip, service, cron, user, group
    Ansible--安装,命令格式, ssh, command, shell, script, copy
    python--openpyxl模块使用, 对excel表格的操作
    Git使用02--branch分支, tag版本, 忽略文件 .gitingore
  • 原文地址:https://www.cnblogs.com/loushiqiang/p/7252916.html
Copyright © 2011-2022 走看看