黑马程序员--05.String字符串类--01. String s1=new String (a)内存图【个人总结】
来源:互联网 发布:socket用法 java 编辑:程序博客网 时间:2024/06/04 17:51
String s1=new String(“a”)内存图
----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
1. 问题描述
【1】Strings1=new String ("a");
说法:new 了一个对象,"a"也是一个对象;"a"不是相当于一个参数么?为什么说"a"是个对象?
【2】内存图的样子是什么?
【3】分析
{1}. 由于当时看到这个问题之后,自己也不是很清楚,所以,不得不借助一下String类的参数是String的重载的构造方法
/**
* Initializesa newly created {@code String} object so that it represents
*the same sequence of characters as the argument; in other words, the
*newly created string is a copy of the argument string. Unless an
*explicit copy of {@code original} is needed, use of this constructor is
*unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {…}
{2}. 现在分析一下这个方法什么时候使用:
我翻译一下上面这段对这个构造方法的说明:
*************************************************************************
这个构造方法用来初始化一个和输入参数具有相同字符序列的字符串对象。换句话说,新建的这个字符串对象是传入的字符串参数对象的一份副本。除非是明确地需要使用字符串复制功能,否则由于字符串常量值是不可变的,这个构造函数是没有必要去直接使用。(意译)
*************************************************************************
【结论】:以String为参数的构造函数开发中使用可能性已经很小。所以,这个构造方法主要在考题里面出现。
2. 源码分析
(1). 分析String类的成员属性 -----源码
public final class String
implements java.io.Serializable,Comparable<String>, CharSequence{
/** The value is used forcharacter storage. */
private final char value[]; //存储字符串中元素的字符数组
/*
注意:value是引用常量:一旦初始化指向了一个内存中的数组,
就不能再指向别的数组了
*/
/** The offset is the first indexof the storage that is used. */
private final int offset; //value中存储字符串的第一个索引位置---常量
/** The count is the number ofcharacters in the String. */
private final int count; //value中存储字符串的长度---常量
...
}
【结论】这样Java中实际上是使用指针(offset)、计数器(count)和字符数组(value[])来描述String类的
大概就是下图:一旦通过构造函数初始化这个新建的字符串之后,这个字符数组对应的指针和计数器都是固定的,不能改变了。
(2). 分析String类的publicString(String original) -----源码
public String(String original) {
//size表示参数数组的真实长度----赋值给新的变量的原因就是count是final的
int size = original.count;
//originalValue表示参数数组的字符数组----赋值给新的变量的原因就value[]
//是final的
char[] originalValue = original.value;
char[] v;//临时定义的字符数组
//参数的字符数组中没有存满有效字符
if (originalValue.length > size) {
int off = original.offset;//----赋值给新的变量:offset也是final的
/*
注意Arrays的这个静态方法的返回值:
a new array containing thespecified range from the original
array, truncated or padded withnull characters to obtain the
required length
意思是:返回包含原数组指定范围的一个新的数组。
为了能获取(off, off + size)这个长度:
如果原数组数组不够这么长,用null字符串来填充以达到指定长度
*/
v =Arrays.copyOfRange(originalValue, off, off + size);
//v指向了新的数组
}else {//参数的字符数组中正好存满有效字符
v= originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
3. 内存图演示
(1). 回答问题1:“a”是存储在常量池的字符串
(2). 回答问题2:
String s1=newString ("abc"); 通过上面的构造方法初始化之后,常量池中的" abc"和这个新在堆内存中的对象s1已经没有关系了。
(3). 内存图演示
----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
- 黑马程序员--05.String字符串类--01. String s1=new String (a)内存图【个人总结】
- 解析String s="a" 和 String s1=new String("a")生成时,内存中的变化
- 黑马程序员--05.String字符串类--01.【String类概述】【内存中的方法区】
- String s1=new String("haha")
- 黑马程序员---string字符串总结
- 黑马程序员--字符串String总结
- 黑马程序员--字符串String总结
- 黑马程序员 string静态字符串个人统计
- String s1 = new String("abc"); String s2 = ("abc");
- String s1="a"和 Sting s2= new String("a")的区别
- 黑马程序员 String字符串
- String s1 ; String s2=null ; String s3=”” ; String s4=new String(“”) ; 的区别。
- 黑马程序员-String总结
- 黑马程序员-----String总结
- 关于String s1 = new String("abc") 创建一个对象问题和Java常量池总结
- 从内存角度去分析String s1= "abc"与String s2 = new String("abc")的问题
- 使用java中汇编指令解析String对象(String s1="a";String s2=s1+"b";String s3=new String("a");String s4=s3+"a";)
- String a="1"; String a=new String(); String a=new String("1")区别
- 【rmzt】柯南牵手&&小兰xp主题_7.23
- POJ2244"Eeny Meeny Moo"
- 多码流RM文件的解析
- 关于我最近看的一本书——大名鼎鼎的APUE
- 斐波纳契数列 线段树的维护
- 黑马程序员--05.String字符串类--01. String s1=new String (a)内存图【个人总结】
- linux内存屏障
- Berkeley DB 5.2 Android平台编译过程介绍
- 网页之间信息传递之四cookie
- http://blog.csdn.net/x1135768777/article/category/1113229
- 机房登陆——三层实践
- ctags的使用及相关参数介绍
- nodejs学习笔记
- Win7系统解决JAVA或者APKtool不是内部或者外部命令