package test;
import java.io.UnsupportedEncodingException;
/**
* Classname :
* ClassType :
*
* @version $Revision$
* @author $author$
*/
public class HelloWorld {
String sEnglish = "Apple";
String sChinese = "苹果";
String mixString = "Apple 苹果";
/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
* @throws UnsupportedEncodingException
*/
public static void main(String[] args)
throws UnsupportedEncodingException {
System.out.println("Hello, World!");
HelloWorld hw = new HelloWorld();
System.out.println("sEnglish String length: " + hw.sEnglish.length());
System.out.println("sChinese String length: " + hw.sChinese.length());
System.out.println("mixString String length: " + hw.mixString.length());
System.out.println("********************************************************************");
System.out.println("UTF-8 Bytes length: "
+ hw.sEnglish.getBytes("UTF-8").length);
System.out.println("UTF-8 Bytes length: "
+ hw.sChinese.getBytes("UTF-8").length);
System.out.println("UTF-8 Bytes length: "
+ hw.mixString.getBytes("UTF-8").length);
System.out.println("********************************************************************");
System.out.println("GBK Bytes length: "
+ hw.sEnglish.getBytes("GBK").length);
System.out.println("GBK Bytes length: "
+ hw.sChinese.getBytes("GBK").length);
System.out.println("GBK Bytes length: "
+ hw.mixString.getBytes("GBK").length);
System.out.println("********************************************************************");
System.out.println("UTF-16 Bytes length: "
+ hw.sEnglish.getBytes("UTF-16").length);
System.out.println("UTF-16 Bytes length: "
+ hw.sChinese.getBytes("UTF-16").length);
System.out.println("UTF-16 Bytes length: "
+ hw.mixString.getBytes("UTF-16").length);
System.out.println("********************************************************************");
System.out.println("Unicode Bytes length: "
+ hw.sEnglish.getBytes("Unicode").length);
System.out.println("Unicode Bytes length: "
+ hw.sChinese.getBytes("Unicode").length);
System.out.println("Unicode Bytes length: "
+ hw.mixString.getBytes("Unicode").length);
System.out.println("********************************************************************");
System.out.println("Default Bytes length: "
+ hw.sEnglish.getBytes().length);
System.out.println("Default Bytes length: "
+ hw.sChinese.getBytes().length);
System.out.println("Default Bytes length: "
+ hw.mixString.getBytes().length);
}
}
Result:
Hello, World!
sEnglish String length: 5
sChinese String length: 2
mixString String length: 8
********************************************************************
UTF-8 Bytes length: 5
UTF-8 Bytes length: 6
UTF-8 Bytes length: 12
********************************************************************
GBK Bytes length: 5
GBK Bytes length: 4
GBK Bytes length: 10
********************************************************************
UTF-16 Bytes length: 12
UTF-16 Bytes length: 6
UTF-16 Bytes length: 18
********************************************************************
Unicode Bytes length: 12
Unicode Bytes length: 6
Unicode Bytes length: 18
********************************************************************
Default Bytes length: 5
Default Bytes length: 4
Default Bytes length: 10