Welcome to Athens Learning Java Software Engineering Lab

download Welcome to Athens Learning Java Software Engineering Lab

If you can't read please download the document

  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Embed Size (px)

Transcript of Welcome to Athens Learning Java Software Engineering Lab

  • Slide 1
  • Welcome to Athens Learning Java Software Engineering Lab.
  • Slide 2
  • Step 1 () - Introduction to Java Why Java? Why Java? Creating a simple Java program Creating a simple Java program Relation between Java and C Relation between Java and C comments comments constants, pre-processor, macros, conditional compilation constants, pre-processor, macros, conditional compilation data types data types strings and objects (objects destruction) strings and objects (objects destruction) objects comparison and copy objects comparison and copy functions and parameter passing functions and parameter passing pointers -the null value pointers -the null value arrays arrays operators operators statements (if-else, for, while, switch, break, continue, finally, synchronized) statements (if-else, for, while, switch, break, continue, finally, synchronized) Exceptions and Errors Exceptions and Errors modifiers (final, native, synchronized, transient, volatile) modifiers (final, native, synchronized, transient, volatile)
  • Slide 3
  • Why Java completely object oriented completely object oriented creates code for the Java Virtual Machine (JVM) and consequently the Java programs can run in any platform creates code for the Java Virtual Machine (JVM) and consequently the Java programs can run in any platform has many similarities with C and C++ has many similarities with C and C++ dynamic and distributed dynamic and distributed a Java class can be loaded and used dynamically in run time a Java class can be loaded and used dynamically in run time Java code, spread in Internet, can be used dynamically (Java Applets) Java code, spread in Internet, can be used dynamically (Java Applets) simple and easy to use simple and easy to use safe, reliable and secure safe, reliable and secure has no pointers (known problems and bugs of C/C++ programs do not exist) has no pointers (known problems and bugs of C/C++ programs do not exist) automatic mechanism of memory management - garbage collection automatic mechanism of memory management - garbage collection very powerful mechanism of exceptions very powerful mechanism of exceptions provides very strict safety rules on the network level provides very strict safety rules on the network level multithreaded - simplifies the procedure of threads creation and concurrency multithreaded - simplifies the procedure of threads creation and concurrency
  • Slide 4
  • Creating a simple program in Java package simple; public class HelloWorld{ public void printMessage(){ System.out.println(Hello World); } public static void main(String[] args){ HelloWorld hello = new HelloWorld(); hello.printMessage(); } Step 1: Select a directory in which you want to write your java programs and add it to the CLASSPATH (ex. setenv CLASSPATH $CLASSPATH:$HOME/java) Step 2: Make a subdirectory simple and write your program named HelloWorld.java Step 3: Compile your program: javac HelloWorld.java Step 4: Run the program from any directory: java simple.HelloWorld
  • Slide 5
  • How Java works
  • Slide 6
  • Comments - Constants - some differences with C /* C - like comments- not nested */ /* C - like comments- not nested */ // C++ - like comments // C++ - like comments /** Special comments for documentation production by means of the javadoc tool - in html format - not nested*/ /** Special comments for documentation production by means of the javadoc tool - in html format - not nested*/ constants are defined as static final (ex. public static final double PI = 3.14159;) constants are defined as static final (ex. public static final double PI = 3.14159;) macros do not exist - a good Java compiler should automatically be able to inline short methods where appropriate macros do not exist - a good Java compiler should automatically be able to inline short methods where appropriate When the compiler understands that parts of code are never executed, it does not compile them. Conditional compilation is achieved by using the condition if (if(DEBUG){} where DEBUG is a boolean constant) When the compiler understands that parts of code are never executed, it does not compile them. Conditional compilation is achieved by using the condition if (if(DEBUG){} where DEBUG is a boolean constant) There is not any #include instruction. Java does not make any discrimination between declaration and definition of variables and methods (header files do not exist). There is not any #include instruction. Java does not make any discrimination between declaration and definition of variables and methods (header files do not exist). Java provides a new declaration: import classes; Java provides a new declaration: import classes; import java.util.Vector; import java.util.Vector; import java.io.*; import java.io.*;
  • Slide 7
  • Primitive data types Conversion between boolean and integer values is not allowed There isnt any keyword unsigned or declarations like long int, short int e.t.c. A long constant can be distinguished by appending the characters L or l. Similarly, for float and double constants we use the characters f or F and d or D. Floating point arithmetic never causes exceptions, even in case of division by zero. On the contrary, integer division by 0 (or modulo 0) causes an ArithmeticException
  • Slide 8
  • Strings and objects Strings are not primitive data types but instances of the class String Strings are not primitive data types but instances of the class String String constants are displayed as in C: This is a string constant - When they are detected from the compiler, they are transformed to String objects. String constants are displayed as in C: This is a string constant - When they are detected from the compiler, they are transformed to String objects. Creating objects Creating objects java.awt.Button b = new java.awt.Button(); java.awt.Button b = new java.awt.Button(); String s = This is an example; (identical to: String s = new String(This is an example);) String s = This is an example; (identical to: String s = new String(This is an example);) Accessing objects Accessing objects String s; s = My name is Fanis; if (s.substring(11).equals(Fanis)) System.out,println(His name is Fanis); if(String.valueOf(i + 5).equals(1000)) System.out.println(OK); String s; s = My name is Fanis; if (s.substring(11).equals(Fanis)) System.out,println(His name is Fanis); if(String.valueOf(i + 5).equals(1000)) System.out.println(OK); Objects destruction Objects destruction We don t care about freeing memory or destroying objects We don t care about freeing memory or destroying objects When its detected that an object is not used anymore, it is destroyed (garbage collection) When its detected that an object is not used anymore, it is destroyed (garbage collection)
  • Slide 9
  • Comparing and copying Objects Objects are accessed by reference Objects are accessed by reference TextField ok = new TextField(Everything is OK); //A new object is created TextField no = new TextField(Nothing is OK); //A new object is created no = ok; // The variables no, ok refer to the same object // The object to which no refered is not used anymore // and it will be freed by the garbage collector no.setText(I dont know what really happens); System.out.println(ok.getText()); // Prints: I dont know what really happens TextField ok = new TextField(Everything is OK); //A new object is created TextField no = new TextField(Nothing is OK); //A new object is created no = ok; // The variables no, ok refer to the same object // The object to which no refered is not used anymore // and it will be freed by the garbage collector no.setText(I dont know what really happens); System.out.println(ok.getText()); // Prints: I dont know what really happens String s1 = Hello; String s2 = Hello; if(s1==s2) System.out.println(They are the same object); //Not printed if(s1.equals(s2) System.out.println(They have the same values); // It is printed String s1 = Hello; String s2 = Hello; if(s1==s2) System.out.println(They are the same object); //Not printed if(s1.equals(s2) System.out.println(They have the same values); // It is printed The classes that implement the interface Cloneable can be copied by using the method clone(): The classes that implement the interface Cloneable can be copied by using the method clone(): Vector c, b = new Vector(); c = b.clone(); // variable c refers to a clone of b (exactly similar but // a different object) Vector c, b = new Vector(); c = b.clone(); // variable c refers to a clone of b (exactly similar but // a different object)
  • Slide 10
  • Functions, parameter passing and pointers Methods can return values of the primitive data types, arrays or objects. When they dont return anything, the keyword void must be used. Methods can return values of the primitive data types, arrays or objects. When they dont return anything, the keyword void must be used. The primitive data types always pass as parameters in methods by value, in contrast to arrays and objects. The primitive data types always pass as parameters in methods by value, in contrast to arrays and objects. For every primitive data type there is a corresponding class. For every primitive data type there is a corresponding class. Integer -> int, Float -> float, Char -> char e.t.c. Integer -> int, Float -> float, Char -> char e.t.c. These classes can be used in case we want to pass integer e.t.c values by reference in a method. These classes can be used in case we want to pass integer e.t.c values by reference in a method. Since objects access is made by reference, the existence of pointers is useless. THERE ARE NOT POINTERS IN JAVA Since objects access is made by reference, the existence of pointers is useless. THE