initialization « Development « Java Data Type Q&A

1. Final initialization in java can be postponed, how does this prevent against returning null inadvertently?

In the "hidden features of java" question, someone mentions that final initialization can be postponed and provides an example:

public Object getElementAt(int index) {
    final Object element;

2. Should I set the initial java String values from null to ""?

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 ...

3. Set a String to "" or leave it null?

I have some String variables which are getting the values from invoking the function getParameter() and some of this variables will probably be null. Later, I will evaluate this variables using equals() ...

4. How do I initialize an object of a class?

My code is something like:

public class Foo {
    public int a;
    Bar[] bar = new Bar[10];

    a = bar[0].baz;

public class Bar {

5. Java: how to initialize String[]?


% javac variable errorSoon might not have been initialized
        errorSoon[0] = "Error, why?";
public class StringTest {

6. When I create a new String in Java, is it initialized with null or with " "?

Here's my test code:

String foo = new String();
The output is blank and a new line is written. Since I'm new to Java, I don't know whether it made a " " ...

7. Java String initialization

Which do you prefer and why"

String myString = null;
   myString = "something";
   myString = "something else";
String myString = "";
   myString = "something";
   myString ...

8. Java String initialization (part 2)

I asked this goofy question earlier today and got good answers. I think what I really meant to ask is the following:

String aString = ""; // Or = null ?

9. What's the difference between non-initialisation and initialising to null?

I have this code:

MyClass object;

.... some code here where object may or may not be initialised...

if (object.getId > 0) {
Which results in a compile error: object may ...

10. Difference in String initialization in Java

Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
Hi Guys, What's the difference between
1. String s = "abc";
2. String s = ...

11. Where is "null" in memory

In java, you cannot state an array's size in its declaration

int[5] scores;  //bad
I'm told this is because the JVM does not allocate space in memory until an object is initialized. ...

12. [eckel] on string initialization

Allen, My curiosity with the URL you posted took me to that wonderful discussion on 'String'. I am given to understand that everything in Java is an object. However, the string 'literal' that figures in the discussion has me confused. Is this 'literal' also an object in true sense. Please clarify. thanks, Raj.

13. String Initialization

In my opinion, it is useless to declare a String as String str=""; What you are doing is allocating a useless String object. Strings are immutable objects. So what will happen next is that you will recreate another String while writing str="Toto". The use of "" is however optimized since it belongs to the String pool. If you need to know ...

14. string initialization

I read Kathy Sierra book and couple of other books when i wrote scjp 4 years back. When i look back again those questions to strengthen my knowledge now, many of those concept are again new to me. That time i practiced on notepad with dcommand line execution and ide also. I was wondering how to remember so many concepts all ...

15. String initialization

16. Re: Null Pointer error, is there anything wrong with my class initialization?

My idea was to create an object TicketList in the main that is supposed to be able to hold N tickets. The main should add individual tickets to the TicketList object. I dont think there is a problem with my algo. Am I missing something? How can it get the Ticket array in class TicketList initialized?

17. initialization of a stringbuffer

Look at the docs for StringBuffer. Is there a constructor that takes a character and a repetition count? If so, there's your answer. If not, you'll have to look at what constructors are available and see if one of them takes an object that in turn has a constructor that takes a character and a count, and so on until you ...

18. String Initialization Problem