-8

What is the difference between String str1 = "hello"; and String str2 = new String ("hello"); in java?

I know the str2 is a object, but what about str1?

I mean for example:

if both of them are object, but why

if(str1.toString() == str2.toString())

does not result a true boolean?

user2131316
  • 3,111
  • 12
  • 39
  • 53

1 Answers1

0

Object is a string in any case, BTW

if(str1.toString().equals(str2.toString()))

even is better

if(str1.equals(str2))

is the proper way to compare strings