Should I set the initial java String values from n

2019-01-23 01:04发布

Often I have a class as such:

public class Foo
{
private String field1;
private String field2;

// etc etc etc
}

This makes the initial values of field1 and field2 equal to null. Would it be better to have all my String class fields as follows?

public class Foo
{
private String field1 = "";
private String field2 = "";

// etc etc etc
}

Then, if I'm consistent with class definition I'd avoid a lot of null pointer problems. What are the problems with this approach?

11条回答
Evening l夕情丶
2楼-- · 2019-01-23 01:05

Does it actually make sense in a specific case for the value to be used before it is set somewhere else, and to behave as an empty String in that case? i.e. is an empty string actually a correct default value, and does it make sense to have a default value at all?

If the answer is yes, setting it to "" in the declaration is the right thing to do. If not, it's a recipe for making bugs harder to find and diagnose.

查看更多
We Are One
3楼-- · 2019-01-23 01:11

That way lies madness (usually). If you're running into a lot of null pointer problems, that's because you're trying to use them before actually populating them. Those null pointer problems are loud obnoxious warning sirens telling you where that use is, allowing you to then go in and fix the problem. If you just initially set them to empty, then you'll be risking using them instead of what you were actually expecting there.

查看更多
冷血范
4楼-- · 2019-01-23 01:11

No way. Why do you want to do that? That will give incorrect results. nulls and """ are not same.

查看更多
男人必须洒脱
5楼-- · 2019-01-23 01:12

Null is better, that is why they are called unchecked exceptions {Null pointer exception}. When the exception is thrown, it tells you that you have to initialize it to some non null value before calling any methods on it.

If you do

private String field1 = "";

You are trying to supress the error. It is hard to find the bug, later.

查看更多
老娘就宠你
6楼-- · 2019-01-23 01:18

Absolutely not. An empty string and a null string are entirely different things and you should not confuse them.

To explain further:

  • "null" means "I haven't initialized this variable, or it has no value"
  • "empty string" means "I know what the value is, it's empty".

As Yuliy already mentioned, if you're seeing a lot of null pointer exceptions, it's because you are expecting things to have values when they don't, or you're being sloppy about initializing things before you use them. In either case, you should take the time to program properly - make sure things that should have values have those values, and make sure that if you're accessing the values of things that might not have value, that you take that into account.

查看更多
在下西门庆
7楼-- · 2019-01-23 01:18

I think when you use String s = null it will create variable "s" on stack only and no object will exists on heap,but as soon as you declare things as like String s=""; what it will does is like it will create "" object on heap.As we know that Strings are immutable so whenever u wil assign new value to string varible everytime it will create new Object on heap...So I think String s=null is efficient than String s = "";

Suggestions are welcome!!!!!

查看更多
登录 后发表回答