File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes What does this line of code mean ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "What does this line of code mean ?" Watch "What does this line of code mean ?" New topic
Author

What does this line of code mean ?

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
I see this similar line of code often in a Java book that I am reading.



Why does it have to set it to args[0] if the argument length is >0 ?

Thanks.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
The ternary operator is a short hand for if-else based assignment. If the array or arguments args has values then the first argument is assigned as the host, otherwise "localhost" is used as the as host. So if you call the program and provide an argument for the host then that argument value is used, if you call without any arguments then "localhost" is used.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Why does it have to set it to args[0] if the argument length is >0


We can only tell that once we take a look at the program written in the book. As far as I can tell, this particular check is just making sure that the 'host' variable name is never null. The program that follows may be dependant on the presence of this variable name and therefore the author has added a check on this variable so that if user hasn't passed in any argument then set the 'host' to 'localhost'.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
E Armitage wrote:The ternary operator is a short hand for if-else based assignment. If the array or arguments args has values then the first argument is assigned as the host, otherwise "localhost" is used as the as host. So if you call the program and provide an argument for the host then that argument value is used, if you call without any arguments then "localhost" is used.


Thanks for the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this line of code mean ?