Introduction to Java Programming_Lecture001

24
Introduction to Programming Lecture 01 Sheikh Qaisar Ay ub, Sulma Rashid Prepare these presentations to help new students to build the programming Skills in JAVA. (SCJP .6 certif ied Professional) [email protected]

Transcript of Introduction to Java Programming_Lecture001

Page 1: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 1/24

Introduction to Programming

Lecture 01

Sheikh Qaisar Ayub, Sulma Rashid

Prepare these presentations to help new students to build theprogrammingSkills in JAVA.

(SCJP.6 certified Professional)

[email protected]

Page 2: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 2/24

What is Language ?³Language is mean of communication´ 

What is Computer Language ?

³Computer language is mean of communicating with computer´ 

Types of Computer Language ?

High Level Language (needs Interpreter/Compiler)Low Level Language (Instructions directly given to computer)

What is Computer Program ?

³Sequence of instructions given to computer in order to carry out a task´ 

What is difference between computer programs and software ?

Types of Software/programs

System SoftwareApplication Software

Introduction to Programming

User 

Application program

Operating System

Hardware

Page 3: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 3/24

What is JAVA ?

Introduction to Programming

Java is computer language developed by under team leader Gosling at sun Microsystems in 1991with name Oak, renamed with JAVA in 1995.

Web Programming/applets:

Page 4: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 4/24

What is JAVA ?

Introduction to Programming

Develop applications on server side: JAVA SERVLET 

Page 5: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 5/24

What is JAVA ?

Introduction to Programming

Develop applications on server side: JAVA SERVLET 

Page 6: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 6/24

What is JAVA ?

Introduction to Programming

Develop Desktop Applications

Page 7: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 7/24

What is JAVA ?

Introduction to Programming

Mobile applications

Page 8: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 8/24

GOALS of JAVA Language

Introduction to Programming

Java Is Simple

� Java is simple as compared to C++.

� Automatic memory management and garbage collection.

� Well documented easy to understand syntax.

Java Is Object Oriented

� Provide reusability / security.

Java Is Distributed

� Distributed computing involves where several computers working as unit to perform a task.

Java Is Interpreted

� A program compiled in java can be executed on all platforms.

Java Is Robust

�A java compiler can detect many errors at compile time which may appear as exception incase of other languages.

Java Is Secure

� When java applet execute it prevents harmful action by applications.

Page 9: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 9/24

GOALS of JAVA Language

Introduction to Programming

Java Is Architectural Natural

� As java use interpreted code, hence code written in java can run on any plate from, it isindepended of architecture of hardware.

Java programs are portable

� Due to architecture independed, once java program compiled can be executed on any plateform.

Java Is Multithreaded

� A java program can perform multiple task at same time.

Java Is Dynamic

� New features can be added in JAVA, without any modification to existing application.

Page 10: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 10/24

How these Goals are achieved ?

JVM

Garbage collection

Code security

Page 11: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 11/24

Byte Code

Most of Languages (C++,PASCAL compile source code of program into machine code which dependson specific machine architecture.

If machine architecture changes, needs to recompile the program.

Introduction to Programming

Drawback

JavaCompiler

Source Code

Byte Code

Page 12: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 12/24

How ? JVM (Java Virtual Machine)

Introduction to Programming

� Java byte code runs on specific type of microprocessor called JVM.

� Unlike physical microprocessor ,JVM is a logical component of JAVA.� The Java Virtual Machine is responsible for interpreting Java byte code and translating this into

actions or Operating System calls

Java Program

Compilation

Byte Code

JVM

Windows Linux Unix

Page 13: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 13/24

Part of JVM

Introduction to Programming

� A set of registers� A stack� A garbage-collected heap� A method storage area� An instruction set

� Class file format

Registers

Registers in java are used to maintain the state of the registers.The Registers are updated after execution of each line of Byte code.

� Frame (Reference Frame): Pointer to the current method in execution.� Optop (Operand top): Pointer to the top of operand stack, used to evaluate the Athmetic.� PC (Program counter): Contains the address of next byte code to be executed.

� Var (variable register): Contains the address of next byte code to be executed.

Page 14: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 14/24

The stack

Introduction to Programming

Follow LIFO (Last in First out) methodology.

The Java Virtual Machine uses an operand stack to supply parameters to methods and operations,and to receive results back from them

� isub byte code instruction expects two integers to be stored on the top of the stack� It means that the operands must have been pushed there by the previous set of instructions.� isub pops the operands off the stack, subtracts them.� pushes the results back onto the stack.

Page 15: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 15/24

Garbage Collection heap

Introduction to Programming

Heap size is IMB,as java program runs heap size grows, in order to maintain the heap, automaticGarbage collection thread is executed to deallocate memory from unused objects.

Each program running in the Java runtime environment has a garbage-collectedheap assigned toIt and instance of class allocated memory from this heap.

Garbage Collection

� Allocated memory that is no longer needed should be deallocated� In other languages, deallocation is the programmer¶s responsibility� The Java programming language provides a systemlevel thread to track memory allocation

� Garbage collection:� Checks for and frees memory no longer needed� Is done automatically

� Can vary dramatically across JVM implementations

Page 16: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 16/24

Introduction to Programming

Class File Format

� Java source file compiled code represented using a format known as the class file format.� The class file format precisely defines the representation of a class, including details such as

byte ordering that might be taken for granted in a platform-specific object file format.

Instruction set

javap -c -s -verbose MyEnv

� Each processor understand the special set of instructions, INTEL CPU execute differentInstructions set then Motorola cup inside Apple.

� Each instruction set consist of a number which tells the CPU what to do.� C6 06 64 00 01 (ADD 1 at memory address 100).

http://www.xs4all.nl/~mpdeboer/scriptie/node17. html#SECTION02421000000000000000

Page 17: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 17/24

Introduction to Programming

Java Run Time Environment (JRE)

Offers a library for Java Virtual Machines (JVM) to execute applications and applets scripted in Java.

� Loads code� Verifies code� Executes code

Java Class Loader

� The class which is referenced inside progarame/class(static void main())which is currentlyrunning is loaded by java class loader.

� Class loader determines when and how classes can be loaded in JRE,from loacal and external.

Types of Class Loader

User define class loaderSystem Define class loader

Page 18: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 18/24

Introduction to Programming

Java Byte code Verifier

when an JAVA application imports a code fragment from anywhere, it doesn't actually know if code fragments follow Java language rules for safety: the code may not have been producedby a known-to-be trustworthy Java compiler.

Type correctness: The arguments of an instruction are always of the types expected by the instruction.istore_1 Instruction shows that variable of type integer, iadd addition of integers

Code containment: The program counter must always point within the code for the method.

Register initialization: Registers that do not correspond to method parameters are not initialized onmethod entrance, and it is an error to load from an uninitialized register.

Object initialization: when an instance of a class C  is created, one of the initialization methods for classC  (corresponding to the constructors for this class) must be invoked before the class instance can beused.

Data Conversions:N

o conversion (from integer to pointer)Stack Protection: The code causes no operand stack overflows or underflows.

Page 19: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 19/24

Java Runtime Environment

abc.java

abc.class (1) Class Loader (2) Byte Code verifier (3) Interpreter (4)Hardware

Compile

Compilation JRE

Page 20: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 20/24

Introduction to Programming

Writing Program in JAVA

� Java API� Java 2 Standard Edition(J2SE)� Java 2 Enterprise Edition (J2EE)� Java 2 Micro Edition (J2ME)NoteJDK consist of set of separate programs for developing and testing java programs invoked fromcommand line.

DrJava: (http://drjava.sourceforge.net/ )JEdit: (http://www.jedit.org/)JBuilder: (http://www.borland.com/)Eclipse: (http://www.netbeans.org/)

Editors

Page 21: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 21/24

Introduction to Programming

Javac

TestGreeting

Java TestGreeting

Page 22: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 22/24

Introduction to Programming

(1) javac: Command not found

Because of javac command is not in path variable

(2) TestGreet.java:4: Public class TestGreeting must be defined in a file called "TestGreeting.java".

Because of Classname and file name is different

(1) Can¶t find class TestGreeting Exception in thread "main³ java.lang.NoSuchMethodError: main

COMPILE TIME ERRORS

RUN TIME ERRORS

When try to run a programe without main function/method

Page 23: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 23/24

Thanks

Page 24: Introduction to Java Programming_Lecture001

8/7/2019 Introduction to Java Programming_Lecture001

http://slidepdf.com/reader/full/introduction-to-java-programminglecture001 24/24