Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.

Similar presentations


Presentation on theme: "Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual."— Presentation transcript:

1 Object Oriented Programming Lecture 3

2 Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual machine (JVM)  The Java platform

3 Java Programming Language  An object-oriented programming language, which is developed by Sun Microsystems.  A familiar C/C++ style of notation.  Use the object-oriented programming methodology.  Allow the same program to be executed on multiple operating systems.  Contain build-in support for using computer networks.

4 The Java Platform  The Java platform, is the environment in which a program runs. Two components:  The Java virtual machine  The Java application programming interface (API)

5 The Java Virtual Machine  The heart of the Java Platform is the concept of a "virtual machine" that executes Java bytecode programs.Java bytecode  Sits between the Java program and the machine it is running on.  Offers the program an “abstract computer” that executes the Java code  virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

6 Contd…..  Through the Java VM, the same application is capable of running on multiple platforms.

7 Java Programming Process

8 Key benefits of Java  Simple: automatic memory allocation and garbage collection  Platform independent.  Network-Centric programming, Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.

9 Java Editions  J2ME (Micro Edition) – Used to create programs that run on small handheld devices, such as phones, PDAs (personal digital assistants), and appliances.  J2SE (Standard Edition) – Used primarily to create programs for desktop computers or for many computers too large for J2ME and too small for J2EE.  J2EE (Enterprise Edition) – Used to create very large programs that run on servers managing heavy traffic and complicated transactions. These programs are the backbone of many online services, such as banking, e-commerce systems etc.

10 Installing Java  The Java Development Kit (JDK) is a collection of software available at no charge from Sun Microsystems, Inc.  The v1.6 download is available at java.sun.com.

11 Java Program Structure  A Java program always consists of one or more classes.  You typically put the program code for each class in a separate file, and  You must give each file the same name as that of the class that is defined within it.  A Java source file name must have the extension.java.

12 Examples

13 Sample Java Program class Hello { public static void main ( String[] args ) { System.out.println("Hello World!"); }

14 Getting Started: Create the source file: open a text editor, type in the code which defines a class Hello and then save it in a file (Hello.java) file and class name are case sensitive and must be matched exactly (except the.java part)  Java is CASE SENSITIVE! Be Careful When You Type  Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher tool (java) are case-sensitive, HelloWorldApp helloworldapp

15  The compiler requires the source code files to be named according to specific rules:  Correct: SimpleProgram.java  Incorrect: simpleprogram.java (wrong case) SimpleProgram.java.doc (wrong extension) Simple~1.java.doc (Microsoft Windows short- names not allowed)

16 (2) Compile the program: compile Hello.java by using the following command: javac Hello.java it generates a file named Hello.class

17

18 At the prompt, type the following command and press Enter. javac HelloWorldApp.java The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.

19 3) Run the program: run the code through: java Hello Note that the command is java, not javac, and you refer to Hello, not Hello.java or Hello.class

20 Compiler Problems :Common Error Messages  'javac' is not recognized as an internal or external command, operable program or batch file  If you receive this error, Window cannot find the compiler ( javac ).  Suppose you installed the JDK in C:\jdk6. At the prompt you would type the following command and press Enter: C:\jdk6\bin\javac HelloWorldApp.java If you choose this option, you'll have to precede your javac and java commands with C:\jdk6\bin\ each time you compile or run program.

21 Compiler Problems :Common Error Messages  Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested  If you receive this error, you forgot to include the.java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.

22 Runtime Problems :Common Error Messages  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp  If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.  One of the places java tries to find your.class file is your current directory. So if your.class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:  cd c:\java The prompt should change to C:\java>. If you enter dir at the prompt, you should see your.java and.class files. Now enter java HelloWorldApp again.

23 Runtime Problems :Common Error Messages  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class  A common mistake made by beginner programmers is to try and run the java launcher on the.class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename.  Exception in thread "main" java.lang.NoSuchMethodError: main  The Java VM requires that the class you execute with it have a main method at which to begin execution of your application.

24 Java Class  A class is an object oriented construct. It is designed to perform a specific task.  A Java class is defined by its class name, an open curly brace, a list of methods and fields, and a close curly brace.  The name of the class is made of alphabetical characters and digits without spaces, the first character must be alphabetical.

25  The line public static void main ( String[] args ) shows where the program will start running. The word main means that this is the main method –  The JVM starts running any program by executing this method first.  The main method in Hello.java consists of a single statement System.out.println("Hello World!");  The statement outputs the characters between quotes to the console.

26 Syntax Errors public Class Hello { public static void main ( String[]args) { System.out.println("Hello World!"); }  The required word "class" has been changed to "Class" with a capital "C". This is called a syntax error.  A syntax error is a “spelling or grammatical error" in the program.  The error message is not very clear. But at least it shows where the error is.  The compiler will not create a new bytecode file because it stops compiling when it gets to an error.

27 Comments // This program outputs “Hello World I am // learning Java” to screen public class MyFirstProgram { public static void main ( String[] args ) { // the output statement starts here System.out.println("Hello World!"); // this statement outputs “Hello World” System.out.println(“I am learning Java”); }  A comment is a note written to a human reader of a program. The program compiles and runs exactly the same with or without comments. Comments start with the two characters "//" (slash slash). Those characters and everything that follows them on the same line are ignored by the java compiler.

28 Comments /* This is my first Java program and I am pretty excited about it. */ public class MyFirstProgram { public static void main ( String[] args ) { System.out.println("Hello World!"); // this statement outputs “Hello World” System.out.println(“I am learning Java”); }  With this style of comment, everything between the two characters "/*" and the two characters "*/" are ignored by the compiler. There can be many lines of comments between the "/*" and the "*/".


Download ppt "Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual."

Similar presentations


Ads by Google