slide1.1_2009 - Module 1.1 Module Introduction to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 1.1 Module Introduction to Computers, Programs, and Java Programs, EE3206/EE5805 Java Programming & Applications EE3206/EE5805 1 Intended Learning Outcomes To understand computer basics, programs, and operating systems. systems. To know Java’s advantages and characteristics. To To distinguish the terms API, IDE, and JDK. To To understand the Java runtime environment. To To know the anatomy of a Java program. To To create, compile, and run a simple Java program and display output on the console and on the dialog box. display EE3206/EE5805 Java Programming & Applications EE3206/EE5805 2 How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. zero and one by Memory address Memory content . . . 2000 2001 2002 2003 2004 . . . 01001010 01100001 01110110 01100001 00000011 Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3 The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character ‘J’ is is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit. minimum 3 EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Programs Computer programs, known as software, are Computer programs known software are instructions to the computer. instructions You tell a computer what to do through You programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. them. Programs are written using programming Programs languages. languages. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 4 Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built Machine into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine various Program language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: binary 1101101010011010 EE3206/EE5805 Java Programming & Applications EE3206/EE5805 5 Programming Languages Machine Language Machine Assembly Language Assembly High-Level Language Assembly languages were developed to make Assembly programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: code ADDF3 R1, R2, R3 Assembly Source File Machine Code File … ADDF3 R1, R2, R3 … Assembler … 1101101010011010 … EE3206/EE5805 Java Programming & Applications EE3206/EE5805 6 Programming Languages Machine Language Machine Assembly Language High-Level Language High The high-level languages are English-like and easy to like learn and program. For example, the following is a high-level language statement that computes the area high level of a circle with radius 5: of area = 5 * 5 * 3.1415; More natural to mankind! EE3206/EE5805 Java Programming & Applications EE3206/EE5805 7 Popular High-Level Languages BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) C (whose developer designed B first) C++ (an object-oriented language, based on C) Java (another OO language simplified from C++) Python (dynamic typing language, often used for web applications) EE3206/EE5805 Java Programming & Applications EE3206/EE5805 8 Compiling Compiling Source Code to Native Code Source A program written in a high-level language is called a source program. Since a computer cannot understand a source program. Program called a Since led compiler is used to translate the source program into a machine compiler is language program called an object program. The object program is often object The then linked with other supporting library code before the object can be then can executed on the machine. executed Platform Dependent Language (e.g. C, C++) You can port a source program to any machine with appropriate compilers that convert the source code to a specific native machine code. The source program must be recompiled, however, because the object program can only run on a specific machine. Source code Object/Machine/Native code Source File Compiler Object File Linker Excutable File EE3206/EE5805 Java Programming & Applications EE3206/EE5805 9 Compiling Java Source Code Platform Independent Language (e.g. Java) Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine (JVM) which is a software that interprets Java bytecode. Java Bytecode Create/Modify Source Code Saved on the disk Source code (developed by the programmer) Source Code Compile Source Code stored on the disk Bytecode JVM Any Computer Byte code (generated by the compiler for JVM to read and interpret, not for you to understand) Run Byteode by JVM Result EE3206/EE5805 Java Programming & Applications EE3206/EE5805 10 Java History and Versions Designed by a team from Sun Microsystems in the early 1990’s Sun Originally named “Oak” for embedded consumer electronic for applications applications Renamed to “Java” in May 20, 1995 for Internet applications The prevalence of Java is due to WWW boom The Java Development Kit (JDK) Java • • • • • • • • JDK JDK JDK JDK JDK JDK JDK JDK 1.0 (1995) 1.1 (1996) 1.2 (1998) 1.3 (2000) 1.4 (2002) 5 (2004) 6 (2006) latest stable release is Update 16 JDK 7 (in development) • http://en.wikipedia.org/wiki/Java_version_history EE3206/EE5805 Java Programming & Applications EE3206/EE5805 11 Java Platforms EE3206/EE5805 Java Programming & Applications EE3206/EE5805 12 JDK Editions Java is a general purpose programming language. There are 3 editions: editions: Java Standard Edition (Java SE) Java • Java SE can be used to develop client-side standalone side applications or applets. applications Java Enterprise Edition (Java EE) Java • Java EE can be used to develop server-side applications such as side Java servlets and Java ServerPages. servlets and ServerPages Java Micro Edition (Java ME). • Java ME can be used to develop applications for mobile devices Java such as cell phones. This course uses Java SE to introduce Java programming. This EE3206/EE5805 Java Programming & Applications EE3206/EE5805 13 Examples of Java’s Versatility Standalone Application: TicTacToe Applet: TicTacToe Servlets: SelfTest Web site Mobile Computing: Cell phones EE3206/EE5805 Java Programming & Applications EE3206/EE5805 14 Why Java? As stated in the white paper by Sun, Java is • Simple and Object-Oriented (simplify from C++) • Distributed (network programming) • Interpreted, portable and architecture-neutral • Robust and secure (exception handling, digital signature) • High-performance (Hotspot technology) Although it is suffer from the performance degradation of bytecode interpretation, some analysis show that Java’s performance is quite close to native code nowadays • Multi-threaded (simultaneous tasks) • Dynamic (no installation, no recompilation, load-on-the-fly) EE3206/EE5805 Java Programming & Applications EE3206/EE5805 15 Composing Java Source You need to install a JDK first. Java IDE Tools (recommended) • NetBeans, Open Source by Sun • Eclipse, Open Source by IBM Any Plain Text Editors (don’t do this unnecessarily) • Notepad • JEdit (with syntax highlight) We will user NetBeans in this course! EE3206/EE5805 Java Programming & Applications EE3206/EE5805 16 A Simple Java Program Any program codes in Java (e.g. variables, functions) are placed inside a class block. //This program prints Welcome to Java! //This public class Welcome { public public static void main(String args) { public args System.out.println("Welcome to Java!"); System.out.println("Welcome } } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 17 Compiling and Running Java from the Compiling Command Window (not using IDE) Command Set path to JDK bin directory Set • set path=c:\Program Files\Java\jdk1.6.0\bin Compile Compile • javac Welcome.java Run Run • java Welcome • NOT java Welcome.class java Welcome.class EE3206/EE5805 Java Programming & Applications EE3206/EE5805 18 Anatomy of a Java Program Comments Comments Package Package Reserved words Reserved Modifiers Modifiers Statements Statements Blocks Blocks Classes Classes Methods Methods The main method The EE3206/EE5805 Java Programming & Applications EE3206/EE5805 19 Comments In Java, comments are In preceded by two slashes (//) in a line; or preceded • // comment 1 enclosed between /* and */ in one or multiple lines; or enclosed • /* multiple lines */ enclosed between /** and */ enclosed • A special format used by javadoc tool to compile into API special javadoc tool specification. specification. Anything being commented are not regarded as instructions and will not be compiled. and EE3206/EE5805 Java Programming & Applications EE3206/EE5805 20 Javadoc Example Generate javadoc from NetBeans EE3206/EE5805 Java Programming & Applications EE3206/EE5805 21 Package A Java package is a mechanism for organizing Java Java classes into namespaces. into namespaces Java packages can be stored in compressed files called JAR (Java Archive) files, allowing classes to download JAR allowing faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality. similar Package is mirrored to the file structure Package • java.lang.Math (class Math in package java.lang) (class Math in java.lang • $CLASSPATH/java/lang/Math.class EE3206/EE5805 Java Programming & Applications EE3206/EE5805 22 Reserved Words and Modifiers Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it class it understands that the word after class is the name for the class. Other reserved words in Listing 1.1 are public, public static, and void. Their use will be introduced later. static and void Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public, protected, private, final, static and abstract. final, and abstract EE3206/EE5805 Java Programming & Applications EE3206/EE5805 23 Statements and Blocks A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in System.out.println("Welcome to the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). with A pair of braces {} in a program forms a block that groups components of a program. components public class Test { public static void main(String args) { System.out.println("Welcome to Java!"); } } Class block Method block EE3206/EE5805 Java Programming & Applications EE3206/EE5805 24 Classes and Methods A class is a template or blueprint for objects. • We will talk more about this in the next module. At the moment, We you just need to think it as a boilerplate that you must include it you it in every program. in The class is an essential Java construct. To program in Java, you must understand classes and be able to write and use them. Java names a function differently – “method” Similar to a function (procedural programming), a method is a collection of statements that performs a sequence of operations. collection A method must be associated with a class (i.e. placed inside a class). class). EE3206/EE5805 Java Programming & Applications EE3206/EE5805 25 main Method Any programs must provide an entry point for the start entry for of execution. The Java interpreter executes the application by invoking the main method. The main method usually provides the control of program flow. program The main method has an argument in the type of String The array and no return value: array public static void main(String args) { public args // Statements; } EE3206/EE5805 Java Programming & Applications EE3206/EE5805 26 Displaying Text Message and Displaying Terminating Program Terminating To console • System.out.println(“Hello World!”); To pop-up window • • • JOptionPane.showMessageDialog(null, “Welcome to Java!”); The first argument is a special value null The second argument is a string of message You don’t need to understand everything behind at this moment. Just try to memorize these two statements for a quick start. More explanations will be seen later. A program will be terminated by calling System.exit() or upon the end of execution. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 27 ...
View Full Document

Ask a homework question - tutors are online