What's wrong with short s1=1;s1=s1+1? short s1=1;s1+=1; what's wrong?
The first one is wrong, short is 2 bytes in memory, and integer 1 defaults to intType 4 wordssection, s1+1 in fact this time on the upward transition to the int type, so the firstLine code must be strong to go. The second reason can be thought that this sentence translation comess1++, which is the short type of dataIncrease by 1, so there is no problem. Java does not have sizeofJava does not require the sizeof () operator to meet this need because all data types are in allThe size of the machine is the same. We don't have to think about porting-it has been designed in the language.foreach SyntaxJava SE5 introduces a new, more concise for syntax for arrays and containers, the foreach syntax, which means that you do not have to create an int variable to count the sequence of accesses, and foreach automatically generates each item. For example, suppose you have a float array, and we want to select each element in the array:
1 ImportJava.util.Random;2 3 Public classForeachfloat {4 Public Static voidMain (string[] args) {5Random Rand =NewRandom (47);6 floatF[] =New float[10];7 for(inti = 0; I < 10; i++) {8F[i] =rand.nextfloat ();9 }Ten for(floatx:f) { One System.out.println (x); A } - } -}
This array is assembled with a legacy for loop, because it must be accessed by index at assembly time, and the foreach syntax can be seen in the following line: for (float x:f) {This statement defines a variable x of type float, and then assigns the element of each F to X.PS:The foreach syntax will not work unless an array is created.calling the constructor in the constructor1. You cannot call two in the same constructor, 2. You must place the constructor call at the very beginning; 3. You cannot call the constructor in any method other than the constructor. For example:
1 Public classFlower {2 intPetalcount = 0;3String s = "Initial value";4 5Flower (intperals) {6Petalcount =perals;7System.out.println ("Constructor w/int arg only, petalcount=" +petalcount);8 }9 Ten Flower (String ss) { OneSystem.out.println ("Constructor w/int arg only, s=" +ss); As =SS; - } - theFlower (String S,intpetals) { - This(petals); - //! This (s); //can't call two! - This. S =s; +System.out.println ("String & int args"); - } + A Flower () { at This("Hi", 45); -System.out.println ("default constructor (no args)"); - } - - voidPrintpetalcount () { - //! This (11); //constructors cannot be called in any method other than the constructor inSystem.out.println ("Petalcount =" + Petalcount + "s =" +s); - } to + Public Static voidMain (string[] args) { -Flower x =NewFlower (); the X.printpetalcount (); * } $}
Java--java