1 / 267

Java

Java. Dr. Yong Uk Song Dept. of MIS Yonsei University Wonju Campus. Session 1. Introduction. Programming Languages (1). First Generation Machine Languages (e.g.) 01101011 Second Generation Assembly Languages (e.g.) ADD X Assembler Third Generation High Level Languages

zea
Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Dr. Yong Uk Song Dept. of MIS Yonsei University Wonju Campus

  2. Session 1.Introduction

  3. Programming Languages (1) • First Generation • Machine Languages (e.g.) 01101011 • Second Generation • Assembly Languages (e.g.) ADD X • Assembler • Third Generation • High Level Languages (e.g.) a = a + x • Compiler, Interpreter

  4. Programming Languages (2) • Fourth Generation • Non-Procedural Languages (cf.)Procedural Languages • Do What? (cf.)Do How? (e.g.) SELECT id FROM customer WHERE id > 50 • Fifth Generation • Artificial Intelligence • Voice Recognition + Natural Language Processing + …

  5. Programming Languages (3) • Categories of Third Generation Languages • Structured Programming Language • Object-oriented Programming Language

  6. Structured Programming Languages • Objectives • Debugging • Maintenance • Tools • Readability • Methodology • Divide and Conquer : Modulization • Control : Sequence, Division, Repetition

  7. Object-oriented Programming Languages • Objectives • Productivity of SW • Tools • Extensibility, Reusability • Methodology • Data-driven approach : Classes and Instances • Encapsulation(Data abstraction), Inheritance, Polymorphism • The importance of control (sequence, division, repetition) in a module is still stressed.

  8. Data Abstraction • Separating of the representation of a data object from the specification that are essential for its correct use. • Classes • Data hiding • Member functions • Initialization • Operator overloading

  9. Inheritance • Inheritance allows you to reuse data and functions in the classes you have created by passing all or parts of them down to children classes (i.e. subclasses, derived classes). • Type hierarchy • Single inheritance vs. Multiple inheritance

  10. Polymorphism • With polymorphism, you can send a message to an object without worrying about how the system will implement the action. • Operator overloading (C++) • Function overloading • Function overriding • Virtual functions (default in Java)

  11. Learning Java Is . . . • Learning programming structures • Learning classes • Learning initialization • Constructors • Learning derived classes • Learning packages, interfaces, exception handling, stream, networking, etc.

  12. History of Java • Green project (Sun Microsystems, 1991) • A compact programming language for electronic devices such as interactiveTV, smart refrigerator, … • The target OS was called Star7 • Oak (James Gosling) • Web Browser • WebRunner (HotJava) (Patrick Naughton & Jonathan Payne) (1994) • JavaOne Conference (1996.5) • JavaSoft (Marc Andreesen(Netscape)) • Java 1.0.2 • Java 1.1.5 • Java 2 (Java 1.2)

  13. Platform Independence • Byte code • Virtual Machine • = Java Interpreter / Java Runtime

  14. What is programming? • The purpose of programming is to make a SW which will be executed later by another people to do a predefined job. • The SWs are in fact executable files. • File name : *.exe • E.g. MS-Office, Notepad, MS-Windows XP, … • The final goal of programming is to make an executable file. • Steps to make an executable file • Coding  Source code • Sometimes we call coding as programming in a narrow sense. • Compiling  Executable file • Compiling is included as an additional step of programming in a wider sense.

  15. Programming in Java • Source Codes • xxx.java • Executable Files • xxx.class • Execution • java xxx

  16. Where are the Tools? • http://java.sun.com/ • Java SE JDK • The core SW to develop and execute a Java program. • NetBeans • IDE (Integrated development environment) • To support every steps while programming in Java; coding(editing), compiling, execution, debugging.

  17. Using NetBeans IDE • Build a New Project • Edit the Source Code • Compile • Execute (Run) • Debug

  18. Hello.java class Hello { public static void main(String[ ] args) { System.out.println("Hello, world"); } }

  19. Exercise • Build a NetBeans project called Hello. • Make a program which prints out "Hello".

  20. Session 2.Variables and Expressions

  21. Comments (1) • /* ... */ • C-language style comments • Comments for multiple lines (e.g.) /* This is a comment line 1. This is a comment line 2. This is a comment line 3. … */ • // ... • C++-language style comments • Comments for a single line (until end of line) (e.g.) // This is a comment.

  22. Comments (2) • /** ... */ • Comments for javadoc • Javadoc is a documentation generator from Sun Microsystems for generating API documentation in HTML format from Java source code. (e.g.) /** * Validates a chess move. Use {@link #doMove(int, int, int, int)} to move a piece. * * @param theFromFile file from which a piece is being moved * @param theFromRank rank from which a piece is being moved * @param theToFile file to which a piece is being moved * @param theToRank rank to which a piece is being moved * @return true if the chess move is valid, otherwise false */ boolean isValidMove(int theFromFile, int theFromRank, int theToFile, int theToRank) { ... }

  23. Exercise • Build a NetBeans project called Hello. • Make a programs which prints out "Hello". • Add a comment as following: This is a Java Hello World program. This is my first Java program.

  24. Variables and Literals • Variables • Of which value may be changed • To store some value (e.g.) a = 3; • Declaration is required (e.g.) int a; • Literals • Of which value is NOT changed • Used as they are (e.g.) 3, 1.2, …

  25. Variable Declarations • Syntax • type variable1, variable2, … • Types • 8 Primitive types • byte, short, int, long, float, double, char, boolean • Arrays • [ ] • Class types • Built-in Classes • Object, Class, String, Byte, Short, Int, Long, Float, Double, Char, Boolean, … • User-defined Classes

  26. Naming of Variables, Classes, … • Alphanumeric • A ~ Z, a ~ z, 0 ~ 9, _ • Under bar(_) is regarded as an alphabet. • Case sensitive • Upper and lower capital characters are regarded as different characters. • e.g.) Int / int • Can not use keywords as names • e.g.) boolean, break, byte, case, catch, char, class, const, do, double, else, extends, final, float, for, goto, if, int, long, new, package, private, protected, public, return, short, static, super, switch, this, throw, throws, try, void, while, … • e.g.) false, null, true • The names of public classes should be same as their file names.

  27. Correct int i; int i, aInteger, _i; char c; char c, aChar, _2; double d; double d, aDbl, f100_; Incorrect int 2a; int i; aInteger; _I; char 2_3; char c; aChar; _2; double a&b; double a Dbl, 100f_; Example of Variable Declarations

  28. Exercise • Build a NetBeans project called Test. • Declare an integer variable "i" and a double variable "d" in the "main" method.

  29. int 10 012 0xA, 0xa, 0XA, 0Xa long 10L, 10l 012L, 012l 0xAL, 0xAl, 0xaL, 0xal float 2.25F 2.25f double 2.25 19E-95, 19e-95 boolean true, false char 'A', '\n', '\r' Escape Sequence '\n', '\t', … '\'', '\\' '\ooo', '\uhhhh' (e.g. '\101', '\uC790') '\A', '\"', … String Constants "Hello, world\n" "-- \n -- ' -- \" -- \\ -- A --" "-- \033 -- \uC790 --" Please, note: 'A' "A" Literals

  30. Expressions • Expressions • 3 * 4 + 2 • a * b + 2 • Operators • *, +, … • Operands • Literals, variables, function calls, another expressions • 3, 4, 2, a, b, Math.sin(3.14), (3 * a)

  31. Forms of Expressions • Infix form • 3 + 4 • Prefix form • (+ 3 4) • Lisp language • Postfix form • 3 4 + • Java and many other third generation languages use infix form for their expressions.

  32. Categories of Operators (by number of operators) • Unary operators • +a, -a, ++a, a++, --a, a--, … • Binary operators • a + b, a >= b, a && b, a = 3, … • Ternary operators • a ? 0 : 1

  33. Another Categories of Operators • Arithmetic Operators • +, -, *, /, % • Relational Operators • >, >=, <, <=, ==, != • Logical Operators • &&, ||, ! • Incremental/Decremental Operators • ++, -- • Assignment Operators • =, +=, -=, *=, /=, %= • Conditional Operators • ? :

  34. Arithmetic Operators • +, -, *, /, %(modulus) • Be careful about the types of operands (e.g.) a + 3 d / 4 a * 4 + b a * b - c 5 % 2 → 1 9 / 2 → 4 (It is NOT 4.5 !)

  35. Exercise • Build a NetBeans project called Test. • Calculate and print out the result of "3 + 4.5".

  36. Relational Operators • Return values • False : false (boolean) • True : true (boolean) (e.g.) a > 3 a == b a != 3

  37. Exercise • Build a NetBeans project called Test. • Calculate and print out the result of "3 >= 4.5".

  38. Logical Operators • Return values • False : false (boolean) • True : true (boolean) (e.g.) (a > 3) && (a <= 7) (a > b) || (c == 1) !(a > b) ↔ a <= b

  39. Exercise • Build a NetBeans project called Test. • Calculate and print out the result of "3 >= 4.5 and 2 < 5".

  40. Incremental/Decremental Operators • Return values • The value after increment/decrement when it appears in the front of the variable • The value before increment/decrement when it appears in the rear of the variable • Side effect • Increment/decrement of the value of the variable • Hence, its operand must be a variable. • Note: unary operators (e.g.) n++ n-- ++n --n ++n + 1 --n + 1 n++ + 1 n-- + 1

  41. Return Values and Side-effects • Return values (e.g.) 3 + 4 → Value 7 returns as a result. (e.g.) a = 4 → Value 4 returns as a result. • Side-effects (e.g.) a = 4 → Variable"a"stores value 4.

  42. Exercise • Build a NetBeans project called Test. • Declare an integer variable "i". • Set the value of the variable "i" as 3. • Calculate and print out the result of "i++" and "++i" sequentially.

  43. Assignment Operators • Return values • The value stored to the left-hand-side (LHS) operand (variable) • Side effect • Change in the value of the variable • Hence, its LHS operand must be a variable. • Note: binary operator (e.g.) a = 3; a = b = 4; a += 3;

  44. Exercise • Build a NetBeans project called Test. • Declare two integer variables "i" and "j". • Calculate and print out the result of "i = j = 3". • Print out the value of "i". • Print out the value of "j".

  45. Conditional Operators • Return values • The return value of the second operand if the return value of the first operand is true • The return value of the third operand if the return value of the first operand is false • Remark • The types of the second and third operands should be same. • If they are different, an implicit type conversion rule applies. (e.g.) (a < 3) ? 4 : 5 (a == 3) ? i : (j + 1)

  46. Exercise • Build a NetBeans project called Test. • Declare three integer variables "i", "j" and "k". • Calculate and print out the result of "i = j = k = 3". • Calculate and print out the result of "(i <= j) ? ++j : k++". • Print out the value of "i". • Print out the value of "j". • Print out the value of "k".

  47. Implicit Type Conversions • Implicit rules • In case of binary or ternary operators, if the types of their (last) two operands are different, the type of one (more specific) operand is automatically converted to that of the other (more general) one. • Generality • byte  short  int  long  float  double

  48. Exercise • Build a NetBeans project called Test. • Print out the result of "(2 < 3) ? 3 : 4.0".

  49. Explicit Type Conversions • Type casting operator (unary operator) • Format (type)expression • Usage • To enhance readability • To remove warnings by compilers (e.g.) (int)a, (int)(9 / 2), i = (int)(9.0 / 2.0)

  50. Exercise • Build a NetBeans project called Test. • Declare an integer variable "i". • Declare two double variables "d" and "e". • Set d as 9.0 and e as 2.0. • Calculate and print out the result of "i = d / e". • If you have an error or a warning after compiling, modify the expression to "i = (int)(d / e)".

More Related