关闭

JavaSE__Integer 的默认初始值

4523人阅读 评论(0) 收藏 举报
分类:

Integer 如没有赋初值会为null,会引发空指针异常,示例代码如下

package com.login.bean;

public class UserObject {
	private Integer user_id;
	private String user_name;
	private String user_pass;

	{
		user_name = "";
		user_pass = "";
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getUser_pass() {
		return user_pass;
	}

	public void setUser_pass(String user_pass) {
		this.user_pass = user_pass;
	}

	public UserObject(int user_id, String user_name, String user_pass) {
		super();
		this.user_id = user_id;
		this.user_name = user_name;
		this.user_pass = user_pass;
	}

	public UserObject(String user_name, String user_pass) {
		super();
		this.user_name = user_name;
		this.user_pass = user_pass;
	}

	public UserObject(String user_name) {
		super();
		this.user_name = user_name;
	}

	public UserObject() {

	}

	public static void main(String[] args) {
		UserObject testObject = new UserObject();
		System.out.println(testObject.getUser_id() == 0);
		System.out.println(testObject.getUser_name() == "");
		System.out.println(testObject.getUser_pass() == "");
	}
}

注意这里user_id 没有赋予初始值,会引发异常

运行后错误如下:




5
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:821825次
    • 积分:12464
    • 等级:
    • 排名:第1300名
    • 原创:343篇
    • 转载:337篇
    • 译文:0篇
    • 评论:39条
    文章分类
    最新评论