Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Review I Java Overview last updated 19-dec-18 The following topics and Java language are assumed to be understood in the text. Be sure you understand them or ask questions for more examples as we proceed into the text.   Philosophy of Java Python is "interpreted". It is translated/compiled behind the scenes prior to execution. You just run it and its code is interpreted internally after a quick syntax check. There is no file generated containing the translation. Everytime you run the Python program it may get rescanned for syntax issues. C/C++/C# and many older languages are compiled/translated to machine code (exe files) for a specific machine. The translated code may only work on an Intel chip or a Motorola chip or an IBM mainframe. To run the same program on two different machines requires separate compilation for both machines. Java is "compile once", run anywhere. The compilation generates a .class file which is then interpreted by Java Virtual Machines running on different platforms. This makes it Internet friendly. Class files are independent of the machine. (Python, JavaScript, PHP are all interpreted and are platform independent as well). There are speed issues. Java is "object oriented (OO)" Your Python background may be limited in OO thinking. This will develop over the course. Don't dispair. Objects are the interacting components of a Java program. They represent, simulate or act as real world things (nouns). Objects are described by classes. Objects are instantiations of those classes. Classes define the components of an object, sometimes called instance variables, and the actions the object can perform on its components, which are called methods (verbs). Programming in Java requires you to think about objects, their organization, their actions and how they interconnect much more than many other languages. Declarations and Types Java is strongly typed: every variable is fixed to a particular type in a declaration statement (DIFFERENT from Python!) Built-in Java types: byte, short, int, long, float, double, char and boolean. These are NOT objects (unfortunately). Object management in Java is expensive in processing time. Primitives align very closely to the computer hardware so that processing speed them is maximized. > char letter = 'a'; > System.out.println(letter); a > boolean flag = true; > System.out.println(flag) true > float f = 13.2f > f 13.2 > double d; > d = 13.231; > d 13.231 > d+f 26.43099980926514 > letter=f Static Error: Bad types in assignment: from float to char > int i =(int) f 13 Variables of primitive types actually hold the values of the variables. E.g., the integer values 25 or -40 or double value 3.14159269 are actually stored in the memory location associated with the variable. Variables associated with objects simply point to the object. The object value (values of its instance variables) is stored in memory specific for objects and the variables simply hold the address of the memory location. Strings are objects. Object variables may refer to nothing. Its value is then null. Null, conceptually is not the value zero (although it is in the hardware). Assignment statement/operation primitiveVariable = expression; + - * / % & | objectVar = new Class ; //instantiation objectVar = anotherObjectVar //or a method that returns an object;   Conditionals if (condition) { then-statement or block of statements } else { else-statement or block of statements } Please note the caveats about indentation and non-use of { }. Always use braces around the blocks even if it is just one statement. < <= > >= == and != as comparison operations for numeric values ONLY--never objects or Strings! && and || as compound condition connectors/operators Looping control for (type var : array) { loop body } > int[] numbers = {3, 6, 6, 2, 5}; > int total = 0; > for (int i : numbers){ total += i; > System.out.println(total); 22 for (initialization ; condition ; increment) { loop body } > for(int k = 0 ; k < 10; k = k+1){ System.out.println(k); } > for(k = 1 ; k<=10 ; k++) ... > for(k = 10 ; k > 0 ; k--) ... while (condition) { loop body } > k = 1; > while (k < 1000) { System.out.println(k); k = k * 2; } Arrays Python lists. We will need to distinguish lists from arrays in Java. Declare an array with type [ ] name. Note array elements are NOT initialized. type [] arrayName = new type[size]; int [] ages = new int[5]; ages[0] = 18; ages[1] = 21; ages[2] = 19; ages[3] = 33; ages[4] = 22; ages[5] = 20; /****last statement will cause runtime error: ArrayIndexOutOfBoundsException: at java.lang.reflect.Array.get(Native Method) */ for (int age : ages){ System.out.println(age); } // often the whole array is not used, so the following is common for(int i=0; i<3; i++){ System.out.println(ages[i]; } int i = 0; while(ages[i]<22){ System.out.println(ages[i]); }