Java Programming for absolute beginner- P27 ppt

20 163 0
Java Programming for absolute beginner- P27 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Java SDK Click on the Java SDK button to get to this page. From this page, you can run the installation file, which will install Java SDK, version 1.3.1. There are different files depending on which operating system you have. Look for the file that is right for your operating system. If you have any Windows version, look under the Microsoft section. If you are using Linux, look under the Linux x86 section. If you want to install the SDK on a Solaris machine, look under the Solaris SPARC/x86 section. You can find the instructions for installing and setting up the SDK for your system in Chapter 1, “Getting Started.” You can also find the Java Documentation installation here. You can either install the documentation on your system or browse it online on Sun’s Web site by using the URL http://java.sun.com/j2se/1.3/docs/. Source Code All of the source code of the examples in this book can be found on the CD-ROM. Click on the Source Code button to get to the source code page. The source code and class files are organized under their respective chapters. Each chapter gives you the option to either browse or download the files. If you chose to download, click the Download button. The download file is in ZIP format, so you will need an unzip tool, such as Winzip (included on the CD-ROM), to unpack the files into the local directory of your choosing. If you choose to browse the files without sav- ing them to your local disk, click Explore. A new window will open up that con- tains the files associated with that chapter. The file extension for the source code is .java. You can open this file with any text editor since it is a straight text file. It is still possible to run the example programs without installing them locally. To do this, you need to open up a shell (command prompt) and change to the class file directory. For example, if you’re using Windows and your CD-ROM drive letter is D, to run the HelloWorld example from Chapter 1, you need to change to the directory D:\Source Code\Chapter 1\. Next, you need to run the java com- mand on the program. (Note: you can only do this after you’ve installed the SDK.) For example, this is what you type at your command prompt to run the Hel- loWorld application: java HelloWorld. 478 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-50A.qxd 2/25/03 8:58 AM Page 478 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Web Links The Web Links section includes links to some useful Internet sites. To get to this section, simply click the Web Links button. The links you will find there are: Sun Java This takes you directly to Sun’s Java Web site. (http://java.sun.com). Sun Microsystems This takes you to Sun’s main Web site. Sun Microsystems is the innovator behind Java technology. (http://www.sun.com). Sun Educational Services Here you can learn how to further your Java education and become a Sun-certified Java programmer! (http://suned.sun.com). East Coast Games A site written by programmers that you can use as a resource for your video game pro- gramming endeavors. (http://www.east- coastgames.com). NetBeans NetBeans is an open source integrated devel- opment environment (IDE) that you can download and use to facilitate your Java appli- cation programming projects (http://www.net- beans.org). Programs This section includes some programs that you can install and use on your system. To get to this section, click on the Programs button. For installation instructions, visit the Web site link, which is under the program name listing on this page. The programs are: Cool Edit Pro This is a demo version of a very cool audio editing program. The GIMP The GIMP is a powerful image-editing tool. Just install it and thank me later. It rocks. Internet Explorer 5.5 Microsoft’s very popular Internet browser. You can install this latest version and run the applet examples from the book with it. Winzip 8.0 Winzip is a great tool for packaging and unpacking file archives. 479 A p p e n d i x A U s i n g t h e C D - R O M JavaProgAbsBeg-50A.qxd 2/25/03 8:58 AM Page 479 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This page intentionally left blank TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Reserved Words The following list included Java’s reserved words, also known as key- words. You cannot use them to name identifiers. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized The boolean literals true and false, and the null literal, although not technically keywords, cannot be used as identifiers either. J a v a L a n g u a g e S u m m a r y B APPENDIX JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 481 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Primitive Data Types Type Number of Bits boolean 1 byte 8 short 16 char 16 int 32 float 32 long 64 double 64 Ranges for Integral Types Type Minimum Maximum byte -2 7 2 7 – 1 short -2 15 2 15 – 1 int -2 31 2 31 – 1 long -2 63 2 63 – 1 char 02 16 – 1 Floating-Point Constants Float.NEGATIVE_INFINITY Float.POSITIVE_INFINITY Float.NaN Double.NEGATIVE_INFINITY Double.POSITIVE_INFINITY Double.NaN NaN stands for “not a number” and represents values of undefined operations such as 0.0 / 0.0. 482 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 482 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. A p p e n d i x B J a v a L a n g u a g e S u m m a r y Comments There are three types of comments used in Java (single line, multi-line, and javadoc). This is a single line comment example: // this is a single line comment This is a multi-line comment example: /* this is a multi-line comment */ This is a javadoc comment example: /** * This is a javadoc comment */ Literals Literals are used in Java to represent values that are of the primitive, String, or null types in source code. The following sections summarize their syntaxes. Any- thing within square braces ( [ and ]) is optional and the bar character (|) sepa- rates different options. For example, [+|-] means the code can include +, -, or neither (because they are within square brackets). Integer Literals Integer literals can be expressed in octal (base 8), decimal (base 10), or hexadeci- mal (base 16). Octal digits can be any digits from 0 to 7. Decimal digits can be any digits ranging from 0 to 9. Hexadecimal digits can be any digit ranging from 0 to 9 and also any letter (case-insensitive) from A to F. A=10, B=11, C=12, D=13, E=14, F=15. The syntax for an integer literal is: [+|-][0[X|x]]number[L|l] Example Description 67 int literal having value 67 +67 int literal having value 67 -67 negative int literal having value –67 012 octal int literal having value 10 483 JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 483 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. -0X27C hexadecimal int literal having value –636 1234567890L long literal having value 1,234,567,890 -0XBEL hexadecimal long literal having value -190 Floating-Point Literals A floating-point literal can be either a float or a double. The syntax for a float- ing-point number is: [+|-]number.number[[+|-]Eexponent|[+|-]eexponent][F|D|f|d] Example Description -10. double literal having value -10.0 +.01 double literal having value 0.01 1.23 double literal having value 1.23 1.23d double literal having value 1.23 1.23f float literal having value 1.23 2E4 double literal having value 20,000.0 (2x10 4 ) -133e-2F float literal having value -1.33 (-133x10 -2 ) Boolean Literals Boolean literals must be either true or false. Character Literals A character literal is a single character or escape sequence enclosed in single quotes. The data type of a character literal is always char. A Unicode character escape sequence is in the form \unnnn, where nnnn is the hexadecimal represen- tation of the character. The syntax for a character literal is (exactly one character or escape sequence must appear within the single quotes): 'character|escape_sequence' Example Description 'a' char literal a '$' char literal $ '\u003F' char literal ? '\'' char literal ' (single quote) '\"' char literal " (double quote) 484 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 484 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. A p p e n d i x B J a v a L a n g u a g e S u m m a r y '\b' char literal for backspace '\f' char literal for form-feed '\n' char literal for new line '\r' char literal for carriage return '\t' char literal for tab '\\' char literal for backslash (\) String Literals A string literal consists of a string of characters and/or escape sequences within double quotes. The data type is always String. The syntax for a String literal is: "[characters&|escape_sequences]" Example Description "" The empty string "Abc" String literal Abc "\"Java\"" String literal "Java" "C:\\My Documents\\myfile.txt" String literal C:\My Documents\myfile.txt Null Literal The null literal is null. Operators In the following table, arg refers to any variable or value. Some operators only take certain types of arguments. For example, ! only works on Boolean types. Type Syntax Description Unary +arg, -arg Sign (positive or negative) ++variable Prefix increment variable++ Postfix increment variable Prefix decrement variable Postfix decrement !arg Boolean compliment (Not) ~arg Bitwise inversion (type)arg Cast 485 JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 485 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Arithmetic arg + arg Addition arg - arg Subtraction arg * arg Multiplication arg / arg Division arg % arg Modulus Shift arg >> arg Left shift arg >> arg Right shift arg >>> arg Unsigned right shift Comparison arg < arg Less than arg > arg Greater than arg <= arg Less than or equal to arg >= arg Greater than or equal to arg == arg Equal to arg != arg Not equal to arg instanceof Instance of class Bitwise arg & arg Bitwise AND arg | arg Bitwise OR arg ^ arg Bitwise XOR Logical arg && arg Logical AND arg || arg Logical OR Ternary condition ? val_if_true : val_if_false Conditional operator Assignment Operators Assignment operators store a value into a variable. This section covers the opera- tors that included the equals sign (=), however, the increment ( ++) and decrement ( ) operators perform assignments as well. The assignment operator can be just the equals sign or the equals sign followed by an additional operator (although the combination of the two constitutes one single operator). The syntax for the assignment operator is: variable =[op] arg 486 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 486 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. A p p e n d i x B J a v a L a n g u a g e S u m m a r y If = is combined with another operator, the operation is logically equivalent to: variable = variable op arg The following are all assignment operators: = +=-=*=/=&=|=^=%=<<= >>= >>>= Loops Name Syntax for loop for([init, init, …];[condition];[update, update, …]) { body } while loop while(condition) { body } do loop do { body } while (condition); Break and Continue The break and continue statements redirect the flow of loops. break takes con- trol out of the loop and continue returns control to the top of the loop. Conditionals The conditional statements are if, switch, and also the Ternary operator described in the “Operators” section. The syntax for the if conditional is (the square brackets [] indicate that the else if and else statements are optional and are not part of the syntax): if (condition) { statements_condition_true; } [else if (other_condition) { statements_other_condition_true; }, …] [else { statements_no_condition_true; }] 487 JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 487 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... subclass TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Java Language Summary [access_modifier] [modifier, ] type variable_name [ = initial_value]; JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 490 490 Java Programming for the Absolute Beginner native Indicates the method body is defined outside of Java in a native library...JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 488 488 Java Programming for the Absolute Beginner The syntax for the switch conditional is: switch (test) { case value: [statements_test_equals_value] [break;] … [default: [statements_test_equals_no_case_value] } Try… Catch Blocks Try… catch blocks are used for exception handling Here is the syntax: try {... ImageTest2 application, source code, 334–336 platform independence, 4–5 size command-line arguments, 336 reasons for using, 7–8 import statement, 22 procedure-oriented program, 5 syntax, 15 importing classes, 22 Java run-time environment (JRE), 5 increment operators, 96–97 javac command, 21 assignment operator, 99 javadoc tags, 435–438 compound assignment, 99–100 JRE ( Java run-time environment), 5 postfix,... 46–47 cd command, 12–13 command-line input, 38–39 CD-ROM commands See also methods Java SDK, 478 cd, 12–13 programs, list of, 479 javac, 21 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Index PlayArea .java, source code listing, 409–414 JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 494 494 Index comments documentation... Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 489 489 [access_modifier] class_name([arg, …]) { [constructor_body] } Variable Declaration Appendix B implied call to the superclass’s no-arg constructor, or a call to another constructor within this class using this(): The syntax for declaring... WindowEvents, 242–245 handling exceptions See exception handling TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 497 497 InputStreamReader class, 40 HTML document, incorporating into applets, 21–22 installing Java Software Development Kit Linux installation, 10–11 Solaris installation,... inner class, 407–409 falling blocks, 404–407 inner classes, 398–401 hitDealer() method, 168 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 493 493 PlayAreaTest .java, source code listing, 414–415 PlayArea Event model, 394 source code examples, 778 Web links, 479 character... syntax for defining a class follows: [package package_name;] [[import Imported_class_or_package;] …] [access_modifier] class class_name [extends super_class_name] [implements implemented_Interface, …] { [class_definition] } Class Modifiers Modifier Description public Access modifier final Class cannot be subclassed abstract Must be subclassed (cannot be instantiated) Constructor Definition The syntax for. .. Java in a native library synchronized Indicates that, at most, only one thread can have control of this method at any given time TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 491 Index // (double slashes), 18 destroy() method, 290–292 / (division operator), 37 functions of,... dispose() method, 181, 244 overview, 241–242 division operator (/), 37 TextEvents, 260–262 do loop, 109–111 WindowEvents, 242–245 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 495 495 events, event listener interfaces, 177 example codes AdjustmentTest application, source code, . char literal for backspace 'f' char literal for form-feed ' ' char literal for new line ' ' char literal for carriage return ' ' char literal for tab '\'. 4–5 procedure-oriented program, 5 reasons for using, 7–8 syntax, 15 Java run-time environment (JRE), 5 javac command, 21 javadoc tags, 435–438 JRE ( Java run-time environment), 5 K KeyEvents. are: Sun Java This takes you directly to Sun’s Java Web site. (http:/ /java. sun.com). Sun Microsystems This takes you to Sun’s main Web site. Sun Microsystems is the innovator behind Java technology.

Ngày đăng: 03/07/2014, 05:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan