Can i do this in Java?

DilLw33D

Veteran X
can i compare two strings like so:

String line = "DOG";
if(line == "DOG);
//do some shit

is that valid or do i need to do something else?
 
i think there may be a string comparator function
or you may have to break it into tokiens and check it that way
 
You cant use == because you're comparing strings, an int or double however, would work just fine. Java can't compare strings unless you are using specific string functions.
 
DilLw33D said:
can i compare two strings like so:

String line = "DOG";
if(line == "DOG);
//do some shit

is that valid or do i need to do something else?


nothing can be done in java, ever. i suggest cobol, or pascal.
 
yubyub said:
You cant use == because you're comparing strings, an int or double however, would work just fine. Java can't compare strings unless you are using specific string functions.

Case of it needing to be a primitive type to use the boolean expressions such as == != etc. Anything other then prim type it compares the memory addresses and thus they never match if my memory serves correct.
 
You can check for a null value with == in a String...just an fyi, it's not if(!stringvariable.equals(null)) it's if(stringvariable != null)
 
Code:
if(stringvariable != null)
compares the reference, not the value :)
what you have there, good sir, is a null pointer :)
 
Back
Top