Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between = = and equals in Java common knowledge points

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what is the difference between = = and equals in the common knowledge points of Java, the content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

= = is an operator. Equals is the method of the string object.

Variables of value types in java (that is, basic ones such as int, float, and so on) are stored in a stack in memory. The reference type (object) is only the address where the reference type variable is stored in the stack, and itself is stored in the heap. So the content of the string is the same, the reference address is not necessarily the same, it is possible to create multiple objects.

The String class is an immutable class String s = "Hello"; / /-1 String s1=new String ("World"); / /-- 2 Mode 1 is the requested variable stored in the constant pool, which is what java's performance optimization does. In other words, each time a string is created, the virtual machine creates a new object, because String is an immutable class, so the virtual machine optimizes and puts the string into a constant pool to reference different strings. The second method is to use objects created by new, then memory will be requested in the heap area, which is very expensive for a large number of such operations, so the second method is not recommended.

So for:

String a = "123"; String b =" 123"; System.out.println (a = = b); System.out.println (a.equals (b))

Will output two true

But for:

String a = new String; String b = new String; System.out.println (a = = b); System.out.println (a.equals (b))

False and true are output.

About Java common knowledge points in = = and equals what is the difference is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report