Results 1 to 8 of 8
  1. #1
    talenone is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Why this java code has written this way?

    Can someone tell me about this style of writing in java?

    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 });
    		}
    	}
    Last edited by talenone; 01-10-2013 at 10:28 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Why this java code has written this way?

    What about it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    talenone is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default 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?

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    I like likes!

  5. #5
    talenone is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Why this java code has written this way?

    My question is how the Java compiler interpret this code?

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    I like likes!

  7. #7
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default 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. :)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why this java code has written this way?

    Quote Originally Posted by talenone View Post
    Java Code:
            
    			NORM_STRINGS[b - 'a'] = new String(new char[] { b });
    I would've written the above snippet as:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need some help with java coding
    By iamamyth in forum New To Java
    Replies: 5
    Last Post: 01-05-2013, 10:43 AM
  2. Replies: 4
    Last Post: 10-25-2010, 04:49 PM
  3. help with java coding
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 11-14-2009, 08:00 AM
  4. Long boolean coding style
    By Aldius in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 01:51 AM
  5. java coding
    By zawad in forum Suggestions & Feedback
    Replies: 1
    Last Post: 07-07-2009, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •