错误

% 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?";
        }
}

当前回答

String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};

其他回答

String[] errorSoon = new String[n];

n是它需要容纳的字符串数。

你可以在声明中这样做,或者在以后不使用String[]的情况下这样做,只要是在你尝试使用它们之前。

你可以这样写

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

字符串声明:

String str;

字符串初始化

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"; 

我们可以在String中获取单个字符:

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

如果我想像这样获取单个字符的Ascii值:

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

现在我想转换Ascii值为字符/符号。

int n=(int)chr;
System.out.println((char)n);//output:S

我相信你刚刚从c++迁移过来,在java中,你必须初始化一个数据类型(除了原始类型和字符串在java中不被认为是原始类型)来根据它们的规范使用它们,如果你不这样做,那么它就像一个空引用变量(很像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?"};   
    }
}
String[] arr = {"foo", "bar"};

如果你将一个字符串数组传递给一个方法,执行以下操作:

myFunc(arr);

或做的事:

myFunc(new String[] {"foo", "bar"});