Basics about the length of basic types in Java

Source: Internet
Author: User

1. How many bytes does an int occupy with Heapbytebuffer?

Accounted for 4 of them. and whether you put 1 or 1 or 0xffff.

Because the length of int is 4 Byte,heapbytebuffer storage is also a byte array.

2. Use Heapbytebuffer to put char in a few bytes?

The same 1, is two, because the length of Char in Java is 2byte.

In the same way, both the English character and the save Chinese characters occupy 2 bytes.

Sample code

Bytebuffer buff = bytebuffer.allocate (1024*1024); Buff.putchar (' C '); System.out.println (Buff.position ()); Buff.putchar (' Medium '); System.out.println (Buff.position ()); Buff.flip (); char ch1 = Buff.getchar (); char CH2 = Buff.getchar (); System.out.println ("CH1:" + ch1 + ", CH2:" + CH2);

How does the word ' medium ' in the previous example be encoded when running in the JVM?

See Java Virtual Machine specification

For more information, see

Is the "Unicode encoding used by the Java language" referring to the Jvm?.java file?

3. What is the difference between Unicode and utf-8?

See what is the difference between Unicode and UTF-8? The title question is not very strict.

4. What are the options for the integer "Wasted Space" in 1

Refer to the Org.h2.mvstore.DataUtils.writeVarInt (bytebuffer, int) method of the H2 database for on-demand use.

Basics about the length of basic types in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.