Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't i compare command line arguments like other string arrays? [duplicate]

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...

like image 716
MrDetail Avatar asked Dec 04 '22 14:12

MrDetail


1 Answers

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

like image 117
TheHe Avatar answered Jan 18 '23 23:01

TheHe