Runtime boolean and String error in java? -


hi println giving me error working fine if print them separately. idea why happening?

 class stringtesting { public static void main(string me[]) {     string s1="varun";     string s2="varun";     string s3="varun";     string s4=new string("varun");     string s5=new string("varun");     system.out.println(" "+s1==s3+" "+s1==s2+" ");//here giving me error } } 

and in advance :)

in terms of operators, + has precedence on ==.

it means code :

" "+s1==s3+" "+s1==s2+" " 

is translated compiler :

" varun"=="varun varun"=="varun " 

the first part " varun"=="varun varun" produces boolean

and applying "varun " string == comparator, results :

booleanexpression == "varun " 

comparing boolean string not valid.
anyway, if was, don't want want output boolean result of == comparison between objects.

so put between parentheses == comparisons indicate compiler operations in have evaluated :

" "+(s1==s3)+" "+(s1==s2)+" " 

it produce valid concatenation :

string + boolean + string + boolean + string


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -