Java類成員變量的默認值


1、布爾型(boolean)變量默認值為false,byte、short、int、long為0,字符型為'\u0000'(空字符),浮點型(float double)為0.0,引用類型(String)為null。

 1 package cn.nxl2018;
 2 public class Test {
 3     private boolean bool;
 4     private byte bt;
 5     private short st;
 6     private char ch;
 7     private int  i;
 8     private long l;
 9     private float f;
10     private double d;
11     private String str;
12     public static void main(String[] args){
13         int j;
14         Test test=new Test();
15         System.out.println(test.bool);//默認值是false
16         System.out.println(test.bt);//默認值是0
17         System.out.println(test.st);//默認值是0
18         System.out.println(test.ch);//默認值是空字符('\u0000')
19         System.out.println(test.i);//默認是0
20         System.out.println(test.l);//默認是0
21         System.out.println(test.f);//默認是0.0
22         System.out.println(test.d);//默認是0.0
23         System.out.println(test.str);//默認是null
24         //System.out.println(j);//The local variable j may not have been initialized(變量可能沒有被初始化)
25     }
26 }

2、注意:未初始化的局部變量是不可以使用的,在這里可以認為是因為局部變量沒有默認值,所以不可以直接使用。空字符('\u0000')什么也不輸出,不要認為輸出是空格。

關聯博客(SCDN):https://blog.csdn.net/m0_38022608/article/details/80209741

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM