| 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: 3793
|
|
|
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: 1778
|
|
|
A good read - ScjpTipLine-StringsLiterally
|
 |
 |
|
|
subject: java main arg[] behaviour
|
|
|