Download - Java Technologies 1
-
8/6/2019 Java Technologies 1
1/68
Java Technologies
Prof. Nitin ZadpeMIT School of Telecom Management
1 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
2/68
What is JAVA Technology?
2 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
3/68
ELEPHANT AND THE BLIND MEN
3 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
4/68
What is Java Technology?
The Java technology is:
A programming language
A development environment
An application environment
A deployment environment
4 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
5/68
Programming Language
As a programming language, Java cancreate all kinds of applications that you
could create using any conventionalprogramming language.
5 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
6/68
A Development Environment
As a development environment, Javatechnology 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...
6 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
7/68
An Application and Runtime
Environment
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.
Java technology applications are typically general-purposeprograms that run on any machine where the Java runtimeenvironment (JRE) is installed.
7 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
8/68
Java Features
Some features of Java:
The Java Virtual Machine
Garbage Collection
Code Security
8 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
9/68
The Java Virtual Machine Java Virtual Machine (JVM)
an imaginary machine that is implemented by emulatingsoftware on a real machine
provides the hardware platform specifications to whichyou 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 thecompiled Java program, no matter what type of
computer the program was compiled on 9 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
10/68
Garbage Collection
Garbage collection thread
responsible for freeing any memory that can be
freed. This happens automatically during thelifetime of the Java program.
programmer is freed from the burden of having
to deallocate that memory themselves
10 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
11/68
Code Security
Code security is attained in Java through theimplementation of its Java Runtime Environment
(JRE). JRE
runs code compiled for a JVM and performs
class loading (through the class loader), codeverification (through the bytecode verifier) and
finally code execution
11 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
12/68
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 areimported from network sources
After loading all the classes, the memory layout of theexecutable is then determined. This adds protectionagainst unauthorized access to restricted areas of thecode since the memory layout is determined duringruntime
12 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
13/68
Code Security
Bytecode verifier
tests the format of the code fragments and
checks the code fragments for illegal code thatcan violate access rights to objects
13 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
14/68
Phases of a Java Program
The following figure describes the process ofcompiling and executing a Java program
14 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
15/68
Phases of a Java Program
15 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
16/68
If you have any doubt. You should ask..
16 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
17/68
17
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'sprograms.
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) whosegoal was to develop software for intelligent home appliances.
An intelligent home appliance's intelligence comes from itsembedded 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, andmore powerful.
To handle the frequent turnover of new chips, appliance softwaremust be extremely portable.
2
1
-
8/6/2019 Java Technologies 1
18/68
18
History of Java Originally, Sun planned to use C++ for its home appliance
software, but they soon realized that C++ was less than idealbecause it wasn't portable enough and it relied too heavily onhard-to-maintain things called pointers.
Thus, rather than write C++ software and fight C++'s inherent
deficiencies, Sun decided to develop a whole new programminglanguage to handle its home appliance software needs.
Their new language was originally named Oak (for the tree thatwas outside project leader James Gosling's window), but it wassoon changed to Java.
When the home appliance software work dried up, Java almostdied before being released.
Fortunately for Java, the World Wide Web exploded in popularityand Sun realized it could capitalize on that.
2
1
-
8/6/2019 Java Technologies 1
19/68
19
History of Java 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 interms 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 currentsuccess, 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 adesktop or a laptop, without the need of the Internet.2
1
3
-
8/6/2019 Java Technologies 1
20/68
MIT School of Telecom Management 20
History of Java
Year Development1990 Sun decided to developed special software that could be
used for electronic devices. A project called Green Projectcreated and head by James Gosling.
1991 Explored possibility of using C++, with some updates
announced a new language named Oak
1992 The team demonstrated the application of their newlanguage to control a list of home appliances using ahand held device.
1993 The World Wide Web appeared on the Internet andtransformed the text-based interface to a graphical richenvironment. The team developed Web applets (timeprograms) that could run on all types of computersconnected to the Internet.
-
8/6/2019 Java Technologies 1
21/68
MIT School of Telecom Management 21
Year Development
1994 The team developed a new Web browsed called HotJava to locate and run Applets. HotJava gained instancesuccess.
1995 Oak was renamed to Java, as it did not survive legalregistration. Many companies such as Netscape andMicrosoft announced their support for Java
1996 Java established itself it self as both 1. the language forInternet programming 2. a general purpose OO
language.1997 A class libraries, Community effort and standardization,
Enterprise Java, Clustering, etc..
History of Java
-
8/6/2019 Java Technologies 1
22/68
MIT School of Telecom Management 22
Year Development
1998 JDK 1.1 Realese
1998 Java 2 platform source code is released
2004 Java 2 Platform, Standard Edition 5 (Project Tiger) is released
2006 Java SE and ME initial components are released as opensource
2007 Sun released the source code of the Class library under GPL
2010 Oracle acquires Sun Microsystems.
History of Java
-
8/6/2019 Java Technologies 1
23/68
MIT School of Telecom Management 23
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
-
8/6/2019 Java Technologies 1
24/68
MIT School of Telecom Management 24
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
Java Features (2)
-
8/6/2019 Java Technologies 1
25/68
MIT School of Telecom Management 25
Java Features (3)
Reliable
extensive compile-time and runtime error checking
no pointers but real arrays. Memory corruptions or unauthorizedmemory 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)
-
8/6/2019 Java Technologies 1
26/68
MIT School of Telecom Management 26
Java Features (4)
Multithreaded
multiple concurrent threads of executions can run simultaneously
utilizes a sophisticated set of synchronization primitives (basedon 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 variableswithout any effect on their clients
interfaces promote flexibility and reusability in code by specifyinga set of methods an object can perform, but leaves open howthese methods should be implemented
can check the class type in runtime
-
8/6/2019 Java Technologies 1
27/68
MIT School of Telecom Management 27
Java Features (5) Robust
Exception handling built-in, strong type checking (that is, all datamust be declared an explicit type), local variables must beinitialized.
Architecture Neutral
The growing popularity of networks makes developers thinkdistributed. In the world of network it is essential that theapplications must be able to migrate easily to different computersystems. Not only to computer systems but to a wide variety ofhardware architecture and Operating system architectures aswell.
-
8/6/2019 Java Technologies 1
28/68
MIT School of Telecom Management 28
Total Platform Independence
JAVA COMPILER
JAVA BYTE CODE
JAVA INTERPRETER
Windows XP Macintosh Solaris Linux
(translator)
(same for all platforms)
(one for each different system)
-
8/6/2019 Java Technologies 1
29/68
MIT School of Telecom Management 29
Java
Syrup
Java
Capsules
Injectable
JavaJava
HairOil
Dont wait for these
products..
-
8/6/2019 Java Technologies 1
30/68
30
Quiz Questions
1. Pseudocodea) uses precise computer syntax.b) compiles into object code.c) is often used when writing an algorithm.
2. Java programs can be embedded in Web pages, and suchprograms are called
a) appletsb) applicationsc) operating systems
-
8/6/2019 Java Technologies 1
31/68
Primitive Data types
Java has 8 (or so) primitive types: float
double
booleanchar
byte
short
int
long
Also the void type
real numbers
integer numbers
two values: true and false
a single character
31 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
32/68
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
Always use doubles
It will save you quite a headache!
32 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
33/68
Primitive integer types
Consider a byte:
0 1 0 0 0 1 0 1
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
33 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
34/68
Primitive integer types
Type Bytes Minimum value Maximum value
byte 1 -27=-128 27-1=127
short 2 -215
=-32,768
215
-1=32,767
int 4 -231=-2,147,483,648 231-1=2,147,483,647
long 8 -263=-9,223,372,036,
854,775,808
263-1=9,223,372,036,
854,775,807
34 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
35/68
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
35 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
36/68
Primitive boolean type
The boolean type has only two values:
true
false
There are boolean-specific operators
&& is and || is or
! is not
36 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
37/68
Identifiers Identifiers are names for variables, classes, etc.
Good ones are compact, but inidicate what they stand for
radius, width, height, length
Bad ones are either too long
theRadiusOfTheCircle
theWidthOfTheBoxThatIsBeingUsed
the_width_of_the_box_that_is_being_used
Or too short
a, b, c, d, e
Good identifiers will help the graders understandyour program!
37 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
38/68
// 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.");
}}
// 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.");
}}
Keywords
// 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.");
}}
Some words are reserved, and cant be used
as identifiers
38 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
39/68
Capitalization
Case matters!
public Public PUBLICThis is different
You can use Public as a identifier
Not recommended, though!
39 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
40/68
Defining variables
Weve seen variables before in math y= mx+ b
Here y, m, x, and bcan hold any value
To store things in a computer program, wealso use variables
Example: int x = 5;
This defines an integer variable with value 5
The variable is x
The type is int40 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
41/68
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 thedouble type
double d = 4.3;
The variable is d
The type is double
41 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
42/68
Assignment operator =Allows the memory location for a variable to be
updated
Considerint j = 11;
j = 1985;
Primitive variable assignment
42 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
43/68
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 youuse it
43 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
44/68
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 valuebefore you use it
44 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
45/68
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
45 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
46/68
Expressions What is the value used to initialize expression
int expression = 4 + 2 * 5;
What value is displayedSystem.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
46 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
47/68
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;
47 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
48/68
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
48 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
49/68
Java operators
Booleans have their own operators && is AND
Only true when both operands are true
true && true yields true
false && true yields false || is OR
True when either of the operands (or both) are true
true || false yields true
false || false yields false ! is NOT
Changes the value
!true yields false
!false yields true49 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
50/68
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
50 MIT School of Telecom Management
-
8/6/2019 Java Technologies 1
51/68
51 MIT School of Telecom Management
Hello World!
Lets consider how to write this program.
What would be the input? Nothing
What would be the output? Hello World!
What would thealgorithm look like?
Display the textHello World!
on the screen.
-
8/6/2019 Java Technologies 1
52/68
52 MIT School of Telecom Management
Sample Java Program
Here is the complete Java code for the HelloWorld! program.
Lets look at it line by line.
-
8/6/2019 Java Technologies 1
53/68
MIT School of Telecom Management 53
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
-
8/6/2019 Java Technologies 1
54/68
MIT School of Telecom Management 54
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.
S l J P
-
8/6/2019 Java Technologies 1
55/68
MIT School of Telecom Management 55
Sample Java Program
The text beginning with//at the right side of somelines are single line comments.
They are also ignored by the Java compiler and not
executed.
S l J P
-
8/6/2019 Java Technologies 1
56/68
MIT School of Telecom Management 56
Sample Java Program
Line 7 is called the class header.
uses the Java keywords public and class
keywords are displayed in blueby most IDEs
S l J P
-
8/6/2019 Java Technologies 1
57/68
MIT School of Telecom Management 57
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
S l J P
-
8/6/2019 Java Technologies 1
58/68
MIT School of Telecom Management 58
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.
S l J P
-
8/6/2019 Java Technologies 1
59/68
MIT School of Telecom Management 59
Sample Java Program
Line 7 is called the class header.
Note: Java is case sensitive.
HelloWorld does not equal Helloworld.
S l J P
-
8/6/2019 Java Technologies 1
60/68
MIT School of Telecom Management 60
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.
S l J P
-
8/6/2019 Java Technologies 1
61/68
MIT School of Telecom Management 61
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 methodthe main method.
The main method is a required method in every
Java application (program).
S l J P
-
8/6/2019 Java Technologies 1
62/68
MIT School of Telecom Management 62
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).
S l J P
-
8/6/2019 Java Technologies 1
63/68
MIT School of Telecom Management 63
Sample Java Program
Line 9 is called the method header.
The rest of the method header will also be explained in a laterlesson.
For now, use the entire main method header as is without being
concerned about understanding it.
Sample Java Program
-
8/6/2019 Java Technologies 1
64/68
MIT School of Telecom Management 64
Sample Java Program
Lines 10 through 12 are called a method body.
Like a class body, a method body is enclosed bycurly braces.
These are indented to improve readability.
S l J P
-
8/6/2019 Java Technologies 1
65/68
MIT School of Telecom Management 65
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.
Sample Java Program
-
8/6/2019 Java Technologies 1
66/68
MIT School of Telecom Management 66
Sample Java Program
Line 11 is an executable statement.
System.out.println displays to the screen the contents thatare enclosed in parenthesis.
In this case, Hello World! will be displayed on the screen.
Sample Java Program
-
8/6/2019 Java Technologies 1
67/68
MIT School of Telecom Management 67
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.
-
8/6/2019 Java Technologies 1
68/68