ÔÚJava ÖУ¬´æ´¢×Ö·ûµÄÊý¾ÝÀàÐÍÊÇchar ¡£µ«ÊÇ£¬C/C++ ³ÌÐòԱҪעÒ⣺Java µÄcharÓëC »òC++ ÖеÄchar²»Í¬¡£ÔÚC/C++ ÖУ¬charµÄ¿íÊÇ8λÕûÊý¡£µ«Java µÄÇé¿ö²»Í¬¡£Java ʹÓÃUnicode Âë´ú±í×Ö·û¡£Unicode ¶¨ÒåµÄ¹ú¼Ê»¯µÄ×Ö·û¼¯ÄܱíʾÆù½ñΪֹÈËÀàÓïÑÔµÄËùÓÐ×Ö·û¼¯¡£ËüÊǼ¸Ê®¸ö×Ö·û¼¯µÄͳһ£¬ÀýÈçÀ¶¡ÎÄ£¬Ï£À°Ó°¢À²®Ó¹Å´ú˹À·òÓϣ²®À´ÓÈÕÎÄƬ¼ÙÃû£¬ÐÙÑÀÀûÓïµÈµÈ£¬Òò´ËËüÒªÇó16λ¡£ÕâÑù£¬Java ÖеÄchar ÀàÐÍÊÇ16룬Æ䷶ΧÊÇ0¡«65,536£¬Ã»ÓиºÊýµÄchar ¡£ÈËÃÇÊìÖªµÄ±ê×¼×Ö·û¼¯ASCII ÂëµÄ·¶Î§ÈÔÈ»ÊÇ0¡«127 £¬À©Õ¹µÄ£¸Î»×Ö·û¼¯ISO-Latin-1 µÄ·¶Î§ÊÇ0¡«255¡£¼ÈÈ»Java ±»Éè¼ÆΪÔÊÐíÆ俪·¢µÄapplet £¨Ð¡Ó¦ÓóÌÐò£©ÔÚÊÀ½ç·¶Î§ÄÚʹÓã¬Òò´ËʹÓÃUnicode Âë´ú±í×Ö·ûÊÇ˵µÃͨµÄ¡£µ±È»£¬Unicode µÄʹÓöÔÓÚÓ¢Óï¡¢µÂÓï¡¢Î÷°àÑÀÓï»ò·¨ÓïµÄÓïÑÔÊÇÓеãµÍЧ£¬ÒòΪËüÃǵÄ×Ö·ûÄÜÈÝÒ׵ر»°üº¬ÔÚ8λÒÔÄÚ¡£µ«ÊÇΪÁËÈ«ÇòµÄ¿ÉÒÆÖ²ÐÔ£¬ÕâÒ»µã´ú¼ÛÊDZØÐëµÄ¡£
×¢Ò⣺ÓйØUnicode ÂëµÄ¸ü¶àÐÅÏ¢¿ÉÔÚÍøÖ·http://www.unicode.org ÉÏÕÒµ½¡£
ÏÂÃæµÄ³ÌÐòʾ·¶ÁËchar ±äÁ¿£º
// Demonstrate char data type.class CharDemo {public static void main(String args[]) { char ch1£¬ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2£º");System.out.println(ch1 + " " + ch2);
}
}
¸Ã³ÌÐòµÄÊä³ö½á¹ûÈçÏ£ºch1 and ch2£ºX Y
×¢Òâ±äÁ¿ch1 ±»¸³Öµ88£¬ËüÊÇASCII Â루Unicode ÂëÒ²Ò»Ñù£©ÓÃÀ´´ú±í×ÖĸXµÄÖµ¡£Ç°ÃæÒÑÌáµ½£¬ASCII ×Ö·û¼¯Õ¼ÓÃÁËUnicode ×Ö·û¼¯µÄÇ°127¸öÖµ¡£Òò´ËÒÔÇ°ÄãʹÓùýµÄһЩ×Ö·û¼¼ÇÉÔÚJava ÖÐͬÑùÊÊÓá£
¾¡¹Üchar ²»ÊÇÕûÊý£¬µ«ÔÚÐí¶àÇé¿öÖÐÄã¿ÉÒÔ¶ÔËüÃǽøÐÐÔËËã²Ù×÷¾ÍºÃÏñËûÃÇÊÇÕûÊýÒ»Ñù¡£ÕâÔÊÐíÄã¿ÉÒÔ½«2¸ö×Ö·ûÏà¼Ó£¬»ò¶ÔÒ»¸ö×Ö·û±äÁ¿Öµ½øÐÐÔöÁ¿²Ù×÷¡£ÀýÈ磬¿¼ÂÇÏÂÃæµÄ³ÌÐò£º
// char variables behave like integers.
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
Õâ¸ö³ÌÐòµÄÊä³ö½á¹ûÈçÏÂËùʾ£º
ch1 contains X
ch1 is now Y
ÔڸóÌÐòÖУ¬ch1Ê×Ïȱ»¸³ÖµÎªX¡£È»ºó±äÁ¿ch1µÝÔö£¨×ÔÔöÁ¿1£©¡£½á¹ûÊÇch1 ±ä³ÉÁË´ú±íY£¬¼´ÔÚASCII £¨ÒÔ¼° Unicode £©×Ö·û¼¯ÖеÄÏÂÒ»¸ö×Ö·û¡£