1 / 164

Chapter One Fundamentals of Programming

Chapter One Fundamentals of Programming. Contents. Introduction History of Java Java Technology Java Development Kit (JDK) JDK Editions JDK Versions Java Runtime Environment (JRE) Characteristics of Java Why Java? The five phases of Java Example Java Programs

Télécharger la présentation

Chapter One Fundamentals of 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. Chapter One Fundamentals of Programming

  2. Contents • Introduction • History of Java • Java Technology • Java Development Kit (JDK) • JDK Editions • JDK Versions • Java Runtime Environment (JRE) • Characteristics of Java • Why Java? • The five phases of Java • Example Java Programs • Compiling and running java programs

  3. Programs • Computer programs, known as software, are instructions to the computer. • We tell a computer what to do through programs. • Without programs, a computer is an empty machine. • Computers do not understand human languages, so we need to use computer languages to communicate with them. • Programs are written using programming languages.

  4. Programming Languages • Machine language is a set of primitive instructions • built 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 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: • 1101101010011010 Machine Language Assembly Language High-Level Language

  5. Programming Languages Assembly languages were developed to make 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: ADDF3 R1, R2, R3 Machine Language Assembly Language High-Level Language

  6. Programming Languages The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415; Machine Language Assembly Language High-Level Language

  7. Popular High-Level Languages • COBOL (COmmon Business Oriented Language) • FORTRAN (FORmulaTRANslation) • BASIC (Beginner All-purpose Symbolic Instructional Code) • Pascal (named for Blaise Pascal) • Ada (named for Ada Lovelace) • C (whose developer designed B first) • Visual Basic (Basic-like visual language developed by Microsoft) • Delphi (Pascal-like visual language developed by Borland) • C++ (an object-oriented language, based on C) • Java

  8. History of Java • Java • Was created in 1991 and formally announced in 1995 by James Gosling et al. of Sun Microsystem. • Initially calledOak, latter was changed to Java because there was already a language called Oak.

  9. History of Java… • The original motivation for Java • The need for platform independent language that could be embedded in various consumer electronic products like toasters and refrigerators. • At about the same time, the World Wide Web and the Internet were gaining popularity. • Gosling et. al. realized that Java could be used for Internet programming.

  10. Java Technology • Java technology is more than just a programming language. • As a programming language, Java can create all kinds of applications that you could create using any conventional programming language. • Java technology applications are typically general-purpose programs that run on any machine where the Java runtime environment (JRE) is installed.

  11. Java Technology • It is a completely new approach to software development. • Java Technology mainly consists two parts : JDK and JRE.

  12. Java Development Kit (JDK) • JDK is A free software development package from Sun Microsystemsthat implements the basic set of tools needed to write, test and debug Java applications and applets. • i.e. JDK is Java Software Development kit (SDK)

  13. Java Development Kit (JDK) • It is thetoolkit for developers that includes the Java compiler and the runtime environment. • To write Java programs, you need the JDK installed. • The major component of JDK are • Java compiler (.java file  .class file) • Java launcher and jdb debugger

  14. JDK Editions • Java Standard Edition (J2SE) • J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) • J2EE can be used to develop server-side applications such as Java servletsand Java ServerPages. • Java Micro Edition (J2ME) • J2ME can be used to develop applications for mobile devices such as cell phones.

  15. JDK Versions • JDK 1.02 (1995) • JDK 1.1 (1996) • JDK 1.2 (1998) • JDK 1.3 (2000) • JDK 1.4 (2002) • JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 • JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 • JDK 1.7 (late 2010) a. k. a. JDK 7 or Java 7

  16. Java Runtime Environment (JRE) • JREis the program that emulates the JVM, so that users can run Java programs. • To run Java programs, you need download and install the JRE. • Runs code compiled for a JVM and performs class loading (through the class loader), code verification (through the bytecode verifier) and finally code execution. • The major component of JRE are • Java platform core classes libraries • Java virtual machine (JVM)

  17. Why Java? • The answer is that Java enables users to develop and deploy applications on the • Desktop computers…………..Standalone Application • Internet for servers …………..Servlets, Applets • Small hand-held devices ……. Mobile Application • The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. • Java is the Internet programming language. • Java is a general purpose programming language.

  18. Phases of a Java Program • Java programs normally go through five phases – edit, compile, load, verify andexecute

  19. Phase 1: Creating a program … • Involves editing a file with an editor • The following are some JavaIntegrated Development Environments (IDEs) • Jbuilder, NetBeans , Sun ONE Studio, Eclipse, jEdit, Jcreator, BlueJ, jGRASP, etc. • Most of this editors can be downloaded for free • Any text editor can also be used for creating Java programs • Programs written on IDEs or any other editors are known as Source Code. • Java source code file names end with .java extension

  20. Phase 2: Compiling Java Programs into Bytecodes • Since a computer cannot understand a source program, program called a compiler is used to translate the source program into a machine language program called an object program. • 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

  21. Phase 2: Compiling Java Programs into Bytecodes.. • A virtual machine is a software application that simulates a computer-JVM is one of the most widely used virtual machine • Java Virtual Machine, which is part of JRE, is a software that interprets Java bytecode. • To compile Java Program • Javac welcome.java • To execute java programs • Java welcome

  22. Phase 3: Loading a program • A program must be placed in memory before it can execute – a process known asloading. • The class loader takes the .class file (produced during compilation process) containing the program’s bytecodes and transform them to primary memory. • The class loader also loads any of the .class files provided by java that your program uses. • The .class files can be loaded from a disk on your system or over a network

  23. Phase4 : ByteCode Verification • Involves examining bytecodes to ensure that they are valid and do not violate Java’s security restriction. • Java enforces strong security, to make sure that Java programs arriving over the network do not damage your files or your system (as computer viruses and worms might)

  24. Phase 5: Execution • The JVM executes the program’s bytecodes, thus performing the actions specified by the program. • .class file is not final machine code yet. It is virtual machine code. JVM need a second compilation to convert virtual machine code to real machine code. We call it just-in-time compiler (JIT). • This greatly affects the speed of the execution. • Traditionally, our C/C++ program can finish all the compilation before execution. The generated machine code could run directly on OS or hardware

  25. Phase 5: Execution…… • But in Java Language, the java virtual machine is standing in our way and adds another level of compilation to the system. • JVMs typically execute bytecodes using a combination ofinterpretationand so-called just-in-time (JIT) compilation. • In this phase bytecodes are translated into the underlying computer’s machine language.

  26. A Simple Java Program • The following code is a java program that prints the string “ Welcome to Java Programming!”. • Now lets make a line by line investigation of the program /* Your first Java program; Welcome.java The program prints a string Welcome to Java Programming */ public class Welcome { //main method begins execution of Java application public static void main (String args[] ){ System.out.println(“Welcome to Java”); }//end of main method } //end of class Welcome

  27. A Simple Java Program… • Lines 1-3 & 5 comment lines • There are two types of comments in Java • End-of-line (Single line) comments • A line that begins with // • A // comment also can begin in the middle of a line and continue until the end of that line • Traditional (Multiple line ) Comments • A text that begins in /* ends in */ • All text between these delimiters is comment so it is ignored by the compiler. • These comment can be applied for a line, more than a lines or part(s) of a line

  28. A Simple Java Program… • Javadoc comments • A text that begins in /** and ends in*/ • As with traditional comments, all text between the Javadoc comment delimiters is ignored by the compiler. • Javadoc comments enable programmers to embed program documentation directly in their programs.

  29. A Simple Java Program… • Line 4: public class Welcome { • This line begins class declaration of the class Welcome • Every program in Java consists of at least one class declaration . • Generally, Java class decleration has the following format: [Access level Specifier] class class_Name { … }

  30. A Simple Java Program… • The class is the essential Java construct. A class is a template or blueprint for objects. • To program in Java, you must understand classes and be able to write and use them. • The mystery of the class will continue to be unveiled throughout this course. For now, though, understand that a program is defined by using one or more classes.

  31. A Simple Java Program… • Access level Specifier • Could be omitted (none), public, private, or protected • These are keywords that help to set the visibility and accessibilityof a class, its member variables, and methods. • They determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class.

  32. A Simple Java Program… • class • The class keyword introduces a class declaration in Java and is immediately followed by the class name (Welcome). • class_Name • Is the name of a class. A Java class name is an identifier. • By convention class names should start with capital letters • Java is case Sensitive

  33. A Simple Java Program… • Line 6: public static void main (String args[]){ • Is a method of which the starting point of every java application. • Every Java application must contain one main method • public is a keyword which specifies access level of the method. • The JVM executes the application by invoking the main method

  34. A Simple Java Program… • staticis another keyword [its meaning is to be discussed latter] • void is a return type that indicates that this method performs a task but will not return any information when it completes its task. • String args[] … is an array of strings which is an argument of the main function.

  35. A Simple Java Program… • Line 7: System.out.println( "Welcome to Java Programming!" ); • This line prints the string contained in quotation • System.out is known as the standard output object. • When System.out.println completes its task, it positions the output cursor (the location where the next character will be displayed) to the beginning of the next line while System.out.printdon’t.

  36. A Simple Java Program… • Every statement of java must end in semicolon (;) so is line 7 • Lines 8 and 9 • This line depicts the end of the main method and the class welcome.

  37. Creating and Editing Using Notepad To useNotepad, type notepad Welcome.java from the DOS prompt.

  38. Creating and Editing Using WordPad To use WordPad, type write Welcome.java from the DOS prompt.

  39. Compiling and Running Java programs • Using Command line • If you are using a text editor which is not specially designed as Java IDE , java program are compiled and run using command line. • Before actually starting compiling and running java source code, environmental variable path must be set (How?) • Set path to JDK bin directory • set path=c:\Program Files\java\jdk1.6.0\bin • Set classpath to include the current directory • set classpath=.

  40. Compiling and Running Java programs…. • Then open command line prompt • To Compile Javac […\]Welcome.Java • To Run Java […\]Welcome • Compiling the source could will add a new file , Welcome.class to the folder containing the source code

  41. Creating, Compiling, and Running Programs

  42. Displaying Text in a Message Dialog Box • Display • Most Java applications use windows or a dialog box • We have used command window • Class JOptionPane allows us to use dialog boxes

  43. Displaying Text in a Message Dialog Box… Packages • Set of predefined classes for us to use • Groups of related classes called packages • Group of all packages known as Java class library or Java applications programming interface (Java API). • JOptionPane is one of the predefined classes in the javax.swing package that has classes for using Graphical User Interfaces (GUIs). • We can use the showMessageDialog method in the JOptionPane class to display message in Dialog box .

  44. The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", “Display Message", JOptionPane.INFORMATION_MESSAGE);

  45. Two Ways to Invoke the Method • There are several ways to use the showMessageDialog method. For the time being, all you need to know are two ways to invoke it. • One is to use a statement as shown in the example: JOptionPane.showMessageDialog(null, x,y,JOptionPane.INFORMATION_MESSAGE); • wherex is a string for the text to be displayed, and y is a string for the title of the message dialog box. • The other is to use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a string for the text to be displayed.

  46. importjavax.swing.JOptionPane; public class Welcome { public static void main( Stringargs[]) { JOptionPane.showMessageDialog( null, Welcome\nto\nJava\nProgramming!" ); } }

  47. import declarations • Used by compiler to identify and locate classes used in Java programs. • Tells compiler to load class JOptionPane from javax.swing package. • Class System part of package java.lang • Noimportdeclaration needed • java.lang automatically imported in every Java program

  48. The exit Method • Prior to JDK 1.5, you have to invoke System.exit() to terminate the program if the program uses JOptionPane dialog boxes. • Since JDK 1.5, it is not necessary.

More Related