267

Error

% javac  StringTest.java 
StringTest.java:4: variable errorSoon might not have been initialized
        errorSoon[0] = "Error, why?";

Code

public class StringTest {
        public static void main(String[] args) {
                String[] errorSoon;
                errorSoon[0] = "Error, why?";
        }
}
hhh
  • 50,788
  • 62
  • 179
  • 282

11 Answers11

419

You need to initialize errorSoon, as indicated by the error message, you have only declared it.

String[] errorSoon;                   // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement

You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index.

If you only declare the array (as you did) there is no memory allocated for the String elements, but only a reference handle to errorSoon, and will throw an error when you try to initialize a variable at any index.

As a side note, you could also initialize the String array inside braces, { } as so,

String[] errorSoon = {"Hello", "World"};

which is equivalent to

String[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
177
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};
Yauhen
  • 2,435
  • 1
  • 17
  • 18
  • 3
    Maybe not the exactly what the OPs question title suggests but i was trying to pass my string to a parameter that accepts String[] , this is the solution – kommradHomer Mar 31 '14 at 13:09
  • Can't you ommit the new String btw? String[] output = {"","",""}; seems to work in my code. – Pieter De Bie Apr 15 '15 at 11:20
  • 4
    If you have already initialized your array and you want to re-initialize it, you can't go `args = {"new","array"};` You will have to `args = new String[]{"new", "array"};` – Darpan May 08 '15 at 06:16
33
String[] errorSoon = { "foo", "bar" };

-- or --

String[] errorSoon = new String[2];
errorSoon[0] = "foo";
errorSoon[1] = "bar";
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
12

In Java 8 we can also make use of streams e.g.

String[] strings = Stream.of("First", "Second", "Third").toArray(String[]::new);

In case we already have a list of strings (stringList) then we can collect into string array as:

String[] strings = stringList.stream().toArray(String[]::new);
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
9

I believe you just migrated from C++, Well in java you have to initialize a data type(other then primitive types and String is not a considered as a primitive type in java ) to use them as according to their specifications if you don't then its just like an empty reference variable (much like a pointer in the context of C++).

public class StringTest {
    public static void main(String[] args) {
        String[] errorSoon = new String[100];
        errorSoon[0] = "Error, why?";
        //another approach would be direct initialization
        String[] errorsoon = {"Error , why?"};   
    }
}
Tayyab Kazmi
  • 376
  • 4
  • 19
8
String[] arr = {"foo", "bar"};

If you pass a string array to a method, do:

myFunc(arr);

or do:

myFunc(new String[] {"foo", "bar"});
trillions
  • 3,669
  • 10
  • 40
  • 59
7
String[] errorSoon = new String[n];

With n being how many strings it needs to hold.

You can do that in the declaration, or do it without the String[] later on, so long as it's before you try use them.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
AaronM
  • 1,577
  • 12
  • 15
2

You can always write it like this

String[] errorSoon = {"Hello","World"};

For (int x=0;x<errorSoon.length;x++) // in this way u create a for     loop that would like display the elements which are inside the array     errorSoon.oh errorSoon.length is the same as errorSoon<2 

{
   System.out.println(" "+errorSoon[x]); // this will output those two     words, at the top hello and world at the bottom of hello.  
}
tjd
  • 4,064
  • 1
  • 24
  • 34
Gopolang
  • 21
  • 1
1

You can use below code to initialize size and set empty value to array of Strings

String[] row = new String[size];
Arrays.fill(row, "");
Ali Sadeghi
  • 312
  • 2
  • 7
0

String Declaration:

String str;

String Initialization

String[] str=new String[3];//if we give string[2] will get Exception insted
str[0]="Tej";
str[1]="Good";
str[2]="Girl";

String str="SSN"; 

We can get individual character in String:

char chr=str.charAt(0);`//output will be S`

If I want to to get individual character Ascii value like this:

System.out.println((int)chr); //output:83

Now i want to convert Ascii value into Charecter/Symbol.

int n=(int)chr;
System.out.println((char)n);//output:S
Py-Coder
  • 2,024
  • 1
  • 22
  • 28
0
String[] string=new String[60];
System.out.println(string.length);

it is initialization and getting the STRING LENGTH code in very simple way for beginners