I don't know how to look at it today string Of == The problem of , I think it's still a problem for me to judge whether several strings are equal at this level ? ha-ha , There's really a big problem , The root cause is right java I don't know about bytecode .

First ,== The operator compares whether the memory address of the object pointed to by two variables is the same , For the same true, The difference is false, So the problem becomes to analyze whether the memory address of the string is the same ;

(1) String objects in the heap and strings in the string constant pool

String s1 = "a";
String s2 = new String("a");

The result is :false

reason :s1 This declaration adds a new one to the string constant pool “a” String object ,s2 In this way, a "a" String object ; So their memory addresses are not the same .

(2) Two string objects in the heap

String s1 = new String("a");
String s2 = new String("a");

The result is :false

reason : Two different objects must have different memory addresses .

(3) Strings in two constant pools

String s1 = "a";
String s2 = "a";

The result is :true

reason : The string constant pool is designed like this : When you add a string to the measurement pool, you will first judge whether you already have this string , The way to judge is equals; therefore s1、s2 Is the same string referenced , Same memory address .

(4)string.intern() Method

String s1 = "a";
String s2 = new String("a").intern();

The result is :true

reason :intern Method puts the string object into the constant pool , So the same as (4).

(5) Add variables and strings

String s1 = "a";
String s2 = "b";
String s3 = "ab";
String s4 = s1+s2;

The result is :false

reason : When adding variables instead of strings , Because we only know the string reference, we don't know the specific string value ,jvm Actually StringBuilder Of append Method , And then through toString Method returns the value of the string ,toString The method is as follows :

public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);

therefore , The reason is the same as (1).

If there are other situations, I will not list them , Just go and see java Bytecode can understand why this is the case , At the end of the day , In addition to helping people understand java Memory model , Nothing else is going to work , Oh no , You can still pretend .

java in String A string of == More related articles are analyzed

  1. JAVA in String( character string ) Class summary

    One :Java Compiler on string automatic optimization Example a : Source code : public class StringPool { public static void main(String args[]) { Str ...

  2. Java in String String constant pool summary

    Recently, I went to an Internet company in Guangzhou for an interview , At that time, the interviewer asked, suppose there are two strings String a="abc",String b = "abc"; Question output a==b yes true still fals ...

  3. Java in String String constant pool

    Let's start with an example , It's easier to understand through this example String Constant pool public static void main(String[] args) { String a = "ab"; St ...

  4. java in String character string

    One . Definition String character string String String and char The characters are different ,char Use single quotes , It can only represent one character , A string is a piece of text .String It's a class . This class uses final modification , So this class can't inherit, expand or repair ...

  5. java in String String replacement function :replace And replaceAll The difference between

    For example, there are the following x String String x = "[kllkklk\\kk\\kllkk]"; I'm going to take the inside “kk” Replace with ++, There are two ways to get the same results replace(CharSe ...

  6. Java in String character string toString()、String.valueOf()、String Strong go 、+ "" The difference between

    Object#toString(): Object object = getObject(); System.out.println(object.toString()); In this way of use , because ja ...

  7. JAVA in String String comparison equals() and equalsIgnoreCase() The difference between

    label : equals and equalsIgnoreC 2012-11-11 16:03 65644 Human reading   Comment on (0)  Collection   report   classification : JAVA(3)  1. Use equals( ) Method to compare two strings is ...

  8. Java String operations in ( Compare String,StringBuiler and StringBuffer)

    One . Preface Just beginning to learn Java when , As can only C The little white of language , I was shocked by the string operation . by comparison ,C Language stores an ending... In a byte array \0 To represent a string , Want to achieve string splicing , You also need to call strcpy Library functions or themselves ...

  9. Java String constant pool in

    ava There are two forms of string object creation in , One is the literal form , Such as String str = "droid";, The other is to use new This standard method of constructing objects , Such as String str = new ...

Random recommendation

  1. SQL Language

    SQL The classification of languages :DDL DML DQL DCL SQL The operation in is nothing more than ( Additions and deletions ) DDL:Data Query Language, Data query language ! It is mainly used to define and maintain various operation objects of database , Like the library . ...

  2. solr Detailed explanation , Necessary for development

    1. Basic knowledge of The process of creating an index is as follows : (1). Indexer IndexWriter, It's the framework of a Book (2). Create document objects Document, This is equivalent to an article (3). Create information field objects Field, This is equivalent to ...

  3. PyDev for Eclipse brief introduction

    PyDev install and configure install PyDev In the installation PyDev Before , Make sure you have installed Java 1.4 Or later .Eclipse as well as Python. Next , Start installation PyDev plug-in unit . start-up E ...

  4. ajax Exercise 3 search

    Make a car search page <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  5. Set up an intranet for the Internet login ftpserver

    ftpserver It's common to use server, But IP Resources are limited . So how to let the intranet server To provide services to users of the Internet ? First of all, we need to find a FTPserver Program , I use it here pure-ftpd-mys ...

  6. (28) Network programming UDP

    Introduce UDP Before programming the communication protocol , Let's start with network programming ,socket Related concepts : Network programming : Network programming is mainly used to solve the problem of computer and computer ( Mobile phone, tablet, etc ) The problem of data transmission between . Network programming : It doesn't need to be based on html Web pages can reach several ...

  7. Object-C Knowledge point

    Object-C Common knowledge points , Here are the knowledge points I use in the actual development , But I can't remember , Need Baidu knowledge 1. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...

  8. JS How to create an object

    js There are many ways to create objects , Here are the introduction

  9. English trip V1 - 8.What&#39;s in My Bag? What's in my bag ? Teacher:Corrine Key: plular( The plural ) and singular( singular )

    In this lesson you will learn to talk about the things you have.    You will learn how to talk about what you have Class content (Lesson) What' ...

  10. LinkServer-- There are three ways to access remote data tables

    stay TSQL There are three ways to access remote database in :1.OPENROWSET2.OPENDATASOURCE3.LinkServer In the use of openrowset/opendatasource Forward search must be enabled first Ad Hoc ...