1

Please find the code snippet below :

public static void main(String[] args) 
{
    System.out.println(args[0]=="abc");
}

I am getting the output as false (believe me I am passing abc as command line args i.e. java Main abc from command line) Can somebody explain me the reason behind "false" in the output? what I could conclude is command line args are created on heap,but couldn't find the concrete reason/explanation for the same.I understand String comparison and difference between equals and ==.Are command line args not stored in String pool?

Rajeev Akotkar
  • 1,377
  • 4
  • 26
  • 46
  • 1
    == checks equality of address of memory location and not the values using equals for string compares the value stored in the variable. If you use "abc".equals(args[0]) this will print true. – Vivek Singh Oct 29 '15 at 08:23
  • I understand that,but I dont find any rule which says that the command line args are created on the heap. – Rajeev Akotkar Oct 29 '15 at 08:24
  • @RajeevAkotkar Did you find a rule that says they aren't? – user207421 Oct 29 '15 at 09:02
  • 1
    Hi, I think the accepted answer of this question might give you the answer: http://stackoverflow.com/questions/29248173/comparing-command-line-arguments-reference-return-false-while-array-of-string-re – Igorovics Oct 29 '15 at 09:09

0 Answers0