1 / 31

Spring 09- ICE0124 Programming Fundamentals I Java Programming

Spring 09- ICE0124 Programming Fundamentals I Java Programming. XuanTung Hoang tung_hx@icu.ac.kr. Lecture No. 2. Introduction to Java Programming and Applications. Compiler/Interpreter Java language Java Platforms, JRE and JDK First programs: Hello world, and Addition

karsen
Download Presentation

Spring 09- ICE0124 Programming Fundamentals I Java Programming

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. Spring 09- ICE0124 Programming Fundamentals IJava Programming XuanTung Hoang tung_hx@icu.ac.kr Lecture No. 2

  2. Introduction to Java Programming and Applications Compiler/Interpreter Java language Java Platforms, JRE and JDK First programs: Hello world, and Addition Syntax: Things to remember … … Other experiences Memory concepts Primitive Data Types Expression, Arithmetic and relation operators

  3. Interpreter and Complier • Computers need translators to translate high-level language to machine instructions • Interpreter translates programs (written in high-level languages) online: • Translate and execute at the same time • Statement by statement • Compiler translates the whole program into machine language before execution • Turn program’s text into executable file (in machine language format) • When user wants to run the program, he/she loads the executable file into computer memory to execute. • Portability issue: Different computer hardwares use different instruction sets (machine language) • We need appropriate interpreter/compiler for each type of machine

  4. History of Java language • Initially, Java language is developed for consumer-electronic devices (embedded systems) (in 1991 by Sun Microsystems) • Also, It turned out to be good for Web and Internet applications • Add dynamic contents (Java applet) • Rapid development of distributed applications • Ease of deployment • … now Java becomes more and more attractive for embedded systems (its primary purpose)

  5. Java Platforms • Sun Microsystems provides most of the utilities for users in “Java Platform” products • Text editor • Bytecode compiler • Class loader • Bytecode Verifier • JVM (interpreter) • … Plain text editor: GVIM, Crimson editor, Ultraedit, … Rich text editor: MSWord,… Java Platforms: J2SE, J2EE, J2ME

  6. Java Platforms • J2SE: Standard Edition • J2EE: Enterprise Edition • J2ME: Micro Edition • JDK v.s. JRE + bytecode compiler + Class libraries + … JDK + JVM (interpreter) + … JRE

  7. Java Development Process • 5 phases • Creating a program: use text editor to create program source • Compiling: use compiler to turn program source into bytecode (.class files) • Loading: use class loader to load .class files into memory • Bytecode verification: bytecode verifier examines the bytecode for validity and security concerns • Execution: Java Virtual Machine (is a kind of interpreter for bytecodes) executes the bytecodes • Combination of compiling and interpreting • Enhance portability of programs: • Write Once – Run Anywhere • Compact bytecode files are easy to exchange • Bytecode verifier guarantees security • Just-In-Time compiler enhances performance

  8. First Application

  9. First Application: Structure • Hello.java defines one class: class Hello • Keyword class is used for defining a class • Class name follows keyword class • Class body is surrounded by braces { … }

  10. First Application: Structure • Class body contains methods (or class functions) and variables: • In the example we have only one method (method main) and no variables • Method has return type (void) and parameters ( args ) • Parameters are given within parenthesis ( … ) • If the method have no parameters, parenthesis are still required • Modifiers public and static characterize the method • Braces surrounds method body • main method is the program entry • Every java program must have one main function

  11. First Application: Structure • Inside method body, there are statements. • There is only one statement in the example. • Each statement should be ended with a semicolon “;” • One statement may span multiple lines

  12. First Application: Comments • Multi-line comments with: /* … */ • Single-line comments with: // • Comments can be placed anywhere. • Comments are for human (compiler ignores comments) • Comments should explain the code

  13. More complex program: Addition

  14. More complex program: Addition • import statement specifies external classes that we use in our program • In the example: • Scanner is the class we use • Scanner is a class in class libraries provided by JDK

  15. More complex program: Addition Variables declaration Using Scanner object to read input Do computation & display result

  16. Read input and display output • Read input with Scanner class • Import Scanner class • import java.util.Scanner; • Create Scanner object • Scanner input = new Scanner( System.in ); • Read input from user using nextInt() method • number1 = input.nextInt(); • Read textbook, section 2.5 • Display input with print, println, printf • System.out.print(“abc”): output the string “abc” • System.out.println(“abc”): outputs the string “abc” then goes to the next line • System.out.printf(“result is %d”, sum): output a string with format specifiers • Read textbook section 2.4

  17. Things to remember … • Java is case-sensitive: class and Class are different • Skeleton of a class • Skeleton of a class function (method) • Skeleton of a simple java program • Simple Java program has one class that contains main method • Name of the source file must be identical to the class name (class identifier) • Statements end with “;” • Braces enclose blocks of code • Braces should go in pairs (Good practice: Open and close brace together) • Read input from user and output result to screen

  18. Other experiences… • Observe outputs of java compiler (javac.exe) when compiling java program (.java file) • When the compilation is successful • What files are produced by javac.exe • What is stored inside those files (files produced by javac) • When the compilation failed • Specify the problem in your source code

  19. Variable and Memory • Example of variables: • A variable is associated with a location in memory • A variable has: • Name: is used to access the memory location • Type (data type): specifies how the variable is treated by the computer • Size: The size of the memory location; is determined according to type of the variable • Value: is stored in binary format at the memory location type name

  20. Variable and Memory: Illustration • What does the following 16-bit string represent ? 00010100100000000 • If you don’t understand, please read textbook, section 2.6

  21. Primitive data types and Objects • There are many data types • We can even define our own data type • Data types are classified into primitive data types and reference types (objects) • Primitives data types: • … are most fundamental • … use small, fixed number of bytes • … are built into Java • … are used as building block for developing more complex data types (objects) • There are 8 primitive data types: • byte, short, int, long, fload, double, char, boolean • Is Int a Java primitive data type ? Floating point numbers Logical (True/false) Integral numbers characters

  22. Number data types • Number literals • Valid integers: 2007 2007L (long integer) • Valid floating point numbers: 2007, 2007.0 2007f, 2007.1f (float) 2007d, 2007.1d (double) 3.4E+3 (scientific notation) Can we use byte data type to store the number 2007 ?

  23. char data type and Character table • Size of char is 2 bytes (16 bits) • Character table is used to map 16-bit codes to letters (Unicode) • Character literal: • Valid characters: ‘a’, ‘b’, ‘A’, ‘\n’, ‘\t’ Is it a character “C” ?

  24. boolean data type • Accept only two values: true and false • They are Java keywords

  25. Expressions and Arithmetic Expressions • Expression is a combination of literals, operators and parenthesis to produce a value • Example: • a + b * c (a, b, c are variables already declared) • Expressions dealing with number variables and operators are arithmetic expressions • Arithmetic operators in Java: + , -, *, /, % • Be careful with division operator /: • Integer division is different from floating point division • 3 / 2  1 • 3.0 / 2  1.5 • How about remainder operator with floating point numbers ?

  26. Order of calculation and Precedence • Order of calculations: • Parenthesis • *, /, % • +/- • Left to right • Examples

  27. Equality and Relation operators • Equality operators : ==, != • Relational operators: <, >, <=, >= • See fig 2.14, section 2.8, textbook • Questions: • Assume that we declare integer variable a and b and assign values 3 to a, and 4 to b. • Is a < b a valid expression ? • What values are produced by a < b, a < b, a == b?

  28. Equality and relation operators • Be careful when using == with float/double variables • Does this produce true ? • 4.0/3.0 == 1.0 + 1.0/3.0 • How about this ? • 1.0/3.0 = 0.333333333333333 • Floating point arithmetic is NOT exact • Exact equality sometimes impossible to achieve  Logically we expect true but the computer report false • Avoid using equality with floating point numbers

  29. Two’s complement notation(Appendix E6, textbook) • Assume 4-bit signed integers • Arithmetic negation "-" is equivalent to "~" then + 1

  30. Floating-point number precision • Two data type (primitive) for floating point numbers: float and double • float: 32-bit (4-byte). Ex: 0.12f • double: 64-bit (8-byte). Ex: 4.15d • Floating-point numbers are treated as double by default • Example: 9.02 and 9.02d are all double numbers • Floating-point number arithmetic are approximate

  31. Assignment • Assignment operator, = • Stores the value evaluated on “the right hand side” into the variable on “the left hand side” • Example:

More Related