java±í´ïʽÖÐÀàÐ͵Ä×Ô¶¯ÌáÉý
·¢²¼Ê±¼ä:2006-05-03 15:07:56À´Ô´:ºìÁª×÷Õß:reing
³ýÁ˸³Öµ£¬»¹ÓÐÁíÍâÒ»ÖÖÀàÐͱ任£ºÔÚ±í´ïʽÖС£ÏëÒªÖªµÀÔÒò£¬ÍùÏ¿´¡£ÔÚ±í´ïʽÖУ¬¶ÔÖмäÖµµÄ¾«È·ÒªÇóÓÐʱ³¬¹ýÈκÎÒ»¸ö²Ù×÷ÊýµÄ·¶Î§¡£ÀýÈ磬¿¼ÂÇÏÂÃæµÄ±í´ïʽ£º byte a = 40; byte b = 50; byte c = 100; int d = a * b / c; ÖмäÏî½á¹ûa*b ºÜÈÝÒ׳¬¹ýËüµÄÈκÎÒ»¸öbyte ÐͲÙ×÷ÊýµÄ·¶Î§¡£Îª´¦ÀíÕâÖÖÎÊÌ⣬µ±·ÖÎö±í´ïʽʱ£¬Java ×Ô¶¯ÌáÉý¸÷¸öbyte ÐÍ»òshortÐ͵IJÙ×÷Êýµ½int ÐÍ¡£ÕâÒâζ×Å×Ó±í´ïʽa*b ʹÓÃÕûÊý¶ø²»ÊÇ×Ö½ÚÐÍÀ´Ö´ÐС£ÕâÑù£¬¾¡¹Ü±äÁ¿aºÍb¶¼±»Ö¸¶¨Îªbyte ÐÍ£¬50*40 Öмä±í´ïʽµÄ½á¹û2000 ÊǺϷ¨µÄ¡£×Ô¶¯ÀàÐÍÌáÉýÓкô¦£¬µ«ËüÒ²»áÒýÆðÁîÈËÒÉ»óµÄ±àÒë´íÎó¡£ÀýÈ磬Õâ¸ö¿´ÆðÀ´ÕýÈ·µÄ³ÌÐòÈ´»áÒýÆðÎÊÌ⣺ byte b = 50; b = b * 2; // Error! Cannot assign an int to a byte! ¸Ã³ÌÐòÊÔͼ½«Ò»¸öÍêÈ«ºÏ·¨µÄbyte Ð͵ÄÖµ50*2 ÔÙ´æ´¢¸øÒ»¸öbyte Ð͵ıäÁ¿¡£µ«Êǵ±±í´ïʽÇóÖµµÄʱºò£¬²Ù×÷Êý±»×Ô¶¯µØÌáÉýΪint ÐÍ£¬¼ÆËã½á¹ûÒ²±»ÌáÉýΪint ÐÍ¡£ÕâÑù£¬±í´ïʽµÄ½á¹ûÏÖÔÚÊÇint ÐÍ£¬²»Ç¿ÖÆת»»Ëü¾Í²»Äܱ»¸³Îªbyte ÐÍ¡£È·ÊµÈç´Ë£¬ÔÚÕâ¸öÌرðµÄÇé¿öÏ£¬±»¸³µÄÖµ½«ÈÔÈ»ÊʺÏÄ¿±êÀàÐÍ¡£ÔÚÄãÀí½âÒç³öµÄºó¹ûµÄÇé¿öÏ£¬ÄãÓ¦¸ÃʹÓÃÒ»¸öÏÔʽµÄÇ¿ÖÆÀàÐÍת»»£¬ÀýÈ磺 byte b = 50; b = (byte)(b * 2); Ëü²úÉú³öÕýÈ·µÄÖµ100 ¡£ 3.10.1 ÀàÐÍÌáÉýµÄÔ¼¶¨³ýÁ˽«byte ÐͺÍshorts ÐÍÌáÉýµ½int ÐÍÒÔÍ⣬Java ¶¨ÒåÁËÈô¸ÉÊÊÓÃÓÚ±í´ïʽµÄÀàÐÍÌáÉý¹æÔò£¨type promotion rules £©¡£Ê×ÏÈ£¬Èç¸Õ²ÅÃèÊöµÄ£¬ËùÓеÄbyte ÐͺÍshortÐ͵ÄÖµ±»ÌáÉýµ½ int ÐÍ¡£Æä´Î£¬Èç¹ûÒ»¸ö²Ù×÷ÊýÊÇlong ÐÍ£¬Õû¸ö±í´ïʽ½«±»ÌáÉýµ½long ÐÍ£»Èç¹ûÒ»¸ö²Ù×÷ÊýÊÇfloat ÐÍ£¬Õû¸ö±í´ïʽ½«±»ÌáÉýµ½float ÐÍ£»Èç¹ûÓÐÒ»¸ö²Ù×÷ÊýÊÇdouble ÐÍ£¬¼ÆËã½á¹û¾ÍÊÇdouble ÐÍ¡£ÏÂÃæµÄ³ÌÐò±íÃ÷£ºÔÚ±í´ïʽÖеÄÿ¸öÖµÊÇÈçºÎ±»ÌáÉýÒÔÆ¥Åä¸÷×Ô¶þ½øÖÆÔËËã·ûµÄµÚ¶þ¸ö²ÎÊý£º class Promote { public static void main(String args[]) { byte b = 42; char c = 'a'; short s = 1024; int i = 50000; float f = 5.67f; double d = .1234; double result = (f * b) + (i / c) - (d * s); System.out.println((f * b) + " + " + (i / c) + " - " + (d * s)); System.out.println("result = " + result);}} ÈÃÎÒÃǽøÒ»²½¿´¿´·¢ÉúÔÚÏÂÁгÌÐòÐеÄÀàÐÍÌáÉý£º double result = (f * b) + (i / c) - (d * s); ÔÚµÚÒ»¸ö×Ó±í´ïʽf*b ÖУ¬±äÁ¿b±»ÌáÉýΪfloat ÀàÐÍ£¬¸Ã×Ó±í´ïʽµÄ½á¹ûµ±È»ÊÇfloat ÀàÐÍ¡£½ÓÏÂÀ´£¬ÔÚ×Ó±í´ïʽi/c£¬ÖУ¬±äÁ¿c±»ÌáÉýΪint ÀàÐÍ£¬¸Ã×Ó±í´ïʽµÄ½á¹ûµ±È»ÊÇint ÀàÐÍ¡£È»ºó£¬×Ó±í´ïʽd*s ÖеıäÁ¿s±»ÌáÉýΪdouble ÀàÐÍ£¬¸Ã×Ó±í´ïʽµÄ½á¹ûµ±È»Ò²ÊÇdouble ÀàÐÍ¡£×îºó£¬¿¼ÂÇÈý¸öÖмäÖµ£¬float ÀàÐÍ£¬int ÀàÐÍ£¬ºÍdouble ÀàÐÍ¡£float ÀàÐͼÓint ÀàÐ͵Ľá¹ûÊÇfloat ÀàÐÍ¡£È»ºófloat ÀàÐͼõÈ¥ÌáÉýΪdouble ÀàÐ͵Ädouble ÀàÐÍ£¬¸Ã±í´ïʽµÄ×îºó½á¹ûÊÇdouble ÀàÐÍ¡£