Why this java code has written this way?
Can someone tell me about this style of writing in java?
Code:
private static final String[] NORM_STRINGS = new String['z' - 'a' + 1];
{
COLLATOR.setStrength(Collator.PRIMARY);
for (char b = 'a'; b <= 'z'; b++)
{
NORM_STRINGS[b - 'a'] = new String(new char[]
{ b });
}
}
Re: Why this java code has written this way?
Re: Why this java code has written this way?
I'm trying to understand the way syntax has been written; the block of code in braces soon after the declaration seems like method, isn't it?
Re: Why this java code has written this way?
Well it is not a good or nice code style. It is probably written like that to confuse someone intentionally. :(devil):
Re: Why this java code has written this way?
My question is how the Java compiler interpret this code?
Re: Why this java code has written this way?
It depends on what is enclosing all this... it is not the whole code, so we cannot tell you that.
The first line is a declaration so the assumption is that this level is inside a class declaration.
Then follows a code block which is executed as is at the beginning straight away as far as I know it.
Re: Why this java code has written this way?
its simple code just replace 'a' and 'z' with its ascii value. and you will get answer. :)
Re: Why this java code has written this way?
Quote:
Originally Posted by
talenone
Code:
NORM_STRINGS[b - 'a'] = new String(new char[] { b });
I would've written the above snippet as:
Code:
NORM_STRINGS[b - 'a'] = new String.valueOf(b);
I don't know if it matters much but the original version allocates two objects (a char array and a String) and I hope those Sun folks were a bit more clever ...
kind regards,
Jos