Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.

Similar presentations


Presentation on theme: " Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library."— Presentation transcript:

1

2  Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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

4 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  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.  Initialization The 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  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

8  List down Java Compile –time and Run-time errors.

9  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 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

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

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

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

15  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  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  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  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  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  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 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 Compiling and Running: > javac ShowArgs.java > java ShowArgs 1 2  Output Argument 0=1 Argument 1=2

23 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 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 different identifier 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 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 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 Separators  In Java, there are a few characters that are used as separators.  The most commonly used separator in Java is the semicolon.

28

29 The Java Keywords  There are 49 reserved keywords currently defined in the Java language  The keywords const and goto are reserved but not used.

30  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.

31 QUIZ!!!!!

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

33 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?

34 Questions? 34


Download ppt " Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library."

Similar presentations


Ads by Google