• java中的包装类


    基本类型是不具备对象特性的,比如基本类型不能调用方法,功能简单,为了让基本类型也具备对象的特性,Java为每个基本数据类型都提供了包装类,这样我们就可以像操作对象一样操作基本数据类型

    基本类型和包装类之间的对应关系:

    这里写图片描述

    包装类主要提供了两大类方法:

    1. 将本类型和其他基本类型进行转换的方法

    2. 将字符串和本类型及包装类互相转换的方法

    以integer包装类为例,看下包装类的特性。

    Integer 包装类的构造方法:

    1. Integer(int value):创建一个Integer对象,表示指定的int值
    2. Integer(String s):创建一个Integer对象,表示String参数所指定的int值

    代码所示:
    int i = 2;//定义int类型变量,值为2

    Integer m = new Integer(5);//定义一个Integer包装类对象,值为5

    Integer n = new Integer(“8”);//定义一个Integer包装类对象值为8

    Integer包装类的常用方法:

    这里写图片描述

    代码示例:

    public class HelloWorld {
        public static void main(String[] args) {
    
            // 定义int类型变量,值为86
            int score1 = 86; 
    
            // 创建Integer包装类对象,表示变量score1的值
            Integer score2=new Integer(score1);
    
            // 将Integer包装类转换为double类型
            double score3=score2.doubleValue();
    
            // 将Integer包装类转换为float类型
            float score4=score2.floatValue();
    
            // 将Integer包装类转换为int类型
            int score5 =score2.intValue();
    
            System.out.println("Integer包装类:" + score2);
            System.out.println("double类型:" + score3);
            System.out.println("float类型:" + score4);
            System.out.println("int类型:" + score5);
        }
    }
  • 相关阅读:
    js 正则表达式 test match exec三个方法的异同
    网页使用MD5加密
    解决Google地图和字体api无法加载的问题(转)
    Javascript 的addEventListener()及attachEvent()区别分析
    get与post的区别
    清除浮动的几种方法
    zoom属性(IE私有属性)
    class,id和name的区别
    深夜偷精之反射函数
    jQuery和js区别
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10454056.html
Copyright © 2020-2023  润新知