Java free-book

Click here to load reader

  • date post

    24-Jun-2015
  • Category

    Software

  • view

    346
  • download

    1

Embed Size (px)

description

Java Programming

Transcript of Java free-book

  • 1. JJaavvaa2004 , markos@debian.org : GNU FDL1. 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 tonative code translation, ). Java, Java (.. C++). , Just In Time (JIT) compilers scheduling JVM, Java ( Java 90% C++), .eBooks4Greeks.gr Java 1

2. C++ Java. (source code)C++ compiler (Native code) (source code)Java compilerJava BytecodeJava VirtualMachine (JVM) C++(CPU) Java C++ . :Java C++ Java Bytecode Native codePortable ( JVM) Java Web, Applets Web (Procedural StructuredProgramming) C, PASCAL, FORTRAN . (code-centric ). , . (Object-Oriented Programming) (C++, Eiffel, Smalltalk Java) . (data-centric) . Java 2 3. . . . . , , , . . . /, . , . , . , ( ). . , ( .. , , ). . , : Encapsulation () . Polymorphism () , . Inheritance () . , . Java 3 4. 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 5. Java: ( bytes) byte 1 -128 127short 2 -32768 32767int 4 -2147483648 -2147483647long 8 -9223372036854775808 -9223372036854775807float 4 1.4 * 10-45 3.4 * 1038double 8 4.9 * 10-324 1.8 * 10308bool 1 true / falsechar 2 -String - 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 6. + - * / % ++ -- , ( , ). :int x = 10;x++; // x 11x--; // 10 . == != > < >= = 10.0;) {// y // y.// for loop y 10.0System.out.println(y);y = Math.sqrt(y);}for (;;) { // infinite loopwait_for_signal();} while while for , :while () {;}:bool exit_from_loop = false;while (exit_from_loop = false) {// read_bytes(file1);// write_bytes(file2); Java 11 12. 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);} :147 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 13. continue;System.out.println(i);} :171319 Java 13 14. 4.ClassesClass: CarInstance of CarObject:Ferrari ModenaInstance of CarObject:Bertona BellaInstance of CarObject:Jaguar XK8Instance of CarObject:Audi TT Java . (instance) . , . , , , , , (, , , ). ( / ). , . Java ( C++ Java) class. , Car:class Car{(/)(/)}member variables . , : , , . , , . Java 14 15. 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 16. 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 17. new , - (instances) . , new Java. new , , (reference) . ( ). new :Car acar = new Car();Car anothercar = new Car(); acar anothercar new. . :Car acar;acar = new Car(); ( ) , :// acaracar.steering_angle// anothercaranothercar.steering_angle// acar 13.4 .acar.turn(13.4);// acarfloat speed = acar.get_speed();// anothercar 32 anothercar.turn(-32.0);// anthercar bool result = anothercar.ghange_gear(-1); ( ). Java . ','.Constructors new, Java, , , (constructor). . , Java 17 18. , 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 19. , . , (.. 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 20. steering_angle = 720.0;}, . turn_wheel_int() :void turn_wheel_int(int relative_angle){turn_wheel_float((float) relative_angle);} , , debugging, . Object-Oriented Java C++ MethodOverloading. 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 21. 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.out.println(data[ + i + ] = + data[i]);} :Size of array data: 10data[0] = 0data[1] = 1data[2] = 4data[3] = 9data[4] = 16data[5] = 25 Java 21 22. data[6] = 36data[7] = 49data[8] = 64data[9] = 81 length . , , :int dataset[] = { 22, 3, 54, 43, 199, 20, 20, 67, 7, 80 }; new dataset. . Java, . , :int twodim[][] = new int[4][4];int i, j, counter = 1;for (i = 0; i < twodim.length; i++) {for (j = 0; j < twodim[i].length; j++) {twodim[i][j] = counter;counter++;System.out.print(twodim[i][j] + );}System.out.println();} :1 2 3 45 6 7 89 10 11 1213 14 15 16 , :1 2 3 45 6 7 89 10 11 1213 14 15 16twodim[1][2]twodim[3][1] Java 22 23. Strings strings System.out.println(). strings , Java strings , String. println(), String, , new.String str = new String(Hello);System.out.println(str); String :String str2 = Another string;System.out.println(str2); String , String. : boolean equals(String str) true String .int length() ( ) String.char charAt(int index) index String.i