You should not use ==
when comparing objects including strings. Generally ==
compares references, so it return true
only of same objects. You should use equals()
method instead: str1.equals(str2)
It occasionally works for you in first case because java caches string constants, so "java"
in both cases is represented by same instance of String.
solved How should I declare strings? [duplicate]