关于java常量存储方式的问题,大家帮忙看一下
我接手前同事写的烂Java代码,不小心搞出了一个内存泄露事故 今天给大家聊聊咱们平时写代码的时候,最常见的 String 字符串代码,他的一些底层原理,以及使用不当可能引发的内存泄漏的问题。 阅读详情
最近学习java,有个String的小问题请教一下大伙,大家帮忙看一下!
String str=new String("Hello");
System.out.prinln(str.length());//输出为5
本人查了一下String的源码,有如下几个构造方法:
public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
public String(char value[]) {
int size = value.length;
this.offset = 0;
this.count = size;
this.value = Arrays.copyOf(value, size);
}
public int length() {
return count;
}
对于str.length(),返回的是Count的值即字符串的长度,我想问的是对于new String("Hello")调用的是哪个构造方法,如果调用的是String(String original),那么在这个函数中Hello的长度是怎么来的,编译器如何解释这部分,大虾们帮忙看一下!!!
String str=new String("Hello");
System.out.prinln(str.length());//输出为5
本人查了一下String的源码,有如下几个构造方法:
public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
public String(char value[]) {
int size = value.length;
this.offset = 0;
this.count = size;
this.value = Arrays.copyOf(value, size);
}
public int length() {
return count;
}
对于str.length(),返回的是Count的值即字符串的长度,我想问的是对于new String("Hello")调用的是哪个构造方法,如果调用的是String(String original),那么在这个函数中Hello的长度是怎么来的,编译器如何解释这部分,大虾们帮忙看一下!!!
...全文
相关推荐
深入剖析Java虚拟机核心机制与性能调优实战指南_涵盖类加载器双亲委派机制源码解读字节码文件结构分析JVM内存模型堆栈方法区常量池详解CPU飙高问题诊断排查流程Java对象.zip
常量池中的条目分为类、字段和方法引用,每种引用类型都有其独特的存储方式和访问机制。最后,当系统遇到CPU使用率异常飙升的问题时,如何快速诊断和排查成为了性能调优的重要环节。
面试常见问题-Java
Java 面试常见问题以下是 Java 面试中常见的问题和知识点:一、基本数据类型和封装类* Java 中有九种基本数据类型:boolean、byte、char、short、int、long、float
Java 内存分析
#### 七、非RAM存储非RAM存储通常指的是硬盘等永久存储空间,Java程序中的持久化数据通常存储在这里。这种存储方式适用于需要长期保留的数据,例如数据库中的记录等。
Java运行时内存分配
这些问题不仅对于深入理解Java虚拟机(JVM)的工作原理至关重要,同时也直接影响到程序的性能与稳定性。本文将详细探讨Java运行时内存分配的六个关键存储区域。
java笔记整理(超详细).pdf
Error(错误):是程序无法处理的错误,表明运行应用程序中的严重问题。大多数错误与代码编写者执行的操作无关,而表明代码运行时JVM(Java虚拟机)出现的问题。


