Java game programming Reading Notes

Source: Internet
Author: User
Java game programming Reading Notes-general Linux technology-Linux programming and kernel information. The following is a detailed description. Chapter 2 Preparation: Learning Java 2 APIs
Java program structure
Basic Java data types, including String and array
Numeric and conditional operators, and their prior Operation Order
Conditional statements, including control statements and loop statements
Throws statement and try-catch Exception Handling
2.1 Game Over! Program
2.1.1 import Statement
Java objects are organized into packages. To facilitate organization, the package contains related classes. Java APIs include 75 packages (about 2000 classes in total, JDK 1.4.0)
C ++ annotation: the import keyword in Java is similar to the # include Directive in C ++. The only difference is that there is no difference between the header file and the source file in java. The declaration and implementation of classes are included in A. java file.
2.1.2 add comments to the Java code
You must write comments at work, but avoid excessive comments. Excessive comments can only confuse the code and disturb attention to a large extent.
Note that C-style annotations are not nested. The first "*/" will make the latest "/*" useless.
2.1.3 Java class declaration
2.1.4 Java method declaration
C ++ annotation: The main method in Java is similar to the typical main function in the console C or C ++ application. Note that in Java, it must be defined in a class, while in C and C ++, it must be globally defined. In addition, the main method in Java always returns void. Different from the main function in C or C ++, they can have multiple return types.
2.1.5 code block in Java
2.1.6 review of key points of Java program components
Explain uses the import statement to make the compiler know the library to use
Comment can help programmers in many ways, but improper use of it will do more harm than good.
Everything written in Java by the handler is encapsulated into a class, even if the start point of the program, that is, the main method, is no exception.
Compile a code block is a statement related to a specific task. Code blocks keep the program structured and easy to read/write
2.2 bits and bytes: original Java type
What should I write in the program? The answer is: data, programs cannot survive without data, especially games.
Java is an object-oriented language. However, Java is not an object-oriented method, because Java supports eight primitive types.
2.2.1 basic integer type
Generally, for game development, the int type is generally used to represent integer data, while the long, short, and byte types are ignored. Because the value range of the int type is greater than 4 billion, it is usually safer to use.
2.2.2 floating point type
A constant floating-point value must be regarded as a float value and end with the suffix f (or F). Otherwise, it is treated as a double value. For double values, the suffix d (or D) is optional.
So far, it is not clear which type of floating point is usually used to store floating point numbers. In essence, the Double Precision gives us twice the precision compared to the floating point number. However, this is costly-usually the memory consumption. The double type is used for the return type and data member of most methods in Java APIs.
2.2.3 Char type
A character (char) in Java represents an element in the Unicode Character Set. Unicode characters consist of 16 characters. Therefore, 216 (65535) different characters are available, which is different from the standard 128 ASCII characters in C ++. The Unicode Character Set gives us a lot of flexibility. It can contain characters of all different languages, as well as common symbols in mathematics, science, and text.
A single character enclosed by single quotes is usually expressed in hexadecimal notation, the range is from '\ u0000' to' \ uffff' (u tells the compiler that you are using two [16-bit] characters to represent a Unicode character ).
C ++ Note: Unlike C ++, a character array in Java does not need to be a string. Later, we will see that Java API defines its own String type. Generally, only sensitive information, such as password fields, is processed as character arrays. This is because Java objects reside in the memory until they are cleared by the Java Virtual Machine as non-referenced objects. If a savvy hacker goes into the system, leaving sensitive data in the memory may cause a dangerous situation.
For more information about the Unicode Character Set and a complete list of characters, go to the http://www.unicode.org site.
2.2.4 Boolean
In Java, any boolean variable has only two valid values: true and false. Type conversion is not allowed because the boolean type does not have an equivalent value assignment. Also, note that all the if and while statements are equivalent to a boolean result.
2.2.5 String type
The String type in Java is not the original type! It is actually a Java class.
Copy an array:
To copy the values in an array to another array, you can use the pre-defined arrayCopy method in the System class.
System. arrayCopy (Object src, int src_position, Object dst, int dst_position, int length );
Src indicates the source array to be copied from, src_position indicates the start subscript of the source array, dst indicates the destination array to be copied, and dst_option indicates the start subscript of the destination array, length specifies the number of elements in the copied array.
Multi-dimensional array:
Array can make the problem variable very simple, but if used improperly, the problem will become abnormal and complex. If you are unsure about whether to use arrays to solve a problem, the following prompt may be helpful:
Explain in brief on the paper to solve the problem. If data cannot be organized in rows or columns, using arrays may not be the best solution.
Before writing your own array tools, such as sorting and query algorithms, refer to Java API. Generally, it is better to start from the Java. util. Arrays package. Without doubt, the Java package will save you time and it is difficult to write code that is more effective than the algorithms provided in the API.
Although Arrays can be defined as three, four, five, or more dimensions, it is very complicated to replace these two-dimensional arrays. If you need to use an array of more than three dimensions, try to check whether it can be designed or find another data structure for use. This is likely to solve the problem more concisely and effectively than a tree.
2.2.6 force conversion of variable types
C ++ annotation: Unlike C ++, the Java boolean type has no equivalent value assignment. Therefore, there is no way to directly convert a boolean to an int variable. The fastest way to convert a boolean value to another type may be as follows:
Boolean gameStarted = true;
Int game = gameStarted? 1:0;
Although there is no strong conversion here, a line of code can also be converted.
Finally, pay attention to the variables. In Java, there is no typedef operator, and there is no operation equivalent to the # define precompiled command. Although this may take some time to adapt, it will help eliminate errors and make the code clearer. Therefore, for those who like to write a lot of "mysterious" code, they can no longer define a variable type such as fa26b9.
2.2.7 key points for memorizing Java data types, arrays, and identifiers
Bytes, short, int, long, float, double, boolean, and char in Java are not Java classes, but they are indispensable parts of the class.
Arrays in objective Java can be roughly written in the same way as those in C and C ++, but Java provides an additional long attribute for accessing the total number of array elements.
Implicit forced conversion is a good way to convert a variable from one type to another type, but be careful with the potential side effects of this method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.