星空网 > 软件开发 > Java

java非静态变量初始化

java费静态变量的初始化分为两种情况,一种是局部变量,一种是类的域。

对于类的域,java在类初始化时,会为变量赋一个初始值。对于基本数据类型,java会将初始值设置为二进制0,具体为将boolean设置为false,将byte设置为0,将char设置为0(即空格),short设置为0,int设置为0,long设置为0,float设置为0.0f,都变了设置为0.0。对于引用类型也会将其初始值设置为二进制0,即null。

而对于局部变量加入你在申明变量的时候没有为其设置初始值,java将认为你这是个粗心导致的错误,所以java会给你一个编译异常。

Talk is weak, show me your code.

package InitializationAndCleanup;public class VariableInit {  private boolean mBoolean;  private byte mByte;  private short mShort;  private char mChar;  private int mInt;  private long mLong;  private float mFloat;  private double mDouble;  private VariableInit mInit;  public void function() {    int i;    //i ++; // compile error  }  public void printInitValue() {    System.out.println("boolean : " + mBoolean);    System.out.println("byte : " + mByte);    System.out.println("short : " + mShort);    System.out.println("char : [" + mChar + "]");    System.out.println("int : " + mInt);    System.out.println("long : " + mLong);    System.out.println("float : " + mFloat);    System.out.println("double : " + mDouble);    System.out.println("reference variable : " + mInit);  }  public static void main(String[] args) {    new VariableInit().printInitValue();  }}

输出:

boolean : false
byte : 0
short : 0
char : [ ]
int : 0
long : 0
float : 0.0
double : 0.0
reference variable : null

 




原标题:java非静态变量初始化

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海运物流老挝:https://www.goluckyvip.com/tag/98326.html
东营莒南物流:https://www.goluckyvip.com/tag/98327.html
至越南国际专线物流:https://www.goluckyvip.com/tag/98329.html
独特成长路:https://www.goluckyvip.com/tag/9833.html
南城县物流:https://www.goluckyvip.com/tag/98330.html
三门峡物流:https://www.goluckyvip.com/tag/98331.html
怪物在游轮上复活的电影 怪物在游轮上复活的电影叫什么:https://www.vstour.cn/a/411230.html
在线旅游如何选择更优惠的旅游产品?:https://www.vstour.cn/a/411231.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流