Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 22:36:56 11月06日 Update

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.

Tags: Object knowledge character string content variable type storage common knowledge point same memory address constant article way method more article variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info NVidia Huawei MariaDB