• Post Reply Bookmark Topic Watch Topic
  • New Topic

Experimenting with cmdline arguments. What am I missing here?  RSS feed

 
Nick Singh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a script that downloads images from the net. I've made a redownload flag (-r) that can be pass on the command line and can across this little problem. I've got the script working now with the .equals() method but I'm still curious about how something like this could happen.

java Test -r



public class Test {
private static String flag = null;
public static void main(String[] args) {
if (args.length == 1) {
flag = args[0];
System.out.println(flag);
if (flag == "-r") {
System.out.println("flag is -r");
}
else {
System.out.println("Rage Quit!!!");
}
}

String[] a = {"-r"};
if (a[0] == "-r") {
System.out.println("-r equals -r");
}
}
}

The output is below, which makes no sense.
-r
Rage Quit!!!
-r equals -r

 
Nick Singh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, forgot to properly edit the post.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Kolatsis wrote:Sorry, forgot to properly edit the post.


You can still go back and edit it.
http://www.coderanch.com/how-to/java/UseCodeTags
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use equals(), not ==, to compare Objects' states (contents). The == operator just tests whether two references point to the same object.

Your "-r equals -r" output tells you that in that case, both references point to the same String object.

So at this point you're probably wondering why sometimes equal Strings are the same object and sometimes there are equal Strings in two different objects. The answer lies in the constant pool. Every String literal in your source code, (that is every "string enclosed in double quotes", plus a few others) goes into a JVM-wide constant pool. So if you have "abc" in your code twice, it will be the same String object. Strings that come from other sources at runtime--such as reading from the command line--do not go into the constant pool (unless we put them there with the intern() method).
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nicholas,

Nicholas Kolatsis wrote: I've got the script working now with the .equals() method but I'm still curious about how something like this could happen.


Well, I assume that you know the difference between == and .equals() method. If not, please go through it, and it'll answer the question.

Apart from that, the problem is here:

Now, since you are not using .equals method, equality is checked on actual references, and not on contents of the object.

Further, when you hard-code a string (like "-r"), it goes to String pool. So, what is happening here is - a reference to String args[0] is being compared with a reference to String "-r" in String pool. Those references are different, and hence, if condition fails.
Since .equals() method checks the contents of the object, the comparison will return true.

Hope this helps.
 
Nick Singh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys, cleared it up for me.

Jeff Verdegan wrote:
Nicholas Kolatsis wrote:Sorry, forgot to properly edit the post.


You can still go back and edit it.
http://www.coderanch.com/how-to/java/UseCodeTags
Ha, I looked for the edit tag at the bottom of the page for ages. It's a little late but good to know it exists up there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!