25
Module 1 Getting Started

A begineers guide of JAVA - Getting Started

Embed Size (px)

Citation preview

Page 1: A begineers guide of JAVA - Getting Started

Module 1

Getting Started

Page 2: A begineers guide of JAVA - Getting Started

Objectives

• Describe key features of Java technology. • Describe the Java virtual machine’s (JVM) function.• Describe how garbage collection works.

• Write, compile, and run a simple Java application. • List the task performed by Java platform that handle code security

Page 3: A begineers guide of JAVA - Getting Started

Definition

• A high-level programming language • Originally called OAK .• In 1995 name changed to Java and also modified to take advantage of World Wide Web.

Page 4: A begineers guide of JAVA - Getting Started

How Java works

• A program is both compiled and interpreted. • With the compiler, first you translate a program into an intermediate language called Java bytecodes • This platform-independent code is interpreted by the interpreter • The interpreter parses and runs each Java bytecode • Compilation happens just once• Interpretation occurs each time the program is executed.

Page 5: A begineers guide of JAVA - Getting Started
Page 6: A begineers guide of JAVA - Getting Started

Key Features of Java Programming Language

1. Platform Independence Java compilers do not produce native object code

for a particular platform but rather ‘byte code’ instructions for the Java Virtual Machine (JVM).

2. Object Orientation Java is a pure object-oriented language. This means

that everything in a Java program is an object and everything is descended from a root object class.

Page 7: A begineers guide of JAVA - Getting Started

3. Rich Standard Library The Java environment includes hundreds of classes and methods in six major functional areas.

1. Language Support 2. Utility3. Input/output 4. Networking5. Abstract Window Toolkit 6. Applet

4. Familiar C++ like Syntax

5. Garbage Collection - Java does not require programmers to explicitly free dynamically allocated memory. This makes Java programs easier to write and less prone to memory errors.

Page 8: A begineers guide of JAVA - Getting Started

6. Multithreaded Java Support multithreaded programming,which allow you to write programs that do many things simultaneously.

Page 9: A begineers guide of JAVA - Getting Started
Page 10: A begineers guide of JAVA - Getting Started
Page 11: A begineers guide of JAVA - Getting Started
Page 12: A begineers guide of JAVA - Getting Started

Java Virtual Machine Functionality

• Provides hardware platform specifications

• Reads compiled byte codes that are platform independent

• The Java interpreter installed on your computer implements the Java VM.

Page 13: A begineers guide of JAVA - Getting Started

Java Platform

Page 14: A begineers guide of JAVA - Getting Started

Java Development Kit

• A Java software development environment from Sun.

• It includes the compiler, JVM, debugger and other tools for developing Java applets and applications.

• Each new version of the JDK adds features and enhancements to the language.

• When Java programs are developed under the new version,the Java interpreter (Java Virtual Machine) that executes them must also be updated to that same version.

Page 15: A begineers guide of JAVA - Getting Started

Java Runtime Environment

A subset of the Java Development Kit (JDK), provides the minimum requirements for executing a Java application. The JRE includes the Java Virtual Machine (JVM), core classes, and supporting files.

Page 16: A begineers guide of JAVA - Getting Started

ClasspathA way to tell the JVM, where to find your source code, as well as any other classes or objectsthat are required by your application.

The CLASSPATH contains directories (or JAR files), from where your java compiler/runtime will look for .class files

PathThe PATH variable contains directories where binary files (e.g. EXE files in Windows) will be looked for.

Page 17: A begineers guide of JAVA - Getting Started
Page 18: A begineers guide of JAVA - Getting Started

ConsiderMyClass myRef = new MyClass();

this creates an object somewhere in the heap, it also creates a reference variable called 'myRef' that effectively points to the spot in the heap where the object is.

Consider the right side indicates the heap and left side is list of references, draw an arrow to show where each ref is pointing to.

MyClass ref2 = myRef;We have two things on the left, each with an arrow pointing to the single object on the right.

Page 19: A begineers guide of JAVA - Getting Started

Go through the code line by line, drawing and erasing arrows as Needed, you can then see when an object has no arrows pointing to it, thus making it eligible for garbage collection.

Ways to call garbage collector algorithms

In reality, it is possible only to suggest to the JVM that it perform garbage collection. However, there are no guarantees the JVM will actually remove all of the unused objects from memory.

The garbage collection routines that Java provides are members of the Runtime class.

The Runtime class is a special class that has a single object (a Singleton) for each main program.

Page 20: A begineers guide of JAVA - Getting Started

The Runtime object provides a mechanism for communicatingdirectly with the virtual machine.

To get the Runtime instance, you can use themethod Runtime.getRuntime(), which returns the Singleton.

Once you have the Singleton you can invoke the garbage collector using the gc() method.

Alternatively, you can call the same method on the System class, which has static methods that can do the work of obtaining the Singleton for you.

System.gc();

Example CheckGC.java

Page 21: A begineers guide of JAVA - Getting Started
Page 22: A begineers guide of JAVA - Getting Started
Page 23: A begineers guide of JAVA - Getting Started

Class Loader

Loads all classes necessary for the execution of a program.

Bytecode Verifier

Java compilers perform extensive compile-time checking on the source code they compile.

However, the Java Virtual Machine (JVM) cannot rely on compile-time checking because it has no way of determining whether the class files it interprets were produced by a trustworthy Java compiler or by a malicious user.

 

Page 24: A begineers guide of JAVA - Getting Started

Verifier serves to make sure that class files loaded into the JVM respect certain security-related properties like

• the class file’s syntax, • the behavior of its code and • the potential interdependencies that it has with other class files.

Page 25: A begineers guide of JAVA - Getting Started

Suppose If a buggy compiler generated a class file that contained a method whose bytecodes included an instruction to jump beyond the end of the method, that method could, if it were invoked, cause the virtual machine to crash.

Thus, for the sake of robustness, it is important that the virtual machine verify the integrity of the bytecodes it imports.