Java Notes 1
Java Notes 1
• Introduction
• Java Programming Paradigms
o Object-Oriented Programming
o Platform Independent Programming Environment
o Multithreading
o Security in Java
• Advantages of Java
o Easy Development of Large Applications
o Simple, Portable, and Robust Programming
o High Performance of Java
o Internet Access Capability
o Distributed Computing with Java
• Tools Avaialbale for Java Programming
o Java Developer�s Kit (JDK)
o Java Packages
o Java -Enabled Web Browser
o Other Third-party Tools
• Assignment
• Q&A
Introduction
IT industries goes on such a diversification that recent programming style should not confide
within a single PC rather it spreads from a single user environment to main frame, to network, to
network of networks . Or in other words, a giant program can be developed using mini programs
which were developed by other geographically distributed programmers and this is also just on
line. Now- a- days, such a distributed programming language and environment is no more an
intellectual gossip but a reality and it is Java the Internet programming language which makes
the whole world accessible from any point on it..
Java, the web-savvy programming language is developed by Sun and the Internet with the intent
to create a dynamic, object oriented programming language suitable for use of the same types of
development tasks as C and C++, but without the difficulties and sources of bugs common to
those languages. This is why Java has been touted as � a better C++�; it contains all the
essential OOP features but with reduced complexities than in C++. Sun describes Java as a
�simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable,
high-performance, multithreaded, and dynamic language�.
In this Chapter, we will briefly highlight over various features of Java programming, superiority of
Java over other programming languages, and then tools available for Java programming.
Multithreading
Another important contribution of Java is the support of multithreading. The multithreading is
basically, the ability to run several tasks within the context of one large application. In more
simpler version, a program is a list of instructions , and path through list of instructions is often
called the thread of execution. In many cases, execution of a program through multiple threads
makes more sense. For an example, suppose, a program needs to open a file, draw some thing
on the screen, and print out something else. Each of these may require waiting for access to
some resource. A multithreaded program can, in effect, start up three different threads of
executions for these tasks. If the printing thread is waiting for more paper to be loaded in the
printer, then the file thread and drawing thread can continue operating. In Java multithreading
interpreter can keep track of all threads and switch among all of them, giving each a fair share of
the processor�s attention. For most applications, multithreading improves interactive
performance and real time system development. The multithreading encourages Java to
interface and support several features of modern operating systems and network protocols.
Security in Java
Java is based on client /sever technology, After the accomplishment of source code into byte
code translation, Java programs are in effect down loaded from host machines and then run on
one�s client machine. Since Java programs are down loaded from all over the world, some
precautions are needed to prevent malafide pieces of code called computer viruses embedded
within the programs, causing problems to the recipient system and spreading it around. This
therefore demands that distributed applications have to exhibit the highest level of security
concerns. A byte code verifier in the Java interpreter looks at the incoming byte codes and
verifies whether the compiled code is strictly language compliant, in case it founds illegal code,
the run time system rejects the code and refuses to run it; thus trapping all malafide codes. Once
the strict verification is over, the application proceeds to run and by this Java impotents security
holes if any.
Advantages of Java
This section is to give the answer of �Why Java? �. Following points are included as answer of
this question.
1.javac:The Java compiler. This program compiles Java source codes into byte codes
.
2.java:The Java interpreter. This program runs Java byte codes.
3.javadoc:Generates API documentation in HTML format from Java source code.
4.appletviewer:A Java interpreter that executes Java applet (a special kind of Jav
a Program) classes.
5.jdb:The Java debugger. Helps us find and fix bugs in Java programs.
6.javap:The Java disassembler. Displays the accessible functions and data in a com
piled class file. It also displays the meaning of the byte codes.
7.javah:Creates C header files that can be used to make C routines, that can call
Java routines,or make C routines that can be called by Java programs.
JDK is a free software and can be down loaded from Java Soft�s web site at http://java
.sun.com
Java Packages
The Java language provides suite of packages that include a windowing tool kit, utilities,, general
I/O, tools, and networking. Following mentioned six packages are very popular:
1.java.applet: This package includes classes designed for use within an applet. Th
ere is one class, Applet and three interfaces AppletContex, AppletStub, and AudioC
lip.
2.java.awt :The Abstract Windowing Toolkit (AWT) package, awt, contains classes us
ed to generate widgets and GUI (Graphical User Interface) components. This packag
e includes the classes: Button, CheckBox, Choice, Component, Graphics, Menu, Panel
, TextArea, and TextField.
3.java.io :The I/O Package include file input and output classes such as the clas
ses FileInputStream and FileOutputStream. File I/O in subject to Security Control
in applets.
4.java.lang:This package includes the Java language classes, including Object, Thr
ead, Exception, System, Integer, Float, Math, String, and so on.
5.java.net :This class supports the TCP/IP networking protocols and includes the S
ocket, SenverSocket, DatagramPacket, DategramSocket, URL, and URLConnection classe
s among others.
6.java.util:This class packages contains miscellaneons classes that are useful for
a variety of programming chores. Those classes include Date, Dictionary, Random (f
or creating random number), Vector, and Stack (for implementing a last-in-first-ou
t (LIFO) stack).
1.Sun�s Java Workshop dev 5 from Sun�s Javasoft provids most robust development
environment for Java. The recent addition with this is Visual Java.
2.Ed for Windows from Soft As It Get provides an Integrated Development Environmen
t ( IDE) for Java. It makes Java programming extremely easy and also provides one
of the most powefull source code editor.
3.Mojo from Penumbra Software is one of the best visual environments for creating
Java applets. Its primary goal is to allow rapid generation of Java applets withou
t writing code.
4.Jamba from Aimtech and IBM is another graphical applet builder and very powerfu
l in Java environment.
5.Semantic Cafe from Semantic another very powerful and is fully integrated Develo
pment Environment (IDE). This is aimed for professional application developers and
programmers. Currently Cafe is the de facto standard for Java development on Windo
ws NT/95.
Assignment
Q: Explain JDK, JRE and JVM.
Q: Why Java is platform independent?
Q: Why java is not 100% Object-oriented?
Q: List any five features of Java?
Q: Which component is used to compile, debug and execute java program?
Q: Which component is responsible to run java program?
Q: What is use of interpreter?
Q&A
Q: I know a lot about HTML, but not much about computer programming.
Can I still write Java programs?
A: If you have no programming experience whatsoever, you most likely will find
programming Java significantly more difficult than HTML. However, Java is an
excellent language to learn programming with, and if you patiently work
through the examples and the exercises in this book, you should be able to
learn enough to get started with Java.
Q: What's the relationship between JavaScript and Java?
A: They have the same first four letters.
A common misconception in the Web world today is that Java and JavaScript
have more in common than they actually do. Java is the general-purpose
programming language that you'll learn about in this book; you use it to create
applets. JavaScript is a Netscape-invented scripting language that looks sort of
like Java; with it you can do various nifty things in Web pages. They are
independent languages, used for different purposes. If you're interested in
JavaScript programming, you'll want to pick up another book, such as Teach
Yourself JavaScript in a Week or Laura Lemay's Web Workshop: JavaScript,
both also available from Sams.net Publishing.
Q: What is Hot Java?
A: • Hot Java was the first Web browser that could download and play (execute)
Java applets.
• Hot Java, created by Sun, is simply a browser, much like the Netscape
Navigator or Microsoft's Internet Exploere.
• Although Hot Java was the first browser to support Java applets, many
browsers now support or will soon support applets.
• Starting with Netscape Navigator 2.0 for example, you can play Java applets
for many platforms (Windows 95, the Mac and so on.).
• Another distinguishing feature of Hot Java is that unlike most browsers which
are written in C/C++.
• Hot Java browser is written with the Java programming language.
Q: I followed all the directions you gave for creating a Java applet. I loaded it
into HotJava, but Hello World didn't show up. What did I do wrong?
A: Don't use HotJava to view applets you've created in this book; get a more up-
to-date browser such as Netscape or Internet Explorer. HotJava was an
experimental browser and has not been updated since soon after its original
release. The steps you take to define and write an applet have changed since
then, and the applets you write now will not run on HotJava.
Q: What is static in Java?
A: Static means one per class, not one for each object no matter how many
instance of a class might exist. This means that you can use them without
creating an instance of a class.
Static methods are implicitly final, because overriding is done based on the
type of the object and static methods are attached to a class, not an object.
A static method in a superclass can be shadowed by another static method in a
sub class, as long as the original method was not declared final.
However, you can't override a static method with a non static method. In other
words, you can't change a static method into an instance method of a subclass.
Q: Why java does not support pointers?
A: Because pointers are unsafe, Java uses reference types to hide pointer and
programmers feel easier to deal with reference types without pointers.
Q: I'm using Notepad on Windows to edit my Java files. The program insists
on adding a .txt extension to all my files, regardless of what I name them
(so I always end up with files like HelloWorld.java.txt). Short of renaming
them before I compile them, what else can I do to fix this?
Q: What is a platform?
A: A platform is the hardware or software environment in which a program runs.
Most platforms can be described as a combination of the operating system and
hardware, like Windows 2000/XP, Linux, Solaris and MacOs.
Q: What is a JVM consists of?
A: Each time of a Java Application is executed then an instance of JVM,
responsible for its running, is created.
A JVM instance is described in terms of subsystems, memory areas, data
types, and instructions.
Q: How Java enabled high performance?
A: Java uses Just In Time compiler to enable high performance.
Just-In-Time compiler is a program that runs Java bytecode, which is a
program that contains instructions that must be interpreted into instructions that
can be sent directly to the processor.
Q: What is JTI compiler?
A: Just In Time compiler: It is used to improve the performance.
JIT compiles parts of the byte code that have similar functionality at the same
time, and hence reduces the amount of time needed for compilation.
Here the term compiler refers to translator from the instruction set of a Java
virtual machine (JVM) to the instruction set of a specific CPU.
Q: What are the supported platforms by Java Programming Languages?
A: Java runs on a variety of platforms, such as
Windows
Mac OS and
The various versions of UNIX/Linux like
HP-Unix
Sun Solaris
Redhat Linux
Ubuntu
CentOS etc.