Course Java Notes

download Course Java Notes

of 71

  • date post

    11-Mar-2015
  • Category

    Documents

  • view

    379
  • download

    3

Embed Size (px)

Transcript of Course Java Notes

Java2004 , markos@debian.org : GNU FDL

1. Java Java (object oriented) . 1991 Sun Microsystems . C++ - . Java

Java Java ( ) . Java Virtual Machine (JVM) . (.. C, C++, PASCAL) . , . , Windows Macintosh. , (source code) . , Java , bytecode. , . Java Virtual Machine (JVM) Java bytecode. JVM bytecode (java bytecode to native code translation, ). Java, Java (.. C++). , Just In Time (JIT) compilers scheduling JVM, Java ( Java 90% C++), .

Java

1

C++ Java. (source code) (source code)

C++ compiler

Java compiler

(Native code)

Java Bytecode

Java Virtual Machine (JVM)

Java C++ . : Java Java Bytecode Portable ( JVM) C++ Native code

(CPU)

C++

Java Web, Applets Web (Procedural Structured Programming)

C, PASCAL, FORTRAN . (code-centric ). , . (Object-Oriented Programming)

(C++, Eiffel, Smalltalk Java) . (data-centric) . Java 2

. . . . , , , . . . /, . , . , . , ( ). . , ( .. , , ). . , :

Encapsulation () .

Polymorphism () , .

Inheritance () . , .

Java

3

2. Java , Java . , , .

. . -' - . :type var;

type var . Java ( ).

, . , , loop for/while/do if/else/switch. :type var1; if () { // var1 (). // var2 (). type var2; // var2 () } // var1 () // var2 ()

Java C C++ ( Java C++). , C++ Java . Java pointers.

Java

4

Java: byte short int long float double bool char String

( bytes) 1 2 4 8 4 8 1 2

-128 127 -32768 32767 -2147483648 -2147483647 -9223372036854775808 -9223372036854775807 1.4 * 10-45 3.4 * 1038 4.9 * 10-324 1.8 * 10308 true / false -

byte, short, int, long , float, double (. ). bool true false. char C/C++ 2 bytes Unicode (UTF-16 ). :int an_integer; an_integer = 10; long a_long = an_integer *1000; double verysmallnumber = 0.000000000003; bool am_i_hungry = false; char alpha = 'a'; String text = this is a text;

(constants)

, . ( ) final. ,final double pi = 3.1415;

(operators)

. . 4 Java ( , ).

Java

5

+ * / % ++ --

, ( , ). :int x = 10; x++; // x 11 x--; // 10

. == != > < >= = 10.0;) { // y // y. // for loop y 10.0 System.out.println(y); y = Math.sqrt(y); } for (;;) { // infinite loop wait_for_signal(); }

while

while for , : while () { ; } :bool exit_from_loop = false; while (exit_from_loop = false) { // read_bytes(file1); // write_bytes(file2);

Java

11

if (file_empty(file1) == true) exit_from_loop = true; }

do/while

do/while while, , loop .do { ; } while ();

:int x = 10; do { System.out.println(x); x++; } while (x 100) break; System.out.println(i); } {

:1 4 7

break loops, for, while, do/while. break continue, iteration loop. iteration. :for (int i = 1; i < 20; i += 3) { // () if ( i % 2 = 0)

Java

12

continue; System.out.println(i); }

:1 7 13 19

Java

13

4.ClassesClass: Car

Instance of Car Object: Audi TT

Instance of Car Object: Jaguar XK8

Instance of Car Object: Ferrari Modena

Instance of Car Object: Bertona Bella

Java . (instance) . , . , , , , , (, , , ). ( / ). , . Java ( C++ Java) class. , Car:class Car { (/) (/) }

member variables

. , : , , . , , .

Java

14

Car, :class Car { // float steering_angle; // (0 = , 100 = !) float gas_pedal; // (0 = , 100 = !) float break_pedal; // (0 = , // 100 = !) float clutch; // ( : 0, 1,2,3,4,5, // 0 = , -1 = ) int gear; // , // float acceleration, speed, rpm; }

, . , . , , , float. , , Car. . ' interface . interface (member methods).member methods

(methods) , () . , . , , , , .

Java

15

Car :// , // . void turn_wheel(float relative_angle); // void press_gas_pedal(float amount); // void press_break_pedal(float amount); // void press_clutch_pedal(float amount); // . true // false (.. 5 ). bool change_gear(int new_gear); // , // float get_acceleration(); float get_speed(); float get_rpm();

, (float) get_*(), bool change_gear(). void . , ' .

, . (implementation). , turn_wheel() :void turn_wheel(float relative_angle) { steering_angle += relative_angle; if (steering_angle = 720.0) steering_angle = 720.0; }

, 2 . , (.. ), . Java 16

new

, - (instances) . , new Java. new , , (reference) . ( ). new :Car acar = new Car(); Car anothercar = new Car();

acar anothercar new. . :Car acar; acar = new Car();

( ) , :// acar acar.steering_angle // anothercar anothercar.steering_angle // acar 13.4 . acar.turn(13.4); // acar float speed = acar.get_speed(); // anothercar 32 anothercar.turn(-32.0); // anthercar bool result = anothercar.ghange_gear(-1);

( ). Java . ','.Constructors

new, Java, , , (constructor). . , Java 17

, SQL, (sockets) server . ( , void). , Car, :Car() { steering_wheel = 0.0; gas_pedal = 0.0; break_pedal = 0.0; float clutch = 0.0; int gear = 0; acceleration = 0.0; speed = 0.0; rpm = 0.0; }

, (initialization) . , . , (engine_cc: , engine_hp: ) , :Car(int cc, int hp) { engine_cc = cc; engine_hp = hp; // }

. constructor overloading Java, method overloading ( ). Destructors? Garbage collection Java

C++, (destructors). C++, , ( delete) ( , , threads, , ). Java , ! Java (garbage collection) Java 18

, . , (.. C++). this

, , . , . this ( ). this, turn_wheel() :void turn_wheel(float relative_angle) { this.steering_angle += relative_angle; if (this.steering_angle = 720.0) this.steering_angle = 720.0; }

this , .Method Overloading

, . / . , turn_wheel() int float. C /, :void turn_wheel_int(int relative_angle) { this.steering_angle += (float) relative_angle; if (this.steering_angle = 720.0) this.steering_angle = 720.0; } void turn_wheel_float(float relative_angle) { steering_angle += relative_angle; if (steering_angle = 720.0)

Java

19

steering_angle = 720.0; }

, . turn_wheel_int() :void turn_wheel_int(int relative_angle) { turn_wheel_float((float) relative_angle); }

, , debugging, . ObjectOriented Java C++ Method Overloading. method overloading, . , :void turn_wheel(float relative_angle) { steering_angle += relative_angle; if (steering_angle = 720.0) steering_angle = 720.0; } void turn_wheel(int relative_angle) { turn_wheel((float) relative_angle); }

turn_wheel() . ' ' , ( void ) .

Java

20

5.

Java , . C/C++, Java . , .. ( Java) , ( C/C++). . C/C++. :type table[];

, new:table = new type[size];

:type table[] = new type[size];

type (bool, byte, short, int, long, float, double, char, String) ( Java ). size table. C/C++, table[i], i . C/C++ Java (0) size-1. A 10 A[0] A[9]. :int data[] = new int[10]; int i; System.out.println(Size of array data: + data.length); for (i = 0; i < data.length; i++) { data[i] = i*i; System