2

When initialising variables with default values:

What is the difference between:

private static String thing = null;

and

private static String thing = "";

I'm not understanding which is better and why nor what is the best way to deal with other data types.

private static int number = 0;
private static double number  = 0;
private static char thing = 0;

Sorry I struggle learning new languages.

  • 7
    No need to apologize. We were all beginners once. – Christian Ternus Oct 31 '13 at 01:47
  • `null` can be assigned only to **objects** (and not **primitives**). – PM 77-1 Oct 31 '13 at 01:47
  • 1
    In Java (for better or worse) Objects default to null which is rather similar to the null pointer in C/C++. Unlike some dynamic languages (JavaScript etc...) which are "falsey" you need to be explicit. It is both good and bad. You're not going to see statements like if(string){..} in Java. You'll see -> if(string != null && !string.isEmpty(){...} or if(SomeClass.isEmptyOrNull(string)) {..} – Daniel B. Chapman Oct 31 '13 at 01:49
  • 1
    @Skippy no worries, hopefully it helps you! – Daniel B. Chapman Oct 31 '13 at 01:53

4 Answers4

3

Except for initializing String to an empty string

private static String thing = "";

the other assignments are unnecessary: Java will set all member variables of primitive types to their default values, and all reference types (including java.String) to null.

The decision to initialize a String to a null or to an empty string is up to you: there is a difference between "nothing" and "empty string" *, so you have to decide which one you want.


* The differences between "nothing" and "empty string" stem from the observation that no operations are possible on a null string - for example, its length is undefined, and you cannot iterate over its characters. In contrast, the length of an empty string is well-defined (zero), and you can iterate over its characters (it's an empty iteration).
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    `Java will set all primitives to their default values, and all reference types (including java.String) to null.` I believe it is better to mention that's only true for member variables. For local variables, Java is not doing such default value initialization and developer need to make sure the variables are properly initialized. – Adrian Shum Oct 31 '13 at 03:12
2

In Java null and an empty are not the same thing.

From suns java tutorial

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type Default Value (for fields)
byte      0
short     0
int       0
long      0L
float     0.0f
double    0.0d
char      '\u0000'
String (or any object) null
boolean   false

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

user207421
  • 305,947
  • 44
  • 307
  • 483
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

When you make:

private static String ptNo = "";

you are creating a variable ptNo and making it to refer an object String "".

When you make:

private static String ptNo = null;

you are creating a variable, but it doesn't refer to anything. null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

"" is an actual string with empty value.

null means that the String variable points to nothing.

As an example,

String a="";
String b=null;

a.equals(b) returns false because "" and null do not occupy the same space in memory.

Baby
  • 5,062
  • 3
  • 30
  • 52
  • 2
    Your answer is very ambiguous, bordering on wrong. `==` is not really a way to compare strings (which are objects). – PM 77-1 Oct 31 '13 at 01:56
  • 1
    @Skippy yeah, i use == just to make it easier to understand. btw, i've edited it. does the NullPointerException means that its points to nothing? correct me if i'm wrong, i'm a begginer too – Baby Oct 31 '13 at 02:17