1 / 58

ITM 172

ITM 172. The Java Programming Language Chapter 1 – Part A Introduction to Java and the Development Tools. Java: Why it is a popular programming language. Main reason: Programs written in Java can run on any type of computer.

jalia
Télécharger la présentation

ITM 172

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. ITM 172 The Java Programming Language Chapter 1 – Part A Introduction to Java and the Development Tools

  2. Java: Why it is a popular programming language • Main reason: Programs written in Java can run on any type of computer. • A computer’s processor has an instruction set. (a list of all the instructions it understands). • A Pentium IV processor’s instruction set is not identical to DEC’s Alpha processor’s instruction set or Sun’s Sparc processor’s instruction set. • Therefore, if you write a program and then compile it so that it runs on a Pentium processor, it might not run on an Alpha or Sparc processor (because the program contains instructions that are not in that processor’s instruction set). • When Java programs are compiled, they can run on any type of processor (Therefore, Java programs have high portabilityand arearchitecturally-neutral).

  3. The portability of Java programs • The way that Java programs must be compiled into bytecode and then interpreted by the JVM is what makes Java programs completely portable. • Portable means that a Java application can run on any hardware/operating system combination. • Since Java programs are not translated into a specific processor’s machine language until runtime, the program can ‘wait’ until it sees what platform it has ‘landed on’ before it finishes compiling. Then it can adapt itself by compiling into any specific processor’s machine language at runtime.

  4. Other advantages of Java • Simple. It’s easier to learn than C++ • Object-oriented. Programming code is organized into modules/building blocks/objects. Huge libraries of these “building blocks” already exist. Programs can be built using standard building blocks. • Distributed. Java programs can interface (communicate with) other programs running on other computers using standardized communication methods. • Multi-threaded. The Java language allows the programmer to write programs that can “spawn processes simultaneously”.

  5. Other advantages of Java • Dynamic. Java is an extensible language. That means, new functionality can be added indefinitely. • Robust. Java programs don’t crash as readily as programs written in other languages because the compiler does some “forward thinking” error detection and correction. • Secure. Viruses cannot embed themselves in Java programs.

  6. No Source Code file (*.java) Compile (using javac.exe) Syntax OK? No Yes Logic OK? Interpret & execute the bytecode (using java.exe) Bytecode Class files (*.class) Yes Turn it in! Java Application Development 1 4 2 3 Write Source Code in Java 5 7 6 8 java.exe is the VIRTUAL MACHINE

  7. The Java platform • In order for an application program to run on a computer, certain resources must be available to it. • Resources consists of hardware and software. Collectively, these resources are called the ‘hosting’ platform. • The next slides will explain what a platform is, and then what platform Java applications require.

  8. What is a computer platform? • In computing, a platform describes all the hardware and software programs that must be available (as resources) in order for an application to run. • Example: A personal computer (complete with a CPU and a RAM memory) running the Windows Vista operating system. • Example: A high-end computer (multiple processors, many GBs of RAM) running the Unix operating system.

  9. What does the platform provide? • The application program cannot carry out all computing tasks on its own without help from other programs (we call ‘services’) that are included in the operating system software package and the hardware. • For example, Microsoft Word cannot carry out all the word processing tasks (such as printing, saving and opening files, etc.) without the help of the operating system’s helper programs (some may be in .dll files) and the hardware (CPU, RAM, disk drive, printer).

  10. The Java Platform • A Java application cannot execute unless it is running on a computer hosting the Java platform. • The Java platform consists of these ‘components’ that must be available at runtime: the hardware, the operating system, and the JRE(the Java runtime environment). • Non-Java applications only need two components in their platform: the hardware and the operating system.

  11. The Java Platform So, to run a Java application, there must be: • Hardware (a CPU, RAM, etc.) • an operating system • The JRE a. the JVM (the byte code interpreter and the program launcher) b. the class library (helper modules – similar to .dll files that augment the functionality of the operating system).

  12. JRE: Part A: The JVM (Java Virtual Machine) • The JVM consists of two things: it uses its interpreter to translate the byte code into machine language and it uses its launcher to initiate the running of the application. • The JVM’s interpreter is the program that completes the translation process by translating the byte code into a specific processor’s machine language.

  13. JRE Part B:The class library • The other component of the JRE is the class library (aka API - Application Programming Interface) • The API is a large collection of reusable ‘modules’ of code. Many of the modules in this library are used like ‘.dll’ files – providing ‘helper’ programs that augment the functionality of the operating system.

  14. Java Development Tools • We just finished discussing what ‘resources’ are needed to run a Java application. • Now we will discuss what resources are available to develop a Java application. • These resources are called ‘tools’: • The JDK (Java Development Kit) • An IDE (NetBeans)

  15. The Java Development Toolkit (JDK) • To create, compile, and run a Java program, you need a program called the Java Development Toolkit (JDK). The toolkit includes: • javac – The compiler, which converts source code into Java bytecode • jar – The archiver, which packages related class libraries into a single file (similar to a ‘zip’ program). • javadoc – The documentation generator, which automatically generates documentation from source code comments • jdb – The debugger • JRE (for testing purposes)

  16. The Integrated Development Environment (IDE) • The IDE is like the ‘dashboard’ of your car: it makes several programming tools accessible from one “integrated”, GUI-style screen. • These tools include the text editor, compiler, run-time module, debugger, and help system. The IDE also ‘forces’ you to organize your files into projects and packages. • The name of our IDE is NetBeans. (Other products that are IDEs include: Forte by Sun, Jbuilder by Borland, Visual J++ by Microsoft, Visual Café by WebGain).

  17. If you want to download and install the JDK and the IDE (Netbeans) on your home computer, go to www.java.sun.com and select this link.

  18. Download this version. On the next link, select the Windows platform (not Linux or Solaris).

  19. Getting started (Lab 1) • The JDK version J2SE 6.3 (containing the JRE) and Netbeans 6.0 have already been installed on your PC.

  20. Lab 1.1- Part A - Class Welcome in Package Chapter1 • This program displays the message “Welcome to Java” on the screen (in the standard output window – not in a Window’s window).

  21. Start the IDE (NetBeans): From the Window’s desktop, click [Start], [Programs], [NetBeans 6.3], [NetBeans IDE]…

  22. See the NetBeans window.

  23. If you use NetBeans, all programs must be encapsulated in a project. A project folder is created and default settings are applied to the project folder. To create a project, from the menu, click [File], [New Project]…

  24. … chose [General] for the project category and [Java Application] for the type of project. Click [Next]…

  25. Type the Project Name: Welcome Type the Project Location: C:\251lastnameLABS Unselect [Create Main Class]. Click [Finish].

  26. See the project name appear in the Projects panel. At least two folders are created below the projects folder. Click the [+] to expand the ‘Welcome’ project folder, and then click the [+] to expand the ‘Source Packages’ folder. Just as a project is a ‘container’ for an application, a package is a ‘container’ (also a folder) for a collection of similar files within a project.

  27. All Java code must be encapsulated in class files. Therefore, the next step is to create a class/file. From the menu, select [File], [New File]…

  28. …in the “New File” window, click [Java Classes] as the Category and [Java Main Class] as the file type. Click [Next]…

  29. welcome …type the class name: Welcome (same as the project name). Type the package name: welcome. Click [Finish].

  30. The class structure The ‘main’ method structure A file is created that contains some ‘startup’ code. It opens in the text editor window. A class structure is created (with the class name ‘Welcome’) and a method is created (with the name ‘main’).

  31. Type your name and the lab number in the comment area in the top. Delete everything except what you see in this screen shot.

  32. To create our first Java program, that displays the words, “Welcome to Java”, type in method main: System.out.println(“Welcome to Java”);

  33. To compile the program, from the menu, select [Build], [Compile “Welcome.java”] or press F9.

  34. The output window shows if you have any syntax errors. If not, you will see the message “BUILD SUCCESSFUL”.

  35. To run the program, from the menu select [Run], [Run other], [Run Welcome.java] or press Shift-F6.

  36. When the program runs, you will see the message ‘Welcome to Java’ appear in the output window.

  37. Minimize the NetBeans application window to view the Windows desktop. Click on [Folders] view on the toolbar. Navigate to the source file that you created. It is under [Local Disk C], [251lastnameLABS], [Welcome], [build], [classes], [welcome]. See one file: Welcome.class

  38. Restore the NetBeans window so you can exit the program.

  39. Anatomy of a Java Program • Programming statements are grouped into methods. • Methods and variables are grouped into classes. • Each class is stored in its own unique file with the same name as the class. • Files are grouped into packages • Packages are grouped into projects.

  40. System.out.println • To display a line of text on a computer screen, we invoked the pre-existing method: println( ). • When you want your program to display a string of text in the standard output window, invoke the println method and “pass” it the string you want displayed (“Welcome to Java”). • The println( ) method is method belonging to the ‘outstream’ class, and ‘out’ is an instance of an outstream. ‘out’ is declared in the System class. • The System class is in the java.lang package, which is in the API (the collection of class files included in the JRE). Recall that the JRE contains a collection of ‘helper’ modules that can be ‘called upon’ to perform certain processes on behalf of your program.

  41. Comments • Lines that begin with // or multiple lines enclosed in /* …. */ Example: // This is a comment. /* This is also a comment. */ • Comments are ignored by the compiler.

  42. Reserved Words • Reserved or key words have special meaning to the compiler and cannot be used for other purposes in a program • Example: words like class, package, public, main, static, void.

  43. Modifiers • Modifiers are key words that specify properties of data, methods, and classes. For example, the key word public specifies whether other methods may invoke the methods and/or access the data (variables that are) declared in this class.

  44. Blocks • A pair of curly braces { } form a block. There are class blocks, method blocks, and if blocks, while blocks, etc.

  45. The main( ) method • All executable classes must contain a method named main( ). • Main( )will be the first method invoked when a class (or an instance/object of that class) is loaded into memory. • Because main( ) is method, it must have a header and a body. Its header looks like this: public static void main(String[ ] args) • Later, this will make more sense to you.

  46. Lab 1.1 – Part B – modify class Welcome in Package Chapter1 • In this program, we simply modify the way we display the output. • The output of this program is a text string: “Welcome to Java”. • Instead of displaying the string in the “standard output window”, we’ll display it in a messageDialog “box”.

  47. Using GUI objects in a Java Program • If you don’t specify otherwise, the output (“Welcome to Java”) of a Java program (such as Welcome.java) will display in the “standard output window”. • Modern day programs generally display their output in a Window’s window called a GUI (Graphic User Interface) component. An example of a GUI component that might be used to display output is a message dialog box. • We will modify Welcome.java so that its output displays in a message dialog box instead of the ‘standard output window’.

  48. Start the IDE (NetBeans): From the Window’s desktop, click [Start], [Programs], [NetBeans 6.0], [NetBeans IDE]…

  49. welcome; The last file that we edited should display in the editing window. (If not, click [File], [Open Project].

  50. Edit the file Welcome.java as indicated.

More Related