Java programming-sun Xin Java no difficulty tutorial lesson1 Java Technology and Application
1. Java language features
Simple, object-oriented, robust, secure, interpreted, platform-independent, multi-thread, and dynamic language.
2. Principles of cross-platform Java
Shows the principles:
Figure 1:
Figure 2:
3. Familiar with Java environment
(1) Path Environment Variable
After cmd.exe is started, type set path to view the path, as shown below:
C: \ Users \ juber> set path
Path = c: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; c: \ windows \ system32
\ Windowspowershell \ V1.0 \; C: \ Program Files \ common files \ thunder Network \ Kankan \ Co
DECS; C: \ Program Files \ common files \ compuware; C: \ Program Files \ Java \ jdk1.7.0 _ 03 \ B
In
Pathext =. com;. EXE;. BAT;. CMD;. vbs;. VBE;. js;. JSE;. WSF;. wsh;. MSC
Shows how to configure environment variables:
(2) jar command
A. Package files
F: \ Java \ javalesson \ lesson1> jar CVF lesson1.jar hello. class welcome. Class
//************************************** ********
B. Package the entire directory
F: \ Java \ javalesson> jar CVF lesson1.jar lesson1/
Added list
Adding: lesson1/(input = 0) (output = 0) (stored 0%)
Adding: lesson1/Hello. Class (input = 416) (output = 286) (compressed by 31%)
Adding: lesson1/Hello. Java (input = 169) (output = 127) (compressed by 24%)
...
(3) The public class name and file name are the same
4. Eight Data Types of Java
Byte B; // one byte signed integer
Short s; // two-byte signed integer
Int I; // 4-byte signed integer
Long L; // 8-byte signed integer
Char C; // 2-byte unsigned integer 65535 characters in Unicode encoding
Float F; // 4-byte floating point number
Double D; // 8-byte floating point number
Boolean bool = true; // two values: false and true
5. Familiar with Java data types and syntaxes
//************************************** ********
// The test code is as follows:
Public class Hello {public static void main (string [] ARGs) {// system. Out. println ("Hello world! ");/* Byte B; // one byte signed integer B = 3; B = (byte) (B * 3 ); // OK // B = B * 3; // error int to byte possible lose of data system. out. println (B); * // * short S; // two-byte signed integer int I; // four-byte signed integer long l; // 8-byte signed integer char C; // 2-byte unsigned integer 65535 types of characters adopt unicode encoding * // * float F; // 4-byte floating point number f = 1.3f; // OK // F = 1.3; // error possible lose of data system. out. println (f); * // * Double D; // 8-byte floating point Boolean bool = true; // OK // Boolean bool = 1; // error cannot be converted to an integer. system. out. println (bool); * // error if (1) {}// OK if (true) {} * // array definition // int num [3]; // space cannot be allocated during error definition // int num1 []; // num1 = new int [3]; // It will be allocated after OK. // It will be allocated at definition. // int [] num2 = new int [] {, 3 }; // OK // int [] num2 = new int [3] {1, 2, 3 }; // error with size error // simple initialization form int [] num3 = new int [3]; num3 [0] = 1; num3 [1] = 2; num3 [2] = 3; * // two-dimensional array int [] [] num4; num4 = new int [3] [4]; num4 [0] [1] = 2; num4 [0] [2] = 3; * // irregular array int [] [] num5; num5 = new int [3] []; num5 [0] = new int [5]; num5 [1] = new int [3]; num5 [2] = new int [2]; num5 [0] [4] = 45; num5 [1] [2] = 34; num5 [2] [1] = 21; system. out. println (num5 [0] [4]); system. out. println (num5 [1] [2]); system. out. println (num5 [2] [1]); // system. out. println (num5 [2] [2]); // error array out of bounds * // two-dimensional array initialization // int [] [] num6 = new int [] [] {1, 2, 3, 4, 5, 6 }; // error // int [] [] num6 = new int [] [] {1, 2, 3}, {4, 5, 6 }}; // OK // int [] [] num6 = new int [2] [3] {1, 2}, {4, 5, 6 }}; // error // int [] [] num6 = {1, 2, 3}, {4, 5, 6 }}; // OK // * // initialize int [] [] num7 = {1, 2, 3}, {4, 5}, {6}; system. out. println (num7 [0] [2]); system. out. println (num7 [1] [1]); system. out. println (num7 [2] [0]); // system. out. println (num7 [2] [2]); // error array out of bounds * // ++ the operation depends on the specific language // ++ Test 1 int I = 3; // system. out. println (I ++); // result 3 // system. out. println (++ I); // Result 4 int COUNT = (I ++) + (I ++); system. out. println (I); // result 6 system. out. println (count); // result 12 // ++ Test 2 Int J = 3; int count2 = (++ J) + (++ J) + (++ J); system. out. println (j); // result 6 system. out. println (count2); // result 15 */}}
// The error types and results are as follows:
//************************************** ********
(1) The main function is missing.
//************************************** ********
Error: The main method cannot be found in the Hello class. define the main method as follows:
Public static void main (string [] ARGs)
(2) set the wrong class path
//************************************** ********
F: \ Java \ javalesson \ lesson1> set classpath = E :\
F: \ Java \ javalesson \ lesson1> JAVA hello
Error: cannot find or cannot load main class hello
(3) Type Mismatch
//************************************** ********
Hello. Java: 8: Error: possible loss of Precision
B = B * 3;
^
Required: byte
Locate: int
1 error
(4) mismatch of Precision
//************************************** ********
Hello. Java: 19: Error: possible loss of Precision
F = 1.3;
^
Required: Float
Locate: double
1 error
(5) incompatible values of the Boolean Type
//************************************** ********
Hello. Java: 24: Error: incompatible type
Boolean bool = 1;
^
Required: Boolean
Locate: int
1 error
(6) type incompatibility in Boolean expressions
//************************************** ********
If (1)
{
}
Hello. Java: 28: Error: incompatible type
If (1)
^
Required: Boolean
Locate: int
1 error
(7) array definition Error 1 int num [3]; // The definition form cannot be similar to the C Language
//************************************** ********
Hello. Java: 35: Error: ']'
Int num [3]; // space cannot be allocated during definition
^
Hello. Java: 35: Error: The expression is invalid.
Int num [3]; // space cannot be allocated during definition
^
2 errors
(8) array definition error. 2. You do not need to specify the size when adding the definition list.
Hello. Java: 39: Error ';'
Int num2 [] = new int [3] {1, 2, 3}; // An error occurred while adding the size.
^
1 error
(9) array definition error 3 out-of-bounds Error
//************************************** ********
45
34
21
Exception in thread "Main" Java. Lang. arrayindexoutofboundsexception: 2
At hello. Main (hello. Java: 71)
(10) array definition error 4 array element initialization error
//************************************** ********
Hello. Java: 79: Error: incompatible type
Int [] [] num6 = new int [] [] {1, 2, 3, 4, 5, 6}; // Error
^
Required: int []
Locate: int
Hello. Java: 79: Error: incompatible type
Int [] [] num6 = new int [] [] {1, 2, 3, 4, 5, 6}; // Error
^
...
(11) array definition error 5 array element initialization error the array size cannot be specified
//************************************** ********
Hello. Java: 81: Error: require ';'
Int [] [] num6 = new int [2] [3] {1, 2, 3}, {4, 5, 6}; // Error
^
Hello. Java: 81: Error: Not a statement
Int [] [] num6 = new int [2] [3] {1, 2, 3}, {4, 5, 6}; // Error
...