• 基础类型与包装类型的区别


    基本类型有默认值,而包装类型初始为null。

    什么时候使用基础类型,什么时候使用包装类型:在阿里巴巴的规范里所有的POJO类必须使用包装类型,而在本地变量推荐使用基本类型。

    Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。 

    1、整数:包括int、short、byte、long初始值为0

    2、浮点型:float、double初始值为0.0

    3、字符:char初始值为空格,即'' ",如果输出,在Console上是看不到效果的。

    4、布尔:boolean初始值为false 

    实例验证:

    public class Model {
        private int num1;
    
        private Integer num2;
    
        private boolean bool1;
    
        private Boolean Bool2;
    
        private String str1;
      
        // get and set
    }
    public class ModelTest {
    
        private static String strTest;
        private static int intTest;
        private static Integer integerTest;
        private static boolean booleanTest;
        private static Boolean BooleanTest;
    
        public static void main(String[] args) {
            System.out.println("str: " + strTest);
            System.out.println("int: " + intTest);
            System.out.println("Integer: " + integerTest);
            System.out.println("boolean: " + booleanTest);
            System.out.println("Boolean: " + BooleanTest);
    
            System.out.println("****** model test ******");
            Model model = new Model();
            System.out.println("int: " + model.getNum1());
            System.out.println("Integer: " + model.getNum2());
            System.out.println("boolean: " + model.isBool1());
            System.out.println("Boolean: " + model.getBool2());
            System.out.println("string: " + model.getStr1());
        }
    }

    输出结果:

  • 相关阅读:
    这么香的Chrome插件,你都安装了吗?
    Android frameworks源码StateMachine使用举例及源码解析
    数据结构与算法系列七(队列)
    openoffice下中文乱码问题解决
    linux yum命令详解
    linux下Redis的安装
    ason 和 Java 对象转化示例
    java将office文档pdf文档转换成swf文件在线预览
    MyBatis SQL xml处理小于号与大于号
    Fisher 线性判别
  • 原文地址:https://www.cnblogs.com/zeroingToOne/p/11784164.html
Copyright © 2020-2023  润新知