aspose file tools*
The moose likes Beginning Java and the fly likes String syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String syntax" Watch "String syntax" New topic
Author

String syntax

Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Hello all,
the below method has a different syntax for assigning values to a String object. Can anybody explain how does this work internally:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Does it work at all? Assuming it compiles at all, it is a weird bit of code. If you look at the array initialiser syntax, and remembering that [x] is array member access, please tell us how you think it works.

It does compile, but you have a tiny logic error, so please also suggest how you can get it to return a different value every time.
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
It does work. I got to understand the syntax now. We are declaring a String array as a 2 dimensional one and also we are initializing the same in the same line itself.

Here the 2nd dimension will be passed dynamically. Hence the value for "RandomName" returned varies each time depending upon the 2nd index passed. I got this code snippet under the topic "Overloaded Constructors" from SCJP 5 - Chapter 2.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
the only problem you have is that the x value will be always evaluated to zero you should have multiplied by 5 and then casting to (int).



Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Thanks Omar,
I realized the misplaced brackets. After placing them correctly, it is producing random values for 'x'. Also , i do understand the wierd syntax in the code snippet.
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Omar,
Got a doubt now. What is the difference between the below 2 code snippets:





both of them declares a single dimentional String array. but in the first declaration, we are assigning the new String object to the String ref "RandomName", whereas in the second decl we are not creating a new String object. Please clarify.Thanks
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
They just create a string array and assign it to the randomNames[]. they are the same however the first one is more understandable ;)
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Got it. Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String syntax