当前位置:网站首页>Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
2022-07-03 15:52:00 【Zhangjian Tianya 2.0】
Basic data type :
Basic data types can be regarded as keywords
Integer type :byte(1) short(2) int(4) long(8) Numbers represent byte sizes One byte has eight binary numbers
floating-point :float(4) double(8) Floating point numbers can be accurate to six decimal places at most
Character :char unocode
Boolean type :boolean true/false(1)
Where defined variables store values
Reference data type : class , Interface ; Array
Where the variable stores the address of the data heap memory
Heap memory and stack memory
Stack memory generally stores variables defined by keywords
Heap memory storage class ; Interface ; Variables such as arrays Generally, the storage form is to use a variable to store the code of the segment value , This code points to a section of memory in the heap memory , The memory stores data
Constant pool ( A special area contained in heap memory )
character string :
String str ="ABC"; First look for this value in the constant pool :
1 If yes, return the address in the constant pool
2 Create the content of this segment of data and store it in the constant pool , Returns the address in the constant pool
String strc = new String("ABC");
1 Now look in the constant pool , No, just create one in the constant pool
2 Then create an object in the heap memory ; Finally, the address in the heap memory is returned
Numbers :
example :
int c = 10,d=10;// This kind of data is relatively insensitive
System.out.println(c==d);
Integer a1=10,b1=10;// It is also the return address, but it is the same address ; All in the constant pool
Integer a2=128,b2=128;// When creating objects like this ( The data is too large )
System.out.println(a2.equals(b2));
System.out.println(a2==b2);
System.out.println(a1==b1);In turn true reason : The first comparison is binary coding
true equals The method is directly correct after comparison
false When the scope of comparison exceeds -128 To 127 The object is not stored in the constant pool ; New objects will be formed in heap memory , The address is no longer in the constant pool memory
true :a1,b1 The returned values are all addresses in the constant pool
String instances :
public class Charcompare {
static String str;// front static The role of
// Heap memory and stack memory ; Stack memory generally stores the types defined by keywords , local variable , Global variables, etc
// When using reference types to store values, they are usually stored in heap memory ; Constant pool in heap memory ( Convenient comparison ); Create objects like keywords ( Stored in the constant pool ; Return constant pool address )
public static void main(String[] args) {
String stra = "ABCD";// Create directly in the constant pool ; What is returned is the code in the constant pool
String strb ="ABCD";
String strc =new String("ABCD");// First create in the constant pool ; Then go back to the heap to create ; The last value returned is the code in the heap
String strd = new String("ABCD");
System.out.println(stra==strb);// The first one is right because ; Because this creation method returns the addresses in the constant pool
System.out.println(stra.equals(strc));// Look for the equals Source code
System.out.println(strc==strd);// Incorrect because the data created with the reference data type , It will first open up an address in the heap memory ; The string variable name stores the number of the address
// The returned value is finally the code of heap memory ; This kind of creation is to reopen the space memory in the heap memory ; So the codes are different
equals Explanation of source code analysis :
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}such as a.equals(b)
1 first if Judge a and b Is it an object (this Is it possible to point directly to a)
2 Judge anObject Is it right? String Example : Not so False
3 Judge a,b Whether the string length is equal ( among value refer to a This string to the left of the dot )
String anotherString =(String)anobject Will instance anobject Strong to string type
4 Judge whether individual characters are equal in turn
5 If both are satisfied, the final return value is true
Be careful boolean The returned value is a Boolean value
边栏推荐
- Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64
- Reflection on some things
- 驱动与应用程序通信
- Popular understanding of linear regression (I)
- Redis在Windows以及Linux系统下的安装
- 软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
- 请做好3年内随时失业的准备?
- 详解指针进阶2
- 坚持输出需要不断学习
猜你喜欢

UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)

Visual host system design and development (Halcon WinForm)

Summary of JVM knowledge points

详解指针进阶2

String functions that you need to know

How are integer and floating-point types stored in memory

"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation

How to use annotations such as @notnull to verify and handle global exceptions

请做好3年内随时失业的准备?

半监督学习
随机推荐
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
Visual host system design and development (Halcon WinForm)
Qt常用语句备忘
GCC cannot find the library file after specifying the link library path
Go language self-study series | if else if statement in golang
Tensorflow realizes verification code recognition (I)
Seckill system 3- product list and product details
Secsha system 1- login function
QT use qzxing to generate QR code
Reentrantlock usage and source code analysis
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
请做好3年内随时失业的准备?
潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
Jvm-05-object, direct memory, string constant pool
Calibre LVL
nifi从入门到实战(保姆级教程)——flow
Problems of CString in multithreading
Microservice sentinel flow control degradation
秒杀系统2-Redis解决分布式Session问题
Visual upper system design and development (Halcon WinForm) -5 camera