1 / 34

Object Oriented Programming

Object Oriented Programming. Topics To Be Covered Today. Java Programming Environment Creating Simple Java Application Lexical Issues Java Class Library. Java Interpreter. Just in Time Compiler. Java Environment/ Life Cycle of Java Code. Runtime Environment. Compile-time Environment.

domingoa
Télécharger la présentation

Object Oriented 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. Object Oriented Programming

  2. Topics To Be Covered Today • Java Programming Environment • Creating Simple Java Application • Lexical Issues • Java Class Library

  3. Java Interpreter Just in Time Compiler Java Environment/ Life Cycle of Java Code Runtime Environment Compile-time Environment Class Loader Bytecode Verifier Java Class Libraries Java Source (.java) Java Bytecodes move locally or through network Java Virtual machine Java Compiler Runtime System Java Bytecode (.class ) Operating System Hardware

  4. Byte Code • Java programs are translated into an intermediate • language called bytecode. • • Bytecode is the same no matter which computer • platform it is run on. • • Bytecode is translated into native code that the • computer can execute on a program called the • Java Virtual Machine (JVM). • • The Bytecode can be executed on any computer • that has the JVM. Hence Java’s slogan, “Write • once, run anywhere”.

  5. Class Loader & Bytecode Verifier • The procedure followed to obtain and install a class goes as follows: • Loading includes the following tasks: • locate the bytecode file in a disk file • read it into the JVM's method area on the heap. • parse the class data into sections for constants, methods, fields, etc.. • create an instance of java.lang.Class to represent the class. • Linking proceeds through these 3 steps • verification - check that the basic structure and form of the class is proper. Run bytecode verification. • preparation - allocate memory for the class data such as static variables. • resolution - convert the symbolic references in the class file, such as variable names, to direct references. • InitializationThe static variables and constants must be set to either default or user assigned values. • ByteCode Verifier is called when class is first loaded in runtime environment. As part of the class loading process, a thorough verification of the bytecodes takes place to insure that the file holds a valid Java class and does not break any of the rules for class behavior.

  6. JIT Compiler • In the Java programming language and environment, a just-in-time (JIT) compiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor. • After you've written a Java program, the source language statements are compiled by the Java compiler into bytecode rather than into code that contains instructions that match a particular hardware platform's processor. The bytecode is platform-independent code that can be sent to any platform and run on that platform. • In the past, most programs written in any language have had to be recompiled, and sometimes, rewritten for each computer platform. One of the biggest advantages of Java is that you only have to write and compile a program once. • The Java on any platform will interpret the compiled bytecode into instructions understandable by the particular processor. However, the virtual machine handles one bytecode instruction at a time. Using the Java just-in-time compiler (really a second compiler) at the particular system platform compiles the bytecode into the particular system code (as though the program had been compiled initially on that platform). Once the code has been (re-)compiled by the JIT compiler, it will usually run more quickly in the computer. • The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed.

  7. JIT Compiler

  8. Home Work • List down Java Compile –time and Run-time errors.

  9. Java programming models • Java applications are stand-alone programs • must be compiled into Java byte code by Java compiler, then distributed • executed by an interpreter (Java Virtual Machine) • Java applets provide for client-side programming • Java servlets provide similar capabilities on the server-side

  10. Getting Started To begin developing Java programs, follow these steps: • Step 1: Obtain the Software Development Kit (SDK) for J2SE (Java 2 Platform, Standard Edition) • Step 2: Install the SDK

  11. Simple Java Application

  12. Simple Application • Step 1: Use an editor to enter the following code for the HelloWoldApp program: • Save this code in a file called HelloWorldApp.java

  13. Simple Application • Step 2:Compile the application with the command line: > javac HelloWorldApp.java • This creates the class file (with the bytecode output): HelloWorldApp.class

  14. Simple Application • Step 3: Use the java command to run the program: > java HelloWorldApp Hello World! • The output is printed after the command line. Output

  15. Description • The keyword class is used to declare that a new class is being defined. • HelloWorldApp is an identifier that is the name of the class. • The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}). • The use of the curly braces in Java is identical to the way they are used in C, C++. • In Java, all program activity occurs within class. This is one reason why all Java programs are object- oriented.

  16. Description • The next line of code is • All Java applications begin execution by calling main( ). • The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. • main( ) must be declared as public, since it must be called by code outside of its class when the program is started.

  17. Description • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. • The keyword void simply tells the compiler that main( ) does not return a value.

  18. Description • main( ) is the method called when a Java application begins. Java is case-sensitive. Thus, Main is different from main. • It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.

  19. Description • String arg[ ] declares a parameter named arg, which is an array of instances of the class String. Objects of type String store character strings. • In this case, arg receives any command-line arguments present when the program is executed. This program does not make use of String arg[ ]. • main( ) is simply a starting place for your program. • A complex program will have dozens of classes, only one of which will need to have a main( ) method to get things started.

  20. Description • The next line of code is shown here • This line outputs string Hello World! • System is a predefined class that provides access to the system, and out is the output stream that is connected to the console. • Output is actually accomplished by the built-in println( ) method. println( ) displays the string which is passed to it.

  21. Command Line Arguments • Differences from C –In Java String is a real type – Java arrays have an associated length – The file name is not part of the command line arguments • File ShowArgs.java: class ShowArgs { public static void main(String abc[]) { for(int i=0; i<abc.length; i++) { System.out.println("Argument " + i + " = " + abc[i]); } } }

  22. Command Line Arguments, Results Compiling and Running: > javac ShowArgs.java > java ShowArgs 1 2 • Output Argument 0=1 Argument 1=2

  23. Lexical Issues Whitespace • Java is a free-form language. • For example, the program could have been written all on one line or in any other strange way you felt like typing it. • In Java, whitespace is a space, tab, or newline.

  24. Lexical Issues Identifiers • Identifiers are used for class names, method names, and variable names. • An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. • They must not begin with a number. • Java is case-sensitive, so VALUE is a differentidentifier than Value. • Some examples of valid identifiers are: AvgTemp count a4 $test this_is_ok • Invalid variable names include: 2count high-temp Not/ok

  25. Lexical Issues Literals • A constant value in Java is created by using a literal representation of it. • For example,here are some literals: 100 98.6 ‘X’ “This is a test” • Left to right, the first literal specifies an integer, the next is a floating-point value, the third is a character constant, and the last is a string. • A literal can be used anywhere a value of its type is allowed.

  26. Lexical Issues Comments • There are three types of comments defined by Java. You already know two: single-line and multiline. The third type is called a documentation comment. • This type of comment is used to produce an HTML file that documents your program. • The documentation comment begins with a /** and ends with a */.

  27. Lexical Issues Separators • In Java, there are a few characters that are used as separators. • The most commonly used separator in Java is the semicolon.

  28. Lexical Issues The Java Keywords • There are 49 reserved keywords currently defined in the Java language • The keywords const and goto are reserved but not used.

  29. Java Class Libraries • the Java environment relies on several built-in class libraries that contain many built-in methods that provide support for such things as I/O, string handling, networking, and graphics. • The standard classes also provide support for windowed output. • Thus, Java as a totality is a combination of the Java language itself, plus its standard classes.

  30. QUIZ!!!!!

  31. 1. What will be result of running the following program? 2. Explain this statement ”Java is platform independent language”.

  32. 1. What will be result of running the following program? 2. Why is it necessary to save file with the same name as the class holding main( ) method?

  33. Questions?

More Related