KEMBAR78
Chapter 1 | PDF | Class (Computer Programming) | Java (Programming Language)
0% found this document useful (0 votes)
29 views12 pages

Chapter 1

Java, developed by Sun Microsystems in 1991, is an Object-Oriented, Multi-threaded programming language designed for portability and security across various platforms. It allows the creation of applications and applets, with applets being small programs executed by Java-compatible web browsers, addressing security and portability through bytecode and the Java Virtual Machine (JVM). Key features of Java include simplicity, robustness, multithreading, and a rich set of APIs for different applications, making it a powerful language for both client-side and server-side development.

Uploaded by

bestmanabela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views12 pages

Chapter 1

Java, developed by Sun Microsystems in 1991, is an Object-Oriented, Multi-threaded programming language designed for portability and security across various platforms. It allows the creation of applications and applets, with applets being small programs executed by Java-compatible web browsers, addressing security and portability through bytecode and the Java Virtual Machine (JVM). Key features of Java include simplicity, robustness, multithreading, and a rich set of APIs for different applications, making it a powerful language for both client-side and server-side development.

Uploaded by

bestmanabela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 1

Introduction to Java
1.1 Creation of Java
Java is related to C++, which is a direct descendent of C. Much of the character of Java is
inherited from these two languages. Java was conceived by James Gosling, Patrick Naughton,
Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months
to develop the first working version. This language was initially called “Oak” but was renamed
“Java”in 1995.
Somewhat surprisingly, the original impetus for Java was not the Internet! Instead, the primary
motivation was the need for a platform-independent language that could be used to create
software to be embedded in various consumer electronic devices, such as microwave ovens and
remote controls.
Java is an Object-Oriented, Multi-threaded programming language. It is designed to be small,
simple and portable across both platforms as well as operating systems, at the source and binary
level.
The popularity of Java is due to its radically unique technology that is designed on a combination
of three key elements. They are the usage of applets, powerful programming language constructs
and a rich set of significant object classes.
1.2 Why Java Is Important to the Internet
Java has had a profound effect on the Internet. The reason for this is quite simple: Java expands
the universe of objects that can move about freely in cyberspace.
As desirable as dynamic, networked programs are, they also present serious problems in the
areas of security and portability.
1.3 Java Applets and Applications

Java can be used to create two types of programs: applications and applets. An application is a
program that runs on your computer, under the operating system of that computer. When used to
create applications, Java is not much different from any other computer language. Rather, it is
Java’s ability to create applets that makes it important.
An applet is an application designed to be transmitted over the Internet and executed by a Java-
compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded
across the network, just like an image, sound file, or video clip.
Page 1 of 12
As exciting as applets are, they would be nothing more than wishful thinking if Java were not
able to address the two fundamental problems associated with them: security and portability.
When you use a Java-compatible Web browser, you can safely download Java applets without
fear of viral infection or malicious intent. Java achieves this protection by confining a Java
program to the Java execution environment and not allowing it access to other parts of the
computer (security).
1.4 The Byte code
The key that allows Java to solve both the security and the portability problems just described is
that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is a
highly optimized set of instructions designed to be executed by the Java run-time system, which
is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter
for bytecode. This may come as a bit of a surprise. As you know, C++ is compiled to executable
code.
Translating a Java program into bytecode makes it much easier to run a program in a wide
variety of environments. The reason is straightforward: only the JVM needs to be implemented
for each platform. Once the run-time package exists for a given system, any Java program can
run on it.
The fact that a Java program is interpreted also helps to make it secure. Because the execution of
every Java program is under the control of the JVM, the JVM can contain the program and
prevent it from generating side effects outside of the system.
1.5 Features of Java
The fundamental forces that necessitated the invention of Java are portability and security, other
factors also played an important role in molding the final form of the languages. They are,
 Simple  Interpreted
 Security  High performance
 Portability  Distributed
 Object-Oriented  Dynamic
 Robust
 Multithreaded
 Architecture-neutral
Simple

Page 2 of 12
Java was designed to be easy for the professional programmer to learn and use effectively.
Assuming that you have some programming experience, you will not find Java hard to master.
Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most
programmers have little trouble learning Java.
Security
Prior to Java, most users did not download executable programs frequently, and those who did
scan them for viruses prior to execution. Even so, most users still worried about the possibility of
infecting their systems with a virus. In addition to viruses, another type of malicious program
exists that must be guarded against. This type of program can gather private information, such as
credit card numbers, bank account balances, and passwords, by searching the contents of your
computer’s local file system. Java answers both of these concerns by providing a “firewall”
between networked application and your computer.
Java achieves this protection by confining a Java program to the Java execution environment and
not allowing it access to other parts of the computer.
Portability
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. As you will soon see
the same mechanism that helps ensure security also helps create portability. Indeed, Java’s
solution to these two problems is both elegant and efficient.
Object-Oriented
Although influenced by its predecessors, Java was not designed to be source-code compatible
with any other language. This allowed the Java team the freedom to design with a blank slate.
One outcome of this was a clean, usable, pragmatic approach to objects.
The object model in Java is simple and easy to extend, while simple types, such as integers, are
kept as high-performance non objects.
Robust
The ability to create robust programs was given a high priority in the design of Java. To gain
reliability, Java restricts you in a few key areas, to force you to find your mistakes early in
program development. At the same time, Java frees you from having to worry about many of the
most common causes of programming errors. Because Java is a strictly typed language, it checks
your code at compile-time. However, it also checks your code at run time.

Page 3 of 12
To better understand how Java is robust, consider two of the main reasons for program failure:
memory management mistakes and mishandled exceptional conditions (that is run-time errors).
Multithreaded
Java was designed to meet the real-world requirement of creating interactive networked
programs. To accomplish this, Java supports multithreaded programming, which allows you to
write programs that do many things simultaneously.
Architecture - Neutral
A central issue for the Java designers was that of code longevity and portability. One of the main
problems facing programmers is that no guarantee exists that if you write a program today, it will
run tomorrow-even on the same machine. Operating system upgrades, processors upgrades, and
changes in core system resources can all combine to make a program malfunction.Their goal was
“write once; run anywhere, any time, forever.” To a great extent, this goal was accomplished.
Interpreted and High Performance
As described earlier, Java enables the creation of cross-platform programs by compiling into an
intermediate representation called Java bytecode. This code can be interpreted on any system that
provides a Java Virtual Machine. Most previous attempts at cross-platform solutions have done
so at the expense of performance. The Java bytecode carefully designed so that it would be easy
to translate directly into native machine code for very high performance by using a just-in-time
compiler.
Distributed
Java is designed for the distributed environment of the Internet, because it handles TCP/IP
protocols. Java has recently revived these interfaces in a package called Remote Method
Invocation (RMI). This feature brings an unparalleled level of abstraction to client/server
programming.
Dynamic
Java program carry with them substantial amounts of run-time type information that is used to
verify and resolve accesses to objects at run time. This makes it possible to dynamically link
code in a safe and expedient manner. This is crucial to the robustness of the applet environment,
in which small fragments of bytecode may be dynamically updated on a running system.
1.6 Object Oriented programming Concepts

Page 4 of 12
The popularity of OOP is the direct result of the power and convenience of using simple, easy to
maintain reusable program modules known as objects. Writing a program in Object Oriented
Programming languages is basically thinking in terms of Objects rather than functions or
procedures. In procedural oriented languages you instruct the machine, in order that it performs
its functions.
Class
It is simply a structure that combines the objects with some attributes (data structure) and some
behavior (operations). The class definition includes all the features that define an object of the
class. Thus a class binds together the features of every object of the class while allowing each
object to differ from any other in data content. Each object is said to be an instance of its class
and each instance of the class has its own value for each attribute but shares the attribute name
and operates the other instance of the class.
Encapsulation
It is a technical name for information hiding. Instead of organizing programs into procedures that
share global data, the data is packaged with the procedures that access that data. This concept is
often called data abstraction or modular programming.
The goal here is to separate the use of the object from its implementation. The user is no longer
aware of how the object is implemented (using an array or linked list). Users can operate on an
object using those messages (function call) that the implementation provides.
Encapsulation provides numerous advantages. It provides protection to data which is packaged
with the procedures. If data is declared PRIVATE then it will accessible within that package or
class only.
Inheritance
It is a major concept in any object oriented programming approach. Every object oriented
programming language provides inheritance in one from or another. Inheritance is basically
reusing code from one class into another related class. It is sharing of common characteristics
from one class into other related classes. Inheritance is the process by which one object acquires
the properties of another object.
Polymorphism
Polymorphism (from the Greek, meaning “many forms”) is a feature that allows one interface to
be used for a general class of actions.

Page 5 of 12
More generally, the concept of polymorphism is often expressed by the phrase “one interface,
multiple methods.” This means that it is possible to design a generic interface to a group of
related activities. This helps reduce complexity by allowing the same interface to be used to
specify a general class of action.
When properly applied, polymorphism, encapsulation, and inheritance combine to produce a
programming environment that supports the development of far more robust and scalable
programs than does the process-oriented model. Polymorphism allows you to create clean,
sensible, readable, and resilient code.
1.7 The Java Language Specification, API, JDK, and IDE
The Java language specification is a technical definition of the language that includes the syntax
and semantics of the Java programming language.
API: - The application program interface (API) contains predefined classes and interfaces for
developing Java programs. The Java language specification is stable, but the API is still
expanding.
Java SE, EE, and ME:-
 Java is a full-fledged and powerful language that can be used in many ways.
 It comes in three editions: Java Standard Edition(Java SE),Java Enterprise
Edition(Java EE), and Java Micro Edition(Java ME).
 Java SE can be used to develop client-side standalone applications or applets.
 Java EE can be used to develop server-side applications, such as Java servlets and
JavaServer Pages.
 Java ME can be used to develop applications for mobile devices, such as cell
phones.
JDK: stands for Java Development Toolkit, which consists of a set of separate programs, each
invoked from a command line, for developing and testing Java programs.
Java IDE: is an integrated development environment, (e.g., NetBeans, Eclipse, and TextPad),
used for rapidly developing Java programs. Editing, compiling, building, debugging, and online
help are integrated in one graphical user interface.

Page 6 of 12
1.8 Developing a First Simple Java Program
The Sun’s Java Development Kit (JDK) provides all features to write programs.
The Java source file is created in a plain text editor and can also be created in an editor that saves
files in plain ASCII without any formatting characters. A Java program has two main parts - a
class definition that encloses the entire program and a method called main that contains the body.
Once the program is typed in, it is saved as file with an extension of.java. Usually, Java files
have the same name as their class definition. This file is then compiled using javac. The javac
program should be in the execution path for compilation of the Java code. Once completed, a file
by the same name, but with the class extension is created. This is the Java bytecode file. The
bytecode is then run using the Java Interpreter. In the JDK, the Java interpreter is called java. To
execute this, java followed by the file name without the .class extension is typed at the DOS
prompt. You should also make sure that the capitalization of the filename matches the class
name. The reason for this is that Java is case-sensitive.
The following example depicts the writing of a Java program. Compile and execute the program.
Listing 1.1 Open a new file in the editor and type the following script.
class First
{
public static void main( String args[] )
{
System.out.println(“This is a simple Java program”);
} }
 Save file as First.java.( the file must be saved in bin i.e program file\java\jdk\bin)
 Compile by typing javac First.java on the command line.
 On successful compilation execute the program by typing java First , on the command line
 The program displays on the screen.

Page 7 of 12
Class declaration is done in First line. In Java, all code must reside inside a class. The entire class
definition, including all of its members, will be between the opening curly brace ({) and the
closing curly brace (}).
Every Java application program has a main() method. All Java applications begin execution by
calling main( ). The public keyword is an access specifier, which allows the programmer to
control the visibility of class members. When a class member is preceded by public, then that
member may be accessed by code outside the class in which it is declared. In this case, main( )
must be declared as public, since it must be called by code outside of its class when the program
is started. The keyword static allows main( ) to be called without having to instantiate a
particular instance of the class. This is necessary since main( ) is called by the Java interpreter
before any objects are made.
The keyword void simply tells the compiler that main( ) does not return a value. As stated,
main( ) is the method called when a Java application begins. It is important to understand that
the Java compiler will compile classes that do not contain a main ( ) method.
Any information that you need to pass to a method is received by variables specified within the
set of parentheses that follow the name of the method. These variables are called parameters. If
there are no parameters required for a given method, you still need to include the empty
parentheses. String[ ] args declares a parameter named args, which is an array of instances of
the class String. All of the code that comprises a method will occur between the method’s
opening curly brace and its closing curly brace. System is a predefined class that provides access
to the system, and out is the output stream that is connected to the console. Output is actually
accomplished by the built-in println( ) method. In this case, println( ) displays the string which
is passed to it.
Notice that the println( ) statement ends with a semicolon. All statements in Java end with a
semicolon. The reason that the other lines in the program do not end in a semicolon is that they
are not, technically, statements.
1.9 Lexical Issues
Java programs are a collection of whitespace, identifier, comments, literals , operators and
keywords.
Whitespace

Page 8 of 12
Java is a free-form language. This means that you do not need to follow any special indentation
rules. For example, the Example program could have been written all on one line or in any other
strange way you felt like typing it, as long as there was at least one whitespace character between
each token that was not already delineated by an operator or separator. In Java, whitespace is a
space, tab, or newline.

Identifiers

Identifiers are used for class names, method names and variable names. An identifier may be
any descriptive sequence of uppercase and lowercase letter, numbers or the underscore and
doller-sign characters. They must not begin with a number. Again, Java is case-sensitive, so
VALUE is a different identifier than value. Some examples of valid identifiers are : AvgTemp
count a4 $test this _is_ok

Invalid variable names include : 2count high-temp Not/ok


Literals
A constant value in Java is created by using a literal representation of it. For example, here are
some literals:
100 98.6 ‘X’ “This is a test”
Left to right, the first literal specifies an integer, the next is a floating-point value, the third is a
character constant, and the last is a string. A literal can be used anywhere a value of its type is
allowed.

Comments
There are three types of comments defined by Java. The two: single-line and multiline (// and
/* */). The third type is called a documentation comment. This type of comment is used to
produce an HTML file that documents your program. The documentation comment begins with a
/** and ends with a */.
Separators
In Java, there are a few characters that are used as separators. The most commonly used
separator in Java is the semicolon. It is used to terminate statements. The separators are shown in
the following:
Symbol Name Purpose
() Parentheses Used to contain lists of parameters in method definition and

Page 9 of 12
invocation. Also used for defining precedence in expressions,
containing expressions in control statements, and surrounding cast types.
{} Braces Used to contain the values of automatically
initialized arrays. Also used to define a block of
code, for classes, methods, and local scopes.
[] Brackets Used to declare array types. Also used when
dereferencing array values.
; Semicolon Terminates statements.
, Comma Separates consecutive identifiers in a variable declaration. Also
used to chain statements together inside a for statement.
. Period Used to separate package names from sub packages and classes.
Also used to separate a variable or method from a reference variable.
The Java Keywords
There are 49 reserved keywords currently defined in the Java language.
These keywords, combined with the syntax of the operators and separators, form the definition of
the Java language. These keywords cannot be used as names for a variable, class, or method.
Abstract continue goto package synchronized
Assert default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
1.10 Operators
Operators are special symbols used in expressions. They include arithmetic operators,
assignment operators, increment and decrement operators, logical operators and comparison
operators.

Page 10 of 12
Arithmetic Operators: Java has five basic arithmetic operators. Each of these operators takes
two operands. One on either side of the operator. The list of arithmetic operators is given below:
Operator Meaning Example
+ Addition 8+10
- Subtraction 10-8
* Multiplication 20*84
/ Division 10/5
% Modulus 10 % 6
Modulus gives the remainder once the division has been completed.
Assignment Operators
The assignment operators used in C and C++ are also used in Java. A selection of assignment
operators is given
Expression Meaning
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
x/=y x=x/y
x%=y x=x%
Incrementing and Decrementing
To increment or decrement a value by one, the ++ operator and -- operator are used respectively.
The increment and the decrement operators can be prefixed or postfixed. The different increment
and decrement operators can be used as given below:
 ++a (Pre increment operator): Increment by 1, then use the new value of a in the expression in
which a resides.
 A++( Post Increment Operator) : Use the current value of a in the expression in which a
resides and then increment a bt 1.
 --B (Pre decrement operator ) : Decrement b by 1 , then use the new value of b in the
expression in which b resides.
 B--(Post decrement operator ) : Use the current value of b in the expression in which b resides
and then decrement b by 1.

Page 11 of 12
Relational operators: The relational operators determine the relationship that one operand has
to the other. Specifically, they determine equality and ordering. The relational operators are
shown here:
Operators Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Page 12 of 12

You might also like