CP1344: PROGRAMMING IN JAVA
Module I: A simple Java Application, a simple Java Applet , Brief History of Java,
Special Features of Java, Data Type & Operators in Java, Arrays, Objects, the
Assignment Statement, Arithmetic Operators, Relational and Logical Operators in Java,
control Structures, The Java Class, Constructor, Finalizers, Classes inside classes:
composition
OVERVIEW OF JAVA
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
1994 as the Web emerged.
HotJava
HotJava is a Web browser that is written in Java. HotJava is a Java-enabled browser.
This means that HotJava can execute Java applets contained on Web pages. In order to
accomplish this, HotJava calls the Java runtime system. The Netscape 2.0 browser, like
HotJava, is also Java enabled. It contains a copy of the Java runtime system embedded
within it.
Basic features
The Java team has summed up the basic features of Java with the following list of
buzzwords :
• Simple
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. If you already understand the basic concepts of object-oriented
programming, learning Java will be even easier. Best of all, if you are an experienced
C++ programmer, moving to Java will require very little effort. Because Java inherits the
C/C++ syntax and many of the object-oriented features of C++, most programmers have
little trouble learning Java.
• Object-oriented
Java is a true object-oriented language. Many of Java’s object-oriented concepts are
inherited from C++, the language on which it is based, but it borrows many concepts
from other object-oriented languages as well. Like most object-oriented programming
languages, Java includes a set of class libraries that provide basic data types, system
input and output capabilities, and other utility functions. These basic classes are part of
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 1
,CP1344: PROGRAMMING IN JAVA
the Java development kit, which also has classes to support networking, common
Internet protocols, and user interface toolkit functions.
• Robust
Java is a robust language. The multiplatform environment of the Web places
extraordinary demands on a program, because the program must execute reliably in a
variety of systems. Thus, 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.
• Secure
Prior to Java, most users did not download executable programs frequently from
Internet, and those who did scanned 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 a networked application and your computer. 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.
• 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. The Java run-time system
comes with an elegant yet sophisticated solution for multiprocessor synchronization that
enables you to construct smoothly running interactive systems.
• 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
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 2
,CP1344: PROGRAMMING IN JAVA
upgrades, processor upgrades, and changes in core system resources can all combine to
make a program malfunction. The Java designers made several hard decisions in the
Java language and the Java Virtual Machine in an attempt to alter this situation. Their
goal was “write once; run anywhere, any time, forever.” To a great extent, this goal was
accomplished.
• Portable
In addition to being architecture-neutral, Java code is also portable. It was an important
design goal of Java that it be portable so that as new architectures (due to hardware,
operating system, or both) are Java and the runtime environment is written in POSIX-
compliant C.
• Distributed
Java is designed for the distributed environment of the Internet, because it handles
TCP/IP protocols. In fact, accessing a resource using a URL is not much different from
accessing a file. The original version of Java (Oak) included features for intra-address
space messaging. This allowed objects on two different computers to execute procedures
remotely. 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 programs 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.
The Java development environment has two parts: a Java compiler and a Java
interpreter. In the Java programming language, all source code is first written in plain
text files ending with the .java extension. Those source files are then compiled into
.class files by the javac compiler. With the compiler, first you translate a program into an
inter-mediate code called Java bytecodes. Bytecodes are not machine instructions and
therefore, in the second stage, Java interpreter generates machine code that can be
directly executed by the machine that is running the Java program. The interpreter parses
and runs each Java bytecode instruction on the computer. Compilation happens just
once; interpretation occurs each time the program is executed.
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 3
, CP1344: PROGRAMMING IN JAVA
Java Virtual Machine (JVM) Java compiler produces an intermediate code known as
bytecode for a machine that does not exist. This machine is called the JVM and it exists
only inside the computer memory. The bytecodes are also known as virtual machine
code which are not the actual machine code. The actual machine codes are generated by
the Java interpreter only.
JAVA ENVIRONMENT
Java environment includes a large number of development tools and hundreds of classes
and methods. The development tools are part of the system known as Java development
Kit (JDK) and the classes and methods are part of the Java Standard Library (JSL), also
known as the Application Programming Interface (API). Java development Kit the Java
development Kit comes with a collection of tools that are used for development and
running Java programs. Some of them are :
java The loader for Java applications. This tool is an interpreter and can interpret the
class files generated by the javac compiler.
javac The compiler, which converts source code into Java bytecode
jar The archiver, which packages related class libraries into a single JAR file.
javadoc The documentation generator, which automatically generates documentation
from source code comments
jdb The Java debugger
jps The process status tool, which displays process information for current Java
processes
javap The class file disassembler
appletviewer This tool can be used to run and debug Java applets without a web
browser.
javah The C header and stub generator, used to write native methods
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 4
Module I: A simple Java Application, a simple Java Applet , Brief History of Java,
Special Features of Java, Data Type & Operators in Java, Arrays, Objects, the
Assignment Statement, Arithmetic Operators, Relational and Logical Operators in Java,
control Structures, The Java Class, Constructor, Finalizers, Classes inside classes:
composition
OVERVIEW OF JAVA
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
1994 as the Web emerged.
HotJava
HotJava is a Web browser that is written in Java. HotJava is a Java-enabled browser.
This means that HotJava can execute Java applets contained on Web pages. In order to
accomplish this, HotJava calls the Java runtime system. The Netscape 2.0 browser, like
HotJava, is also Java enabled. It contains a copy of the Java runtime system embedded
within it.
Basic features
The Java team has summed up the basic features of Java with the following list of
buzzwords :
• Simple
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. If you already understand the basic concepts of object-oriented
programming, learning Java will be even easier. Best of all, if you are an experienced
C++ programmer, moving to Java will require very little effort. Because Java inherits the
C/C++ syntax and many of the object-oriented features of C++, most programmers have
little trouble learning Java.
• Object-oriented
Java is a true object-oriented language. Many of Java’s object-oriented concepts are
inherited from C++, the language on which it is based, but it borrows many concepts
from other object-oriented languages as well. Like most object-oriented programming
languages, Java includes a set of class libraries that provide basic data types, system
input and output capabilities, and other utility functions. These basic classes are part of
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 1
,CP1344: PROGRAMMING IN JAVA
the Java development kit, which also has classes to support networking, common
Internet protocols, and user interface toolkit functions.
• Robust
Java is a robust language. The multiplatform environment of the Web places
extraordinary demands on a program, because the program must execute reliably in a
variety of systems. Thus, 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.
• Secure
Prior to Java, most users did not download executable programs frequently from
Internet, and those who did scanned 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 a networked application and your computer. 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.
• 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. The Java run-time system
comes with an elegant yet sophisticated solution for multiprocessor synchronization that
enables you to construct smoothly running interactive systems.
• 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
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 2
,CP1344: PROGRAMMING IN JAVA
upgrades, processor upgrades, and changes in core system resources can all combine to
make a program malfunction. The Java designers made several hard decisions in the
Java language and the Java Virtual Machine in an attempt to alter this situation. Their
goal was “write once; run anywhere, any time, forever.” To a great extent, this goal was
accomplished.
• Portable
In addition to being architecture-neutral, Java code is also portable. It was an important
design goal of Java that it be portable so that as new architectures (due to hardware,
operating system, or both) are Java and the runtime environment is written in POSIX-
compliant C.
• Distributed
Java is designed for the distributed environment of the Internet, because it handles
TCP/IP protocols. In fact, accessing a resource using a URL is not much different from
accessing a file. The original version of Java (Oak) included features for intra-address
space messaging. This allowed objects on two different computers to execute procedures
remotely. 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 programs 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.
The Java development environment has two parts: a Java compiler and a Java
interpreter. In the Java programming language, all source code is first written in plain
text files ending with the .java extension. Those source files are then compiled into
.class files by the javac compiler. With the compiler, first you translate a program into an
inter-mediate code called Java bytecodes. Bytecodes are not machine instructions and
therefore, in the second stage, Java interpreter generates machine code that can be
directly executed by the machine that is running the Java program. The interpreter parses
and runs each Java bytecode instruction on the computer. Compilation happens just
once; interpretation occurs each time the program is executed.
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 3
, CP1344: PROGRAMMING IN JAVA
Java Virtual Machine (JVM) Java compiler produces an intermediate code known as
bytecode for a machine that does not exist. This machine is called the JVM and it exists
only inside the computer memory. The bytecodes are also known as virtual machine
code which are not the actual machine code. The actual machine codes are generated by
the Java interpreter only.
JAVA ENVIRONMENT
Java environment includes a large number of development tools and hundreds of classes
and methods. The development tools are part of the system known as Java development
Kit (JDK) and the classes and methods are part of the Java Standard Library (JSL), also
known as the Application Programming Interface (API). Java development Kit the Java
development Kit comes with a collection of tools that are used for development and
running Java programs. Some of them are :
java The loader for Java applications. This tool is an interpreter and can interpret the
class files generated by the javac compiler.
javac The compiler, which converts source code into Java bytecode
jar The archiver, which packages related class libraries into a single JAR file.
javadoc The documentation generator, which automatically generates documentation
from source code comments
jdb The Java debugger
jps The process status tool, which displays process information for current Java
processes
javap The class file disassembler
appletviewer This tool can be used to run and debug Java applets without a web
browser.
javah The C header and stub generator, used to write native methods
Bachelor in Computer Applications, UNIVERSITY OF KERALA Page 4