String java
https://www.golinuxcloud.com/java-interview-questions-answers-experienced-2/
75. What is the meaning of Immutable in the context of String class in Java?
- An Immutable object cannot be modified or changed in Java. String is an Immutable class in Java.
- Once a String object is created, it cannot be changed. When we assign the String to a new value, a new object is created
76. Why a String object is considered immutable in java?
- Java language uses String for a variety of purposes. For this it has marked String Immutable.
- There is a concept of String literal in Java.
- Let say there are 2 String variables A and B that reference to a String object “TestData”. All these variables refer to same String literal. If one reference variable A changes the value of the String literal from “TestData” to “RealData”, then it will affect the other variable as well. Due to which String is considered Immutable. In this case, if one variable A changes the value to “RealData”, then a new String literal with “RealData” is created and A will point to new String literal. While B will keep pointing to “TestData”
77. How many objects does following code create?
Code:
String s1="HelloWorld";
String s2=" HelloWorld ";
String s3=" HelloWorld ";
The above code creates only one object. Since there is only one String Literal “HelloWorld” created, all the references point to same object.
78. How many objects does following code create?
Code:
String s = new String("HelloWorld");
The above code creates two objects. One object is created in String constant pool and the other is created on the heap in non-pool area.
79. What is String interning?字符串的驻留
- String interning refers to the concept of using only one copy of a distinct String value that is Immutable.
- It provides the advantage of making String processing efficient in Time as well as Space complexity. But it introduces extra time in creation of String.
http://www.cnblogs.com/artech/archive/2007/03/04/663728.html
80. What is the basic difference between a String and StringBuffer object?
String is an immutable object. Its value cannot change after creation. StringBuffer is a mutable object. We can keep appending or modifying the contents of a StringBuffer in Java.
https://en.wikipedia.org/wiki/String_interning
String interning
In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.[1] Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.
The single copy of each string is called its intern and is typically looked up by a method of the string class, for example String.intern()[2] in Java. All compile-time constant strings in Java are automatically interned using this method.[3]
String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.
Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]
Contents
History
Lisp introduced the notion of interned strings for its symbols. Historically, the data structure used as a string intern pool was called an oblist (when it was implemented as a linked list) or an obarray (when it was implemented as an array).
Modern Lisp dialects typically distinguish symbols from strings; interning a given string returns an existing symbol or creates a new one, whose name is that string. Symbols often have additional properties that strings do not (such as storage for associated values, or namespacing): the distinction is also useful to prevent accidentally comparing an interned string with a not-necessarily-interned string, which could lead to intermittent failures depending on usage patterns.
Motivation
String interning speeds up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on associative arrays with string keys to look up the attributes and methods of an object. Without interning, comparing two distinct strings may involve examining every character of both.[Note 1] This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fill up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.
String interning also reduces memory usage if there are many instances of the same string value; for instance, it is read from a network or from storage. Such strings may include magic numbers or network protocol information. For example, XML parsers may intern names of tags and attributes to save memory. Network transfer of objects over Java RMI serialization object streams can transfer strings that are interned more efficiently, as the String object's handle is used in place of duplicate objects upon serialization.[7]
Issues
Multithreading
One source of drawbacks is that string interning may be problematic when mixed with multithreading. In many systems, string interns are required to be global across all threads within an address space (or across any contexts which may share pointers), thus the intern pool(s) are global resources that should be synchronized for safe concurrent access. While this only affects string creation (where the intern pool must be checked and modified if necessary), and double-checked locking may be used on platforms where this is a safe optimization, the need for mutual exclusion when modifying the intern pool can be expensive.[8]
Contention can also be reduced by partitioning the string space into multiple pools, which can be synchronized independently of one another.
Reclaiming unused interned strings
Many implementations of interned strings do not attempt to reclaim (manually or otherwise) strings that are no longer used. For applications where the number of interned strings is small or fixed, or which are short-lived, the loss of system resources may be tolerable. But for long-running systems where large numbers of string interns are created at runtime, the need to reclaim unused interns may arise. This task can be handled by a garbage collector, though for this to work correctly weak references to string interns must be stored in the intern pool.
String java的更多相关文章
- SpringBoot报错:nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk.test.User matching [java.lang.Long, java.lang.String, java.lang.String]
错误提示: Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk._16 ...
- java.lang.String & java.lang.StringBuilder
java.lang.String & java.lang.StringBuilder String 成员方法 作用 public charAr(int index) 返回给定位置的代码单元 p ...
- Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, java.lang.String>
org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'log ...
- No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- String(Java版本)
import java.io.UnsupportedEncodingException; public class Driver { public static void main(String[] ...
- String java问题随笔
1.字符串加密 设计思想: 每个字符都能够转化为整数型,也能将整数型转化为字符类型,这样我们在加密时候由于向后推3个,所以可以将字符转换为整形,然后加3,之后在将运算完的变量转化为字符后输出,就可以实 ...
- Scanner、String(java基础知识十二)
1.Scanner的概述和方法介绍 * A:Scanner的概述 * 是一个从键盘输入的类,有final修饰,不能被子类继承 * Scanner sc = new Scanner(System.in) ...
- leetcode 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- leetcode 97 Interleaving String ----- java
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- 327. Count of Range Sum(inplace_marge)
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 3DMAX 多维材质及对应的UVW展开,UVW贴图
多维材质说明 多维材质就是一个模型多个材质,(混合材质是多个材质混一起,跟这个貌似没关,比如地表草地,泥土等的混合操作) 作用: 比如一个模型就是需要两种材质,刀的金属刀身,木质刀柄,墙的一面是木板, ...
- DOM核心API
是什么? 是各大浏览器提供的针对HTML和XML文档的一个API(Application Programming Interface应用程序编程接口).DOM描述了一个层次化的节点树,容许开发人员对D ...
- Announcing .NET Core 2.1
Announcing .NET Core 2.1 https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-net-core-2-1/ ...
- NET Core中使用Irony
在.NET Core中使用Irony实现自己的查询语言语法解析器 在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建 ...
- DevExpress PivotGrid 使用记录
1.自定total值: 调试的时候,如果要定位,给一个index,然后,把e.CustomVale=index++;定位后,监视ds的值,每个ds的值不一样!
- C# Mutex互斥锁
Mutex 构造函数 (Boolean, String, Boolean) public Mutex ( bool initiallyOwned, string name, out bool crea ...
- 对jvm虚拟机 内存溢出的思考
java内存溢出:当新产生对象时,新生代空间不够,导致无法申请到足够的空间,报内存溢出 内存泄漏:一些静态集合,静态常量没有被gc清理,越来越多,占用内存,最后导致无法申请到新的空间
- left join \ right join \ inner join 详解
left join 和 left outer join 的区别 通俗的讲: A left join B 的连接的记录数与A表的记录数同 A right join B ...
- MVC 路由URL重写
在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. URL重写与优化就是搜索引擎优化的手段之一. 假如某手机网站(基于ASP.NET MVC)分 ...