Pourquoi ne pas tenir#39;t Java paramètre#39;s +=, -=, *=, /= les opérateurs d'assignation de composés le font

Mots clés : javacastingoperatorsvariable-assignmentassignment-operatorjava

meilleur 5 Réponses Pourquoi ne pas tenir#39;t Java paramètre#39;s +=, -=, *=, /= les opérateurs d'assignation de composés le font

vote vote

92

short x = 3; x += 4.6; 
short x = 3; x = (short)(x + 4.6); 
vote vote

88

byte b = 10; b *= 5.7; System.out.println(b); // prints 57 
byte b = 100; b /= 2.5; System.out.println(b); // prints 40 
char ch = '0'; ch *= 1.1; System.out.println(ch); // prints '4' 
char ch = 'A'; ch *= 1.5; System.out.println(ch); // prints 'a' 
vote vote

78

short x = 3; x += 4.6; 
short x = 3; x = (short)(x + 4.6); 
vote vote

64

i += l;  
i = (int)(i + l); 
vote vote

51

i = i + (int)l; 
i = (int)((long)i + l); // this is what happens in case of += , dont need (long) casting since upper casting is done implicitly. 

Questions similaires