Java Unit 1
Java Unit 1
1. History of Java,
2. Java buzzwords,
3. JVM architecture
4. Data types,
5. variables,
6. scope and life time of
7. variables,
8. arrays,
9. operators,
10. control statements
11. Type conversion and casting,
12. simple java program,
Java History
L 1.1
Before Java: C
L 1.2
Before Java: C++
L 1.3
Java: History
In 1990, Sun Microsystems started a project called Green.
Objective: to develop software for consumer electronics.
Project was assigned to James Gosling, a veteran of classic
network software design. Others included Patrick Naughton,
ChrisWarth, Ed Frank, and Mike Sheridan.
The team started writing programs in C++ for embedding into
– toasters
– washing machines
– VCR’s
Aim was to make these appliances more “intelligent”.
L 1.4
Java: History (contd.)
L 1.5
Java: History (contd.)
Hence, the team built a new programming language called Oak, which
avoided potentially dangerous constructs in C++, such as pointers,
pointer arithmetic, operator overloading etc.
Introduced automatic memory management, freeing the programmer to
concentrate on other things.
Architecture neutrality (Platform independence)
Many different CPU’s are used as controllers. Hardware chips are
evolving rapidly. As better chips become available, older chips become
obsolete and their production is stopped. Manufacturers of toasters and
washing machines would like to use the chips available off the shelf, and
would not like to reinvest in compiler development every two-three
years.
So, the software and programming language had to be architecture
neutral.
L 1.6
Java: History (contd)
It was soon realized that these design goals of consumer electronics perfectly suited
an ideal programming language for the Internet and WWW, which should be:
object-oriented (& support GUI)
– robust
– architecture neutral
Internet programming presented a BIG business opportunity. Much bigger than
programming for consumer electronics.
Java was “re-targeted” for the Internet
The team was expanded to include Bill Joy (developer of Unix), Arthur van Hoff,
Jonathan Payne, Frank Yellin, Tim Lindholm etc.
In 1994, an early web browser called WebRunner was written in Oak. WebRunner
was later renamed HotJava.
In 1995, Oak was renamed Java.
A common story is that the name Java relates to the place from where the
development team got its coffee. The name Java survived the trade mark search.
L 1.7
Java History
L 1.8
The Java Buzzwords
L 1.11
Distributed: Java handles TCP/IP protocols,
accessing a resource through its URL much like
accessing a local file.
Dynamic: substantial amounts of run-time type
information to verify and resolve access to objects at
run-time.
Secure: programs are confined to the Java execution
environment and cannot access other parts of the
computer.
L 1.12
Portability: Many types of computers and operating
systems are in use throughout the world—and many
are connected to the Internet.
For programs to be dynamically downloaded to all the
various types of platforms connected to the Internet,
some means of generating portable executable code is
needed. The same mechanism that helps ensure
security also helps create portability.
Indeed, Java's solution to these two problems is both
elegant and efficient.
L 1.13
Data Types
L 1.14
byte: 8-bit integer type.
Range: -128 to 127.
Example: byte b = -15;
Usage: particularly when working with data streams.
short: 16-bit integer type.
Range: -32768 to 32767.
Example: short c = 1000;
Usage: probably the least used simple type.
L 1.15
int: 32-bit integer type.
Range: -2147483648 to 2147483647.
Example: int b = -50000;
Usage:
1) Most common integer type.
2) Typically used to control loops and to index arrays.
3) Expressions involving the byte, short and int values
are promoted to int before calculation.
L 1.16
long: 64-bit integer type.
Range: -9223372036854775808 to 9223372036854775807.
Example: long l = 10000000000000000;
Usage: 1) useful when int type is not large enough to hold the
desired value
float: 32-bit floating-point number.
Range: 1.4e-045 to 3.4e+038.
Example: float f = 1.5;
Usage:
1) fractional part is needed
2) large degree of precision is not required
L 1.17
double: 64-bit floating-point number.
Range: 4.9e-324 to 1.8e+308.
Example: double pi = 3.1416;
Usage:
1) accuracy over many iterative calculations
2) manipulation of large-valued numbers
L 1.18
char: 16-bit data type used to store characters.
Range: 0 to 65536.
Example: char c = ‘a’;
Usage:
1) Represents both ASCII and Unicode character sets;
Unicode defines a
character set with characters found in (almost) all human
languages.
2) Not the same as in C/C++ where char is 8-bit and
represents ASCII only.
L 1.19
boolean: Two-valued type of logical values.
Range: values true and false.
Example: boolean b = (1<2);
Usage:
1) returned by relational operators, such as 1<2
2) required by branching expressions such as if or
for
L 1.20
Variables
L 2.1
Variables
L 2.2
Basic Variable Declaration
L 2.3
Variable Declaration
L 2.4
Variable Scope
L 2.6
Arrays
type array-variable[];
or
type [] array-variable;
L 2.8
Array Creation
After declaration, no array actually exists.
In order to create an array, we use the new operator:
type array-variable[];
array-variable = new type[size];
This creates a new array to hold size elements of type
type, which reference will be kept in the variable
array-variable.
L 2.9
Array Indexing
L 2.10
Array Initialization
{31,28,31,30,31,30,31,31,30,31,30,31};
Note:
L 2.11
Multidimensional Arrays
L 2.12
Type Conversion
• Size Direction of Data Type
– Widening Type Conversion (Casting down)
• Smaller Data Type Larger Data Type
– Narrowing Type Conversion (Casting up)
• Larger Data Type Smaller Data Type
• Conversion done in two ways
– Implicit type conversion
• Carried out by compiler automatically
– Explicit type conversion
• Carried out by programmer using casting
L 3.5
Type Conversion
L 3.8
Type Casting
4.1