aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java main arg[] behaviour 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 "java main arg[] behaviour" Watch "java main arg[] behaviour" New topic
Author

java main arg[] behaviour

Lucas Toledo
Greenhorn

Joined: Apr 17, 2012
Posts: 6
I have the following program

when I run it
java ArgIt x
it does not print x2.
Why is that? why is it that when the string passed as an argument to the program is not ==??? does the args have a different value?
why is then "x"=="x" when args[0] is not equal (==) to "x" ?

thanks in advance,
Lucas
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Basically, you should never use == to compare Strings. You should always use equals(). However, because of the way the String pool is managed, == will sometimes give the right results. Line 5 is an example - using String literals will result in the same object being used, so == does what you expect. But this behaviour should never be relied on, and line 4 shows an example why.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
A good read - ScjpTipLine-StringsLiterally
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java main arg[] behaviour