Relationship between bit, byte, letters, and Chinese characters in Java

Source: Internet
Author: User

[Java]
Package com. jadyer. demo;
 
/**
* Relations between characters in Java including bit, byte, letter, and Chinese Character
* @ See ================================
* @ See 1 bit = 1 binary data
* @ See 1 byte = 8 bit (range:-128 to + 127)
* @ See 1 letter = 1 byte = 8 bit
* @ See 1 Chinese Character = 2 byte = 16 bit
* @ See ================================
* @ Author Xuan Yu * @ Create Aug 20,201 2 10:28:20 AM
*/Www.2cto.com
Public class ByteDemo {
/**
* @ See console:
* @ See 4
* @ See 4
* @ See ----------------------------
* @ See str = ABab
* @ See 65
* @ See 66
* @ See 97
* @ See 98
* @ See ----------------------------
* @ See name = Xuan Yu
* @ See-48
* @ See-2
* @ See-45
* @ See-15
* @ See ----------------------------
*/
@ SuppressWarnings ("unused ")
Public static void main (String [] args ){
/Byte bb11 = 128; // cannot convert from int to byte
// Byte bb22 =-129; // cannot convert from int to byte
Byte bb11 = 127;
Byte bb22 =-128;
Byte bb33 = 56;

// Byte bb44 = 'xuan'; // error, because: 1 Chinese Character = 2 bytes
// Byte bb55 = 'bb'; // error, because: 'bb' = 2 bytes
Byte bb44 = 'B ';
Byte bb55 = 'B ';
Byte bb66 = 'Z ';

// Int i11 = ""; // 1 int = 32 bit, but int is numeral
// Short ss = 'xuanyu '; // 1 short = 2 byte = 16 bit = 1 Chinese Character
Short ss11 = 'xuan ';

// Char cc11 = 'xuanyu '; // This is not acceptable
// Char cc22 = 'cc'; // This is not acceptable
Char cc11 = 'xuan ';
Char cc22 = 'C ';

String str = "ABab ";
Byte [] data = str. getBytes ();
System. out. println (str. length ());
System. out. println (data. length );
System. out. println ("----------------------------");

System. out. println ("str =" + new String (data ));
For (int I = 0; I <data. length; I ++ ){
System. out. println (data [I]);
}
System. out. println ("----------------------------");

Byte [] name = "". getBytes ();
System. out. println ("name =" + new String (name ));
For (int I = 0; I <name. length; I ++ ){
System. out. println (name [I]);
}
System. out. println ("----------------------------");
}
}

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.