Number of bytes in Java for various data types

Source: Internet
Author: User

1 bytes (byte) =8bit

Basic types of Java

Type Occupied bytes
Byte 1
Short 2
Int 4
Long 8
Float 4
Double 8
Char 2

The characters in a string are not the same as those of Chinese characters, and are related to encoding

English alphabet: A

Number of bytes Coding
1 GB2312
1 GBK
1 GB18030
1 Iso-8859-1
1 UTF-8
4 UTF-16
2 Utf-16be
2 Utf-16-le

Chinese characters: I

Number of bytes Coding
1 GB2312
2 GBK
2 GB18030
1 Iso-8859-1
3 UTF-8
4 UTF-16
2 Utf-16be
2 Utf-16-le

Appendix: Code that calculates the number of bytes in a string

Package Com.dingrui.stringbytelength;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import org.dozer.loader.api.FieldDefinition; public class Stringbytelength {/* * GB2312 GBK GB18030 iso-8859-1 UTF-8 UTF-16 utf-16be utf-16le */public static void main (string[] args) throws IOException {String S1 = "a"; String s2 = "Tantrayana"; SYSTEM.OUT.PRINTLN (S1 + "GB2312" + s1.getbytes ("GB2312"). length); SYSTEM.OUT.PRINTLN (S1 + "GBK" + s1.getbytes ("GBK"). length); SYSTEM.OUT.PRINTLN (S1 + "GB18030" + s1.getbytes ("GB18030"). length); SYSTEM.OUT.PRINTLN (S1 + "iso-8859-1" + s1.getbytes ("iso-8859-1"). length); SYSTEM.OUT.PRINTLN (S1 + "UTF-8" + s1.getbytes ("UTF-8"). length); SYSTEM.OUT.PRINTLN (S1 + "UTF-16" + s1.getbytes ("UTF-16"). length); SYSTEM.OUT.PRINTLN (S1 + "Utf-16be" + s1.getbytes ("Utf-16be"). length); SYSTEM.OUT.PRINTLN (S1 + "Utf-16le" + s1.getbytes ("Utf-16le"). length); System.out.println ("---------------"); System. OUT.PRINTLN (s2 + "GB2312" + s2.getbytes ("GB2312"). length); SYSTEM.OUT.PRINTLN (s2 + "GBK" + s2.getbytes ("GBK"). length); SYSTEM.OUT.PRINTLN (s2 + "GB18030" + s2.getbytes ("GB18030"). length); SYSTEM.OUT.PRINTLN (s2 + "iso-8859-1" + s2.getbytes ("iso-8859-1"). length); SYSTEM.OUT.PRINTLN (s2 + "UTF-8" + s2.getbytes ("UTF-8"). length); SYSTEM.OUT.PRINTLN (s2 + "UTF-16" + s2.getbytes ("UTF-16"). length); SYSTEM.OUT.PRINTLN (s2 + "utf-16be" + s2.getbytes ("Utf-16be"). length); SYSTEM.OUT.PRINTLN (s2 + "Utf-16le" + s2.getbytes ("Utf-16le"). length);} /** * Writes the transcoded text to the file, opens the test through the editor, confirms that the corresponding code is written * * @param bytes * @throws ioexception */public void WriteFile (byte[] bytes) throw s IOException {String path = StringByteLength.class.getResource ("/"). GetPath (); SYSTEM.OUT.PRINTLN (path); String File_path = path + "Charsetfile"; File File = new file (File_path), if (!file.exists ()) {file.createnewfile ();} FileOutputStream out = new FileOutputStream (file); out.write (bytes); Out.close ();}}

  

Number of bytes in Java for various data types

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.