Tuesday 28 May 2019

java - Why can't i compare command line arguments like other string arrays?




There seems to be a difference between main(String[] args) and other string arrays that i can not figure out, my example.




public class TestArgs 
{
public static void main(String[] args) {
String[] x = {"1","2","3"};
System.out.print( x[2] == "3" );
System.out.print( args[2] == "3" );
}}


I run this program as:




java TestArgs 1 2 3


I would expect the output to be "truetrue" but instead I get "truefalse"



Could someone please tell me what the difference is, or am I just doing something really stupid...


Answer



in java, you have to use "test".equals("test") to test for string-equality ;)




strings are objects and the objects are not the SAME, they just have the same VALUE


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...