JAVA里String数组在内存分配中分配的空间每个占几个字节?
publicclassCar{Stringname;publicCar(Stringname){this.name=name;}}publicclassTest01{pu...
public class Car {
String name;
public Car(String name){
this.name = name;
}
}
public class Test01 {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 20;
a[2] = 32;
a[1] = 45;
Car[] cars = new Car[4];
cars[0] = new Car("奔驰");
}
}
比如这个程序,堆中分配4个空间给数组Car,3个空间给int,每个空间占几个字节?另外Char占几个字节? 展开
String name;
public Car(String name){
this.name = name;
}
}
public class Test01 {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 20;
a[2] = 32;
a[1] = 45;
Car[] cars = new Car[4];
cars[0] = new Car("奔驰");
}
}
比如这个程序,堆中分配4个空间给数组Car,3个空间给int,每个空间占几个字节?另外Char占几个字节? 展开
3个回答
展开全部
如果String串为纯英文,那么一个英文字母是一个字符,长度为1,占1个字节,不会出错,但如果String串中包含中文,一个中文汉字也是一个字符,长度为1,但是却占多个字节(具体占几个字节跟使用的编码有关),如果数据中包含中文,数据的长度就很有可能会超过数据库中对应字段的长度限制
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Java程序计算各种对象所占内存的大小的方法。
基类:
publicabstractclass SizeOf { privatefinal Runtime s_runtime = Runtime.getRuntime(); protectedabstract Object newInstance(); publicint size() throws Exception { runGC(); Object[] objects = new Object[count]; long heap1 = usedMemory(); for (int i = -1; i < count; ++i) { Object object = null; object = newInstance(); objects[i] = object; object = null; runGC(); heap1 = usedMemory(); } } runGC(); long heap2 = usedMemory(); finalint size = Math.round(((float) (heap2 - heap1)) / count); for (int i = 0; i < count; ++i) { objects[i] = null; } } privatevoid runGC() throws Exception { for (int r = 0; r < 4; ++r) { _runGC(); } } privatevoid _runGC() throws Exception { long usedMem1 = usedMemory(); long usedMem2 = Long.MAX_VALUE; for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) { s_runtime.runFinalization(); s_runtime.gc(); Thread.currentThread().yield(); usedMem2 = usedMem1; usedMem1 = usedMemory(); } } privatelong usedMemory() { return s_runtime.totalMemory() - s_runtime.freeMemory(); } }
子类:
publicclass SizeOfObject extends SizeOf { protected Object newInstance() { } publicstaticvoid main(String[] args) throws Exception { SizeOf sizeOf = new SizeOfObject(); System.out.println("所占内存:" + sizeOf.size() + "字节"); } }
输出为:所占内存:8字节
基类:
publicabstractclass SizeOf { privatefinal Runtime s_runtime = Runtime.getRuntime(); protectedabstract Object newInstance(); publicint size() throws Exception { runGC(); Object[] objects = new Object[count]; long heap1 = usedMemory(); for (int i = -1; i < count; ++i) { Object object = null; object = newInstance(); objects[i] = object; object = null; runGC(); heap1 = usedMemory(); } } runGC(); long heap2 = usedMemory(); finalint size = Math.round(((float) (heap2 - heap1)) / count); for (int i = 0; i < count; ++i) { objects[i] = null; } } privatevoid runGC() throws Exception { for (int r = 0; r < 4; ++r) { _runGC(); } } privatevoid _runGC() throws Exception { long usedMem1 = usedMemory(); long usedMem2 = Long.MAX_VALUE; for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) { s_runtime.runFinalization(); s_runtime.gc(); Thread.currentThread().yield(); usedMem2 = usedMem1; usedMem1 = usedMemory(); } } privatelong usedMemory() { return s_runtime.totalMemory() - s_runtime.freeMemory(); } }
子类:
publicclass SizeOfObject extends SizeOf { protected Object newInstance() { } publicstaticvoid main(String[] args) throws Exception { SizeOf sizeOf = new SizeOfObject(); System.out.println("所占内存:" + sizeOf.size() + "字节"); } }
输出为:所占内存:8字节
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询