College of Arts and Sciences

Department of Mathematics and Physical Sciences

Computer Science Section

FALL 2012

COMP 301

Introduction to Java Programming

(2 Hours Lectures and 2 Hours lab)

CHAPTER 1 & 2

Contents:

*Basics

*Introduction to Java

*Introduction to JVM,JDK and Applets.

*Features of java

*Mechanism of Java programs

Basics:

Memory is to store data and program instructions for CPU to execute. A memory unit is an ordered sequence of bytes, each holds eight bits. A program and its data must be brought to memory before they can be executed. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.

How Data is Stored?

Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of next adjacent bytes. 1BYTE= 8 BITS.A byte is the minimum storage unit. 1 BIT= 0 or 1.

Programming Languages

Programming languages are of 3 types as follows:

1)Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code(0 AND 1), so you have to enter binary codes for various instructions. Programming in machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this:

1101101010011010

It is not possible to remember such instruction made of 0 and 1’s and very difficult write a program in 0’s and 1’s.

2)Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this:

ADDF3 R1, R2, R3

3)High-level Languages:

The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5:

area = 5 * 5 * 3.1415;

eg: Java,c++,c#,Java script,BASIC,Visual Basic.

Introduction to Java:

Who Created Java?

In the early nineties, Java was created by James Gosling and his team, for Sun Microsystems company. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity(GUI) with users by giving developers a way to produce animated webpages.

Why Java?

The answer is that Java enables users to develop and deploy(use) applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Java is the Internet programming language.

1)Java is a general purpose programming language.

2)Java is the Internet programming language.

3)Java can be used to develop Web applications.

4)Java Applet (applications using 3D images, videos, sounds)

What is Applet?

An applet is a small Internet-based program written in Java. The applet program is usually written in an HTML language and can be executed in a browser like Internet Explorer.
Applets are used to provide interactive features to web applications that cannot be provided by HTML alone. They can capture mouse input (like rotating 3D object) and also have controls like buttons or check boxes.

Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic (interactive) and entertaining.

5)Java can also be used to develop applications for hand-held devices such as Palm and cell phones.

6)Java allows you to play online games.

7)chat with people around the world and many more..

8) Helps develop applications that are rich in GUI(Graphical user interface).

What is interface?

The point of interaction or communication between a computer and any person, program/application, or device is called interface. Interface is a bridge for communication between computers ,programs and user.

What is GUI?

GUI Stands for Graphical User Interface and refers to the display of software on a screen using graphics, symbols, and icons instead using of text alone. GUI is a way of interacting with a computer using items such as windows, icons, and menus, used by most modern operating systems.

A GUI useswindows,icons, andmenusto carry out commands such as opening files, deleting files, moving.files,etc.
GUI presents information in a user-friendly way using pictures and icons. GUI makes our interaction with computer easy with use of images, graphics, icons, menu’s and scroll bars.

API introduction:

An application-programming interface (API) is a set of programming instructions and standards (Rules) for accessing (using or communicating with) a Web-based software application or Web tool. A software company releases its API to the public so that other software developers can design (develop) products (programs/applications) that are powered by its service. With APIs, applications talk to each other without any user knowledge or intervention

What is API in java?

An API stands for a "Java Application Programming Interface". It is like a super dictionary of the Java language, that one uses to look something up in. It has an index or collection of all Java packages, classes and interfaces, with all of their methods, fields and constructors, and how to use them.

The API is a collection of interfaces telling us what each class can do, not telling us how it is done. The API contains many packages for many different purposes, among them, the applet package for creating applets, the awt and swing packages for graphics and GUI, the io package for input and output, the sql package for Java databasing, and many many more.

The API is also called as Java Standard Library

The library consists of hundreds of classes and methods grouped into several functional packages.

Most commonly used packages are:

v Language support package: A collection of classes and methods required for using basic features of java.

v Utilities package: A collection of classes to provide utility functions such as date and time functions.

v Input/output packages:A collection of classes required for input/output manipulation(modification/changing/ alteration).

v Networking packages: A collection of classes for communicating with computers via internet.

v AWT package:The Abstract window tool kit package is used for add GUI to our java programs.

v Applet package: Helps in creation of applets.

A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together to make a successful program.

Example of Java API’s:

java.applet

java.math

java.io

java.awt

java.awt.font.

Just import them in your program to make use of Applets,mathematical functions,input/output operations, Graphics and font modification functions.

Java Development kit(JDK):

The first thing which we get when we download java software is JDK.

JDK is a free software development package from Sun Microsystems that contains the basic set of tools needed to write, test and debug Java applications and applets. It also contains JRE(Java runtime environment) which is responsible for making the Java programs run.JDK has the following:

1)  Appletviewer(for viewing java Applets):

Enables us to run Java Applets (without using a java compatible browser)

2)  Javac(java compiler):

Translates java source code to bytecode files that interpreter can understand.

3)  Java( java interpreter):

It interprets the bytecode line by line and converts the entire bytecode to machine code.

4)  Javadoc( for creating HTML documents)

Creates HTML-format documentation from our java source code files.

5)  jdb( java debugger):

Java Debugger helps us to find errors in our programs.

(Note: Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc

1)  Editor– To type your program into , a notepad could be used for this

Compiler translates source code into byte code, it takes whole code and executes at once.

Interpreter executes byte code line by line, and it converts byte code into Machine code.

Compiler is faster compared to Interpreter)

Java virtual Machine(JVM):

v The JRE consists of the Java Virtual Machine (JVM).

v JVM is the heart of Java runtime environment.(JRE)

v JVM consists of both compiler(JIT) and interpreter, that’s why java is called a compiled and interpreted language.

v JVM is responsible for the entire translation of bytecode into machine code.

v JVM consist of a special compiler called Just-in-time Compiler(JIT)

v JIT converts only a specific part of the bytecode (not the entire block of bytecode as in interpreter) to machine code, as and when needed.

v Difference between Java Interpreter and JIT:

Java interpreter take the entire block of bytecode and reads the entire bytecode line by line and converts the entire block to machine code .

Whereas, JIT reads only a part of the bytecode from the entire bytecode block and converts only that particular part of bytecode which is needed by the user/program ( at that particular time) to Machine code.

JIT is faster and occupies less memory when compared to Java Interpreter.

Features of Java:

Java was designed with a few key principles in mind:

1)Easy to Use:The fundamentals of Java came from a programming language called c++. Although c++ is a powerful language, it is too complex in its syntax, Java is built on improved ideas of c++, to make programming powerful and simple to use.

Difficult concepts of c++ such as Pointers, Structures, Unions and operator overloading and multiple inheritance have been excluded (removed) in java.Thus making Java more easy programming language.

2)Reliability:Since java uses object oriented concepts (polymorphism, encapsulation and inheritance) and exception handling, the errors made by the programmers are reduced. Java enables strict data types checking at compile and runtime, therefore making the java application more reliable (dependable/trustful) and effective (robust).

3) Secure:Java has a good built in security mechanism that it is impossible for anyone to attack java systems across internet. So it is not easy for a hacker to break, change or cause any damage to java applications on the internet. Java is probably the most secure programming language to date.

4) Platform Independent:The Java program written for one machine (using a particular Operating system such as Windows XP), can be executed on different machines( using different operating systems such a UNIX, Mac, Windows 7 etc) Java was written to be a portable language that doesn't care about the operating system or the hardware of the computer.

Eg: java program written on one machine using UNIX platform can be executed on a different machine using entirely different platform, such as Windows.

5)Java is Distributed: Java is designed in such a way that remote(far) objects/resources(computers) can access (communicate) each other by sharing data and programs on internet. Java supports a lot of network communication protocols, such as FTP,HTTP and SMTP etc.Therefore java is ideal language for distributed application.

A distributed application consist of several parts of a program (which are remotely located) communicating with each other, which cooperate to carry out a common

task.

For example: client-server application.

.

6)Compiled and Interpreted: A language can be compiled or interpreted. *Compiler translates source code into byte code, it takes whole code and executes at once. Most of the languages are only compiled languages where the source code is directly converted to machine code by the compiler. But in java, source code is converted to bytecode.(Bytecode is a translated form of source code which can be understood(read) by almost all the machines.)

*Interpreter translates bytecode into Machine code. Interpreter executes byte code line by line, and it converts byte code into Machine code.

Compiler is faster compared to Interpreter

Source code bytecode Machine code

7)Dynamic Binding:

Dynamic binding refers to linking a procedure call(requested question) to the code(requested answer) to be executed in response to the call.

For example: lets consider that a program has different functions/classes (eg:Add, subtract, multiply and divide). When the user places a request to only add numbers ,then only the add function/class will be executed in response to the add request. The entire program code and other functions/classes are not executed.(Others classes/fuctions of the same program such as subtract, divide and multiply are not binded/executed)

But in the case of C language which does not support the dynamic binding concept, loads the entire code to execute no matter the particular piece of code is needed for the execution or not.

Mechanism of a Java program:-

The term mechanism here explains how a java program works. Mechanism of a java program refers to how a program is created, compiled, checked for errors and run(executed) successfully.