• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

What does this line of code mean ?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 439
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
money grubbing section goes here:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic