当前位置:网站首页>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
边栏推荐
- Function introduction of JMeter thread group
- 关于网页中的文本选择以及统计选中文本长度
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
- Popular understanding of random forest
- Go语言自学系列 | golang中的if else if语句
- 软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
- Microservices - load balancing ribbon
- 半监督学习
- Popular understanding of decision tree ID3
- Get the executable path through the process PID (queryfullprocessimagename)
猜你喜欢
Second kill system 3 - list of items and item details
子类隐藏父类的同名函数
Subclass hides the function with the same name of the parent class
Creation and destruction of function stack frames
Halcon and WinForm study section 2
Redis在Windows以及Linux系统下的安装
Jvm-05-object, direct memory, string constant pool
Jvm-06-execution engine
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
About text selection in web pages and counting the length of selected text
随机推荐
Jvm-03-runtime data area PC, stack, local method stack
软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
[system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
Popular understanding of random forest
QT common sentence notes
详解指针进阶2
Markdown file titles are all reduced by one level
Jvm-09 byte code introduction
子类隐藏父类的同名函数
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
Popular understanding of gradient descent
CString的GetBuffer和ReleaseBuffer使用说明
C language brush questions ~leetcode and simple questions of niuke.com
Digital image processing -- popular Canny edge detection
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
Jvm-05-object, direct memory, string constant pool
Popular understanding of linear regression (I)
Final review points of human-computer interaction
Microservice - declarative interface call openfeign