Tuesday, May 28, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...