java string 编码,Java中的String的字符编码是什么?

本文探讨了Java中字符串的内存表示,指出字符串是对象,通常包含字符数组和长度信息,使用Unicode字符集,通常是UTF-16编码。当赋值一个字符如'a'时,它被转换为对应的Unicode编码(如ASCII)。字符串的内存占用取决于JVM实现和类的编码。此外,字符串存储还涉及到额外的对象开销,如类描述符、锁控制字等。
摘要由CSDN通过智能技术生成

I am actually confused regarding encoding of string in a language. I had some of these questions .... Please help me if you know the answer to them....

1) What is the native encoding of java strings in memory ie when I write String a = "Hello" in which format will it be stored. Since java is machine independent so I don't think it will be the encoding done by the system ??

2) I read this on the net that "UTF-16" is the default encoding but I got confused because say when I write that int a = 'c' the default answer gives me ASCII so are ASCII and UTF-16 same ??

3) Also I had this doubt that on what factors the storage of a string in the memory depend os , language ??

解决方案

1) Strings are objects, which typically contain a char array and the strings's length. The character array is usually implemented as a contiguous array of 16-bit words, each one containing a Unicode character in native byte order.

2) Assigning a character value to an integer converts the 16-bit Unicode character code into its integer equivalent. Thus 'c', which is U+0063, becomes 0x0063, or 99.

3) Since each String is an object, it contains other information than its class members (e.g., class descriptor word, lock/semaphore word, etc.).

ADENDUM

The object contents depend on the JVM implementation (which determines the inherent overhead associated with each object), and how the class is actually coded (i.e., some libraries may be more efficient than others).

EXAMPLE

A typical implementation will allocate an overhead of two words per object instance (for the class descriptor/pointer, and a semaphore/lock control word); a String object also contains an int length and a char[] array reference. The actual character contents of the string are stored in a second object, the char[] array, which in turn is allocated two words, plus an array length word, plus as many 16-bit char elements as needed for the string (plus any extra chars that were left hanging around when the string was created).

Take a look at the actual source code for Apache's implementation, e.g. at:

http://www.docjar.com/html/api/java/lang/String.java.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值