String a="aa";
String is not a basic data type, but a reference type. For the above simple sentence, the Java compiler can determine it in the recompilation stage. Then store the "aa" object in the constant pool area of the current class file (note that it is not the heap, specifically the Perm area). The constant pool can also be expanded at runtime, such as the call of "XXX".intern() , that is, injecting data into the constant pool of the class during runtime. Continuous injection can lead to the java.lang.OutofMemory:PermGen space exception. The process is probably to first compare this string with each string in the constant pool for equals. If there is a match, return a reference to the constant pool. If neither exists, inject the constant value and return the current address. You can see String.intern( ) is not very efficient.
private static void test1(){ String a = “a” + “b” +1; Stirng b = “ab1”; System.out.println(a==b); }
Obviously the return result is True
About "==": For basic data types == is to compare the values of basic data types such as (byte, short, int, float, char , double, long, boolean), and for reference types, what is compared is the logical address of the two reference objects
About equals and hashcode(): The equals method of the Object class defaults to the reference of the two objects being compared. Logical address (return (this == obj);), the design of equels is to require subclasses to implement the comparison between two identical subclasses (it makes no sense to compare different subclasses, so I see many equels In the implementation, it is first judged whether the two objects belong to the same class) and whether the objects are equal. Here, it is not whether the reference addresses are equal. Java's default hashcode method provides the hash value of the object. It is a native method (the cost of calling native methods is still very high). Its return value is consistent with the return value of the System.identifyHashCode(Object) method. Usually it is A part of the binary number in the object header. The hashcode method can be said to identify the object and is used to hash the object in the hash algorithm. The String class overrides the HashCode method. It needs to traverse all the chars in char[] to generate the corresponding hashcode of the String class. In fact, the equals method of the object has nothing to do with the hashcode itself. It is just the use of some classes that makes everyone think that rewriting is The equals() method must override the hashcode() method. For example, in HashMap (and HashSet, but HashSet is implemented using HashMap), the process of putting an element in HashMap is to first call the key's hashcode method, and calculate the Entity index based on the return value of this method and the length of the Entity array in HashMap. , if there is already an element on this index, call the equals method of the key for comparison. If there is a reasonable element, replace it. If it does not exist, insert it into the linked list.
About compile-time optimization: For the compiler, the principle of compile-time optimization is to optimize what can be determined (such as final), and do not process what cannot be determined. Optimize final type variables, and optimize methods. The call is not optimized. Note that String a="a" + "b" is optimized to a="ab" at compile time, and String a="a" String b=a+"b" is optimized to the splicing of Stringbuffer at compile time.
The above is the detailed content of Interpret the String class in java. For more information, please follow other related articles on the PHP Chinese website!

使用Java的String.valueOf()函数将基本数据类型转换为字符串在Java开发中,当我们需要将基本数据类型转换为字符串时,一种常见的方法是使用String类的valueOf()函数。这个函数可以接受基本数据类型的参数,并返回对应的字符串表示。在本文中,我们将探讨如何使用String.valueOf()函数进行基本数据类型转换,并提供一些代码示例来

char数组转string的方法:可以通过赋值来实现,使用{char a[]=" abc d\0efg ";string s=a;}语法,让char数组对string直接赋值,执行代码即可完成转换。

大家好,今天给大家分享java基础知识之String。String类的重要性就不必说了,可以说是我们后端开发用的最多的类,所以,很有必要好好来聊聊它。

使用Java的String.replace()函数替换字符串中的字符(串)在Java中,字符串是不可变的对象,这意味着一旦创建了一个字符串对象,就无法修改它的值。但是,你可能会遇到需要替换字符串中的某些字符或者字符串的情况。这时候,我们可以使用Java的String类中的replace()方法来实现字符串的替换。String类的replace()方法有两种重

使用Java的String.length()函数获取字符串的长度在Java编程中,字符串是一种非常常见的数据类型,我们经常需要获取字符串的长度,即字符串中字符的个数。在Java中,我们可以使用String类的length()函数来获取字符串的长度。下面是一个简单的示例代码:publicclassStringLengthExample{publ

String中split方法使用String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组。1、一般用法用一般的字符,例如@或,等符号做分隔符时:Stringaddress="上海@上海市@闵行区@吴中路";String[]splitAddr=address.split("@");System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

一、认识String1.JDK中的String首先我们看看JDK中的String类源码,它实现了很多接口,可以看到String类被final修饰了,这就说明String类不可以被继承,String不存在子类,这样所有使用JDK的人,用到的String类都是同一个,如果String允许被继承,每个人都可以对String进行扩展,每个人使用的String都不是同一个版本,两个不同的人使用相同的方法,表现出不同的结果,这就导致代码没办法进行开发了继承和方法覆写在带来灵活性的同时,也会带来很多子类行为不

String.toLowerCase()函数是Java中一个非常有用且常见的字符串处理函数,它可以将一个字符串转换为小写形式。在本文中,我们将介绍该函数的使用方法,并给出一些相关的代码示例。首先,让我们来看一下String.toLowerCase()函数的基本语法。它没有任何参数,只需要调用它即可。下面是示例代码:Stringstr="Hel


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
