java programming environment creating simple java application lexical issues java class library

34

Upload: dwayne-banks

Post on 11-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library
Page 2: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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

Page 3: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

JavaBytecodes

move locallyor through

network

JavaSource(.java)

JavaCompiler

JavaBytecode(.class )

JavaInterpreter

Just in Time

Compiler

Runtime System

Class Loader

BytecodeVerifier

Java ClassLibraries

Operating System

Hardware

JavaVirtualmachine

Runtime EnvironmentCompile-time

Environment

Java Environment/ Life Cycle of Java Code

Page 4: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

• 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”.

Page 5: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 6: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 7: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library
Page 8: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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

Page 9: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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

Page 10: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

To begin developing Java programs, followthese steps: Step 1: Obtain the Software Development

Kit (SDK) for J2SE (Java 2 Platform, Standard Edition)

Step 2: Install the SDK

Page 11: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library
Page 12: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Step 1: Use an editor to enter the following code for the HelloWoldApp program:

Save this code in a file called HelloWorldApp.java

Page 13: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Step 2: Compile the application with the command line:

> javac HelloWorldApp.java

This creates the class file (with the bytecode output):

HelloWorldApp.class

Page 14: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Step 3: Use the java command to run the program:

> java HelloWorldApp

Hello World!

The output is printed after the command line.

Output

Page 15: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 16: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 17: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 18: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 19: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 20: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 21: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

• 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]); }

} }

Page 22: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Compiling and Running:> javac ShowArgs.java> java ShowArgs 1 2

Output Argument 0=1 Argument 1=2

Page 23: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 24: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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

Page 25: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 26: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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 */.

Page 27: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Separators In Java, there are a few characters that are used

as separators. The most commonly used separator in Java is the

semicolon.

Page 28: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library
Page 29: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

The Java Keywords There are 49 reserved keywords currently defined in the Java

language

The keywords const and goto are reserved but not used.

Page 30: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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.

Page 31: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

QUIZ!!!!!

Page 32: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

1. What will be result of running the following program?

2. Explain this statement ”Java is platform independent language”.

Page 33: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

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?

Page 34: Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library

Questions?