1
String s = new String({'h','e','l','l','o'});

I received the error:

1 Invalid expression term '{'

I thought {'h','e','l','l','o'} should be a character array, why did it fail to compile?

1
  • See stackoverflow.com/a/5678393/414076 and Section 7.6.10.4 of the language specification. Your specific example is only a legal array initialization syntax in conjunction with a variable declaration, which is notably missing in your code. Commented Jun 28, 2012 at 1:13

2 Answers 2

6

I think you mean:

String s = new String(new[] {'h','e','l','l','o'});

The code you had before was not initializing an array correctly. Check out the MSDN article on implicitly typed arrays for more info.

You could also explicitly specify the type of the array:

String s = new String(new char[] {'h','e','l','l','o'});
3

Try something like this:

String s = new String(new char [] {'h','e','l','l','o'});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.