zoukankan      html  css  js  c++  java
  • 数组元素的默认初始化值

    对于基本数据类型的变量创建的数组:byte,short,int,long,float,double,char,boolean:
    1.对于byte,short,long,int而言:创建数组以后默认值为0

    package com;
    
    public class V {
        public static void main(String[] args){
            int[] a=new int[3];//定义从0开始的三个数字的数组
            a[0]=70;
            a[2]=90;
            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }


    2.对于double,float而言:默认值为0.0

    public class V {
        public static void main(String[] args){
            float[] a=new float[3];//定义从0开始的三个数字的数组
            a[0]=70F;
            a[2]=90F;
            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }


    3.对于char类型而言:默认值为空

    public class V {
        public static void main(String[] args){
            char[] a=new char[3];//定义从0开始的三个数组
            a[0]='男';
            a[2]='女';
            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }


    4.对于Boolean而言:默认值为false

    public class V {
        public static void main(String[] args){
            boolean[] a=new boolean[3];//定义从0开始的三个数组
            a[0]=true;
            a[2]=false;
            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }

    5.对于引用类型的变量构成的数组而言:默认初始化值为null

    public class V {
        public static void main(String[] args){
            String[] a=new String[3];//定义从0开始的三个数组
            a[0]="阳阳";
            a[2]="寅寅";
            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
    }

  • 相关阅读:
    jupyter安装出现问题:安装后无法打开
    GitHub上传文件问题总结
    GitHub上传文件夹
    ELK 搭建
    mysql 开放远程连接权限连不上
    mysql linux下安装
    多个springboot项目部署到tomcat,Error deploying web application archive
    mysql 新增时,唯一索引冲突时更新
    日期计算
    mysql 忘记密码
  • 原文地址:https://www.cnblogs.com/KeepCalmAndNeverSayNever/p/10099805.html
Copyright © 2011-2022 走看看