You are on page 1of 68

Java Technologies

Prof. Nitin Zadpe MIT School of Telecom Management

MIT School of Telecom Management

What is JAVA Technology?

MIT School of Telecom Management

ELEPHANT AND THE BLIND MEN

MIT School of Telecom Management

What is Java Technology?


The Java technology is: A programming language A development environment An application environment A deployment environment

MIT School of Telecom Management

Programming Language

As a programming language, Java can create all kinds of applications that you could create using any conventional programming language.

MIT School of Telecom Management

A Development Environment
As a development environment, Java technology provides you with a large suite of

tools: A compiler (javac) An interpreter (java) A documentation generator (javadoc) A class file packaging tool and so on...
MIT School of Telecom Management 6

An Application and Runtime Environment


Java technology applications are typically general-purpose programs that run on any machine where the Java runtime environment (JRE) is installed.
There are two main deployment environments:
1. The JRE supplied by the Java 2 Software Development Kit (SDK) contains the complete set of class files for all the Java technology packages, which includes basic language classes, GUI component classes, and so on. 2. The other main deployment environment is on your web browser. Most commercial browsers supply a Java technology interpreter and runtime environment.
MIT School of Telecom Management 7

Java Features
Some features of Java: The Java Virtual Machine Garbage Collection Code Security

MIT School of Telecom Management

The Java Virtual Machine

Java Virtual Machine (JVM) an imaginary machine that is implemented by emulating software on a real machine provides the hardware platform specifications to which you compile all Java technology code Bytecode a special machine language that can be understood by the Java Virtual Machine (JVM) independent of any particular computer hardware, so any computer with a Java interpreter can execute the compiled Java program, no matter what type of computer the program was compiled on
MIT School of Telecom Management 9

Garbage Collection

Garbage collection thread

responsible for freeing any memory that can be


freed. This happens automatically during the lifetime of the Java program. programmer is freed from the burden of having to deallocate that memory themselves

MIT School of Telecom Management

10

Code Security

Code security is attained in Java through the implementation of its Java Runtime Environment (JRE).

JRE

runs code compiled for a JVM and performs class loading (through the class loader), code verification (through the bytecode verifier) and finally code execution
MIT School of Telecom Management 11

Code Security

Class Loader

responsible for loading all classes needed for the Java program adds security by separating the namespaces for the classes of the local file system from those that are imported from network sources After loading all the classes, the memory layout of the executable is then determined. This adds protection against unauthorized access to restricted areas of the code since the memory layout is determined during runtime
MIT School of Telecom Management 12

Code Security

Bytecode verifier

tests the format of the code fragments and checks the code fragments for illegal code that can violate access rights to objects

MIT School of Telecom Management

13

Phases of a Java Program


The following figure describes the process of compiling and executing a Java program

MIT School of Telecom Management

14

Phases of a Java Program

MIT School of Telecom Management

15

If you have any doubt. You should ask..

MIT School of Telecom Management

16

1 2

History of Java

In the early 1990's, putting intelligence into home appliances was thought to be the next "hot" technology. Examples of intelligent home appliances:

Coffee pots and lights that can be controlled by a computer's programs. Televisions that can be controlled by an interactive television device's programs.

Anticipating a strong market for such things, Sun Microsystems in 1991 funded a research project (code named Green) whose goal was to develop software for intelligent home appliances. An intelligent home appliance's intelligence comes from its embedded processor chips and the software that runs on the processor chips. Appliance processor chips change often because engineers continually find ways to make them smaller, less expensive, and more powerful. To handle the frequent turnover of new chips, appliance software must be extremely portable.
17

History of Java

Originally, Sun planned to use C++ for its home appliance software, but they soon realized that C++ was less than ideal because it wasn't portable enough and it relied too heavily on hard-to-maintain things called pointers. Thus, rather than write C++ software and fight C++'s inherent deficiencies, Sun decided to develop a whole new programming language to handle its home appliance software needs. Their new language was originally named Oak (for the tree that was outside project leader James Gosling's window), but it was soon changed to Java. When the home appliance software work dried up, Java almost died before being released. Fortunately for Java, the World Wide Web exploded in popularity and Sun realized it could capitalize on that.
18

History of Java

2 3

Web pages have to be very portable because they can be downloaded onto any type of computer. What's the standard language used for Web pages? Java programs are very portable and they're better than HTML in terms of providing user interaction capabilities. Java programs that are embedded in Web pages are called applets. Although applets still play a significant role in Java's current success, some of the other types of Java programs have surpassed applets in terms of popularity. In this course, we cover Standard Edition (SE) Java applications. They are Java programs that run on a standard computer a desktop or a laptop, without the need of the Internet.
19

History of Java
Year 1990 Development Sun decided to developed special software that could be used for electronic devices. A project called Green Project created and head by James Gosling. Explored possibility of using C++, with some updates announced a new language named Oak

1991

1992

The team demonstrated the application of their new language to control a list of home appliances using a hand held device.
The World Wide Web appeared on the Internet and transformed the text-based interface to a graphical rich environment. The team developed Web applets (time programs) that could run on all types of computers connected to the Internet.
MIT School of Telecom Management 20

1993

History of Java
Year 1994 Development The team developed a new Web browsed called Hot Java to locate and run Applets. HotJava gained instance success. Oak was renamed to Java, as it did not survive legal registration. Many companies such as Netscape and Microsoft announced their support for Java Java established itself it self as both 1. the language for Internet programming 2. a general purpose OO language. A class libraries, Community effort and standardization, Enterprise Java, Clustering, etc..

1995

1996

1997

MIT School of Telecom Management

21

History of Java
Year
1998 1998 2004 2006 2007 2010

Development JDK 1.1 Realese


Java 2 platform source code is released Java 2 Platform, Standard Edition 5 (Project Tiger) is released Java SE and ME initial components are released as open source Sun released the source code of the Class library under GPL Oracle acquires Sun Microsystems.

MIT School of Telecom Management

22

Java Features (1)

Simple

fixes some clumsy features of C++ no pointers automatic garbage collection rich pre-defined class library

Object oriented

focus on the data (objects) and methods manipulating the data all functions are associated with objects almost all datatypes are objects (files, strings, etc.) potentially better code organization and reuse

MIT School of Telecom Management

23

Java Features (2)

Interpreted

java compiler generate byte-codes, not native machine code the compiled byte-codes are platform-independent java bytecodes are translated on the fly to machine readable instructions in runtime (Java Virtual Machine)

Portable(Platform Independence )

same application runs on all platforms the sizes of the primitive data types are always the same the libraries define portable interfaces The Write-Once-Run-Anywhere

MIT School of Telecom Management

24

Java Features (3)

Reliable

extensive compile-time and runtime error checking no pointers but real arrays. Memory corruptions or unauthorized memory accesses are impossible automatic garbage collection tracks objects usage over time

Secure

usage in networked environments requires more security memory allocation model is a major defense access restrictions are forced (private, public)

MIT School of Telecom Management

25

Java Features (4)

Multithreaded

multiple concurrent threads of executions can run simultaneously utilizes a sophisticated set of synchronization primitives (based on monitors and condition variables paradigm) to achieve this

Dynamic

java is designed to adapt to evolving environment libraries can freely add new methods and instance variables without any effect on their clients interfaces promote flexibility and reusability in code by specifying a set of methods an object can perform, but leaves open how these methods should be implemented can check the class type in runtime
MIT School of Telecom Management 26

Java Features (5)

Robust

Exception handling built-in, strong type checking (that is, all data must be declared an explicit type), local variables must be initialized.

Architecture Neutral

The growing popularity of networks makes developers think distributed. In the world of network it is essential that the applications must be able to migrate easily to different computer systems. Not only to computer systems but to a wide variety of hardware architecture and Operating system architectures as well.

MIT School of Telecom Management

27

Total Platform Independence


JAVA COMPILER
(translator)

JAVA BYTE CODE


(same for all platforms)

JAVA INTERPRETER
(one for each different system)

Windows XP

Macintosh

Solaris

Linux
28

MIT School of Telecom Management

Java Syrup

Java Capsules

Injectable Java

Java HairOil

Dont wait for these products..

MIT School of Telecom Management

29

Quiz Questions
1. a) b) c) Pseudocode uses precise computer syntax. compiles into object code. is often used when writing an algorithm.

2. Java programs can be embedded in Web pages, and such programs are called a) applets b) applications c) operating systems

30

Primitive Data types

Java has 8 (or so) primitive types:


float double boolean char byte short int long
integer numbers

real numbers
two values: true and false a single character

Also the void type


MIT School of Telecom Management 31

Primitive real (floating-point) types

A float takes up 4 bytes of space


Has

6 decimal places of accuracy: 3.14159

A double takes up 8 bytes of space


Has

15 decimal places of accuracy: 3.14159265358979 will save you quite a headache!


MIT School of Telecom Management 32

Always use doubles


It

Primitive integer types

Consider a byte:

1 byte = 8 bits Each bit has two possibilities: 0 or 1 28 = 256 Thus, a byte can have any one of 256 values

A Java byte can have values from -128 to 127


From -27 to 27-1

C/C++ has unsigned versions; Java does not


MIT School of Telecom Management 33

Primitive integer types


Type byte Bytes Minimum value 1 -27=-128 -215= -32,768 -231=-2,147,483,648 Maximum value 27-1=127 215-1= 32,767 231-1=2,147,483,647

short 2

int

long

-263=-9,223,372,036, 854,775,808

263-1=9,223,372,036, 854,775,807

MIT School of Telecom Management

34

Primitive character type

All characters have a integer equivalent


0

= 48 1 = 49 A = 65 a = 97

Thus, you can refer to B as A+1

MIT School of Telecom Management

35

Primitive boolean type

The boolean type has only two values:


true false

There are boolean-specific operators


&&

is and || is or ! is not
MIT School of Telecom Management 36

Identifiers

Identifiers are names for variables, classes, etc. Good ones are compact, but inidicate what they stand for

radius, width, height, length


theRadiusOfTheCircle theWidthOfTheBoxThatIsBeingUsed the_width_of_the_box_that_is_being_used a, b, c, d, e

Bad ones are either too long


Or too short

Good identifiers will help the graders understand your program!


MIT School of Telecom Management 37

Keywords

Some words are reserved, and cant be used as identifiers

// Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window


public class DisplayForecast {

// method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); }
}
MIT School of Telecom Management 38

Capitalization

Case matters! public Public PUBLIC


This

is different

You can use Public as a identifier


Not

recommended, though!

MIT School of Telecom Management

39

Defining variables

Weve seen variables before in math


y

= mx + b Here y, m, x, and b can hold any value

To store things in a computer program, we also use variables Example:

int

x = 5; This defines an integer variable with value 5

The variable is x The type is int

MIT School of Telecom Management 40

More on variables

An integer variable can only hold integers


In

other words, it cant hold 4.3

To hold floating point values, we use the double type


double

d = 4.3;

The variable is d The type is double

MIT School of Telecom Management 41

Primitive variable assignment

Assignment operator =
Allows

the memory location for a variable to be updated

Consider
int j = 11; j = 1985;
MIT School of Telecom Management 42

Variables must be declared before use

The following code will not work:


x

= 5; System.out.println (x);

Java requires you to declare x before you use it


MIT School of Telecom Management 43

Variable initialization
Consider the following code: int x; System.out.println(x); What happens? Error message:

variable

x might not have been initialized

Java also requires you to give x a value before you use it


MIT School of Telecom Management 44

Constants

Consider the following:


final int x = 5;

The value of x can NEVER be changed!


The

value assigned to it is final

This is how Java defines constants


MIT School of Telecom Management 45

Expressions

What is the value used to initialize expression


int expression = 4 + 2 * 5;

What value is displayed


System.out.println(5 / 2.0);

Java rules in a nutshell


Each

operator has a precedence level and an associativity

Operators with higher precedence are done first

* and / have higher precedence than + and -

Associativity indicates how to handle ties

When

floating-point is used the result is floating point


MIT School of Telecom Management 46

Question on expressions

Does the following statement compute the average of double variables a, b, and c? Why or why not?
double average = a + b + c / 3.0;

MIT School of Telecom Management

47

Java operators

The following are the common operators for ints: +-/*% Division is integer division 6 / 2 yields 3 7 / 2 yields 3, not 3.5 Because everything is an int, the answer is an int Modulus is % Returns the remainder 7 % 2 yields 1 6 % 2 yields 0 Floats and doubles use the same first four operators +-/* 7.0 / 2.0 yields 3.5 7.0 / 2 yields 3.5 7 / 2.0 yields 3.5 7 / 2 yields 3
MIT School of Telecom Management 48

Java operators

Booleans have their own operators


&&

is AND

Only true when both operands are true true && true yields true false && true yields false True when either of the operands (or both) are true true || false yields true false || false yields false Changes the value !true yields false !false yields true
MIT School of Telecom Management 49

||

is OR

is NOT

Functions
In Java, functions are called methods Think of mathematical functions:

sin() cos() tan()

They take input (the angle)


And

produce output (the result)

In Java, they are called Math.sin(), Math.cos(), etc.


Meaning,

from the Math library, call the sin() method


MIT School of Telecom Management 50

Hello World!
Lets consider how to write this program. What would be the input? What would be the output? What would the algorithm look like? Nothing Hello World! Display the text Hello World! on the screen.

MIT School of Telecom Management

51

Sample Java Program

Here is the complete Java code for the Hello World! program. Lets look at it line by line.
MIT School of Telecom Management 52

Sample Java Program

Lines 1-5 are called comment lines.


shown in green by most IDEs ignored by the Java compiler not executed used for internal documentation
MIT School of Telecom Management 53

Sample Java Program

A multi-line comment is enclosed with /*...*/


Middle lines of code usually begin with an asterisks (*) to improve readability, but it is not a requirement.
MIT School of Telecom Management

54

Sample Java Program

The text beginning with // at the right side of some lines are single line comments.

They are also ignored by the Java compiler and not executed.
MIT School of Telecom Management

55

Sample Java Program

Line 7 is called the class header.

uses the Java keywords public and class keywords are displayed in blue by most IDEs
MIT School of Telecom Management 56

Sample Java Program

Line 7 is called the class header.

identifies the name of this class or program HelloWorld is the class name in this program
MIT School of Telecom Management 57

Sample Java Program

Line 7 is called the class header.

The Java filename should match the class name.


Therefore, the Java filename should be HelloWorld.java on your disk. Created by doing a File/Save As from the text editor.
MIT School of Telecom Management 58

Sample Java Program

Line 7 is called the class header.

Note: Java is case sensitive.

HelloWorld does not equal Helloworld.


MIT School of Telecom Management 59

Sample Java Program

Lines 8 through 13 are called the class body. The class body begins with a left curly brace ( { )
on the line after the class header. The class body ends with a right curly brace ( } ) on the last line of the program.
MIT School of Telecom Management 60

Sample Java Program

Lines 8 through 13 are called the class body.


A class body is divided into one or more methods. This class has only one method the main method. The main method is a required method in every Java application (program).
MIT School of Telecom Management 61

Sample Java Program

Line 9 is called the method header.


It uses the keywords public static void (meanings will be explained later in the semester). It also uses the name of the method (main).
MIT School of Telecom Management 62

Sample Java Program

Line 9 is called the method header.


The rest of the method header will also be explained in a later lesson. For now, use the entire main method header as is without being concerned about understanding it.
MIT School of Telecom Management 63

Sample Java Program

Lines 10 through 12 are called a method body. Like a class body, a method body is enclosed by
curly braces. These are indented to improve readability.
MIT School of Telecom Management 64

Sample Java Program

Line 11 is an executable statement.


Java executable statements end with a semicolon. Notice that class headers and method headers are NOT terminated with a semicolon.
MIT School of Telecom Management

65

Sample Java Program

Line 11 is an executable statement.


System.out.println displays to the screen the contents that are enclosed in parenthesis. In this case, Hello World! will be displayed on the screen.
MIT School of Telecom Management 66

Sample Java Program

Line 11 is an executable statement.


The content inside the parentheses is called a string literal.
It must be surrounded by double quotes.
MIT School of Telecom Management 67

MIT School of Telecom Management

68

You might also like