jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can any one explain this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can any one explain this" Watch "can any one explain this" New topic

can any one explain this

venkat kaka

Joined: Sep 27, 2007
Posts: 10
hi i cant understand last line in the following code . can any one explain brefily
public static String removeLeadingZeros(String str){
if (str == null) {
return EaglelinkConstants.EMPTY_SPACE ;
char[] chars = str.toCharArray();
int length = str.length();
double value = Double.parseDouble(str);
int index =0;
if(value/10 != 0){
for (; index < length; index++) {
if (chars[index] != '0') {
return str;
return (index == 0) ? str : str.substring(index);// this line i need explanation.
what is ? there and what it returns
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
this is a ternary if operator
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

It's a shorthand for this code.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
it looks so absurd but it is totally legal.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
Originally posted by adam lui:
it looks so absurd but it is totally legal.

...and just like if-else-if-else. They can be nested as well. On one side it aids writability on the other language's readability suffers.

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Xyz Abc

Joined: Aug 24, 2007
Posts: 27
I agree. Here's the link: http://aspose.com/file-tools
subject: can any one explain this
It's not a secret anymore!