Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does this line of code mean ?

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic