Why new keyword not needed for String

后端 未结 4 730
耶瑟儿~
耶瑟儿~ 2020-12-07 19:18

I am new in java.

In java, String is a class.But we do not have to use new keyword to create an object of class String<

4条回答
  •  我在风中等你
    2020-12-07 19:54

    In java
    "==" compares the left & right hand sides memory locations(and not the value at that memory location) and therefore in case of

    new String("hai")==new String("hai")

    it returns false.

    In case of "Hai"=="Hai", java doesn't allocate separate memory for same string literal therefore here "==" returns true. You can always use equals method to compare values.

提交回复
热议问题