0
0
Paper – 5
Java Programming
(36 hours @ 3 hrs per week)
Java Programs using Java Development Kit (JDK) 1.4 covering the concepts given in Appendix – I.
Suggested Reading:
1. The Complete Reference Java2 – Herbert Schildt (third edition )
2. Java2 Programming Black Book—Steven Holzner (dreamtech press)
Java
Introduction:
In today’s era the internet and smart phones have become an integral part of our lives. We use our phones for
almost all of our day to day tasks. Earlier we use to shop at malls, queue up in front of Banks and make our
travel bookings at travel agencies .But now, we can do all this in the comfort of our homes using smart phones.
All of this is possible because of a high level programming language called JAVA.
Java is a class based Object Oriented Programming Language that is used in a distributed environment on the
internet. It is a high-level language that is easy to read and understand.
Java is popularly used in console, GUI, Web and mobile applications, game development and also to make
embedded system apart from these java is also used not only in computers and mobiles but even in electronic
devices like Televisions, Washing Machines, Air Conditioners and so on, online forms, banking and shopping
are possible because of Java.
Java is a computer based programming language invented by James Gosling and Sun Micro Systems in 1991.
It was WORA(Write Once and Run Anywhere) this meant that code would have to be written only once but,
it could be used anywhere.
They named this language Oak because the tree outside Gosling Office.. later it was named to Green, then to
“Java coffee”” which was named after coffee from Indonesia, and finally to Java in 1995.
Steps to create a java program
Step 1) Open Notepad from Start menu by selecting Programs > Accessories > Notepad.
Public means the method is visible and called from other objects of other types
Static means that the method is associated with the class, not a specific instance(object) of that class
which means you can call a static method without creating an object of the class.
String args[] – it stores java command line arguments and is an array of type java.lang.String class.
• Now Type the System.out.println("Welcome to Nizam College"); which displays the text Hello
World.
class Demo
{
public static void main(String args[])
{
System.out.println(“ Welcome to Nizam College”);
}
}
Step 5) To execute the code, enter the command java followed by the class name, as expected
output Welcome to Nizam College is displayed now.
A Compiler is a computer program (or a set of programs) that transforms source code written in a programming
language (the source language) into another computer language (the target language) this binary code is known
as Object code(Machine code) language ie. Binary.
A Java Virtual Machine(JVM) an implementation of the Java Virtual Machine specification, interprets
compiled java binary code (called byte code) for z computer’s processor(or a Hardware platform) so that it
can perform a java program’s instructions.
Java was designed to allow application programs to be built that could be run any platform with out having to
be rewritten or recompiled by the programmer for each separate platform.
A JVM makes this possible because it is aware of the specific instruction lengths and other particularities of
the platform.
Java code(.java)
Javac
compiler
Byte code(.class)
There is given many features of java. They are also known as java buzzwords.
1) Simple
2) Object-oriented
3) Platform independent
4) Secured
5) Robust
6) Architectural neutral
7) Portable
8) Dynamic
9) Interpreted
10) High performance
11) Multithread
12) Distributed
Simple:-
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun, Java
language is a simple programming language because:
o Java syntax is based on C++ (so easier for programmers to learn it after C++).
o Java has removed many complicated and rarely-used features, for example, explicit pointers, operator
overloading, etc.
o There is no need to remove unreferenced Garbage objects because there is an Automatic garbage
Collection in Java.
Object-oriented:-
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent:-
Java is platform independent because it is different from other languages like C, C++, etc. which are compiled
into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware
or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-based
platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform that runs
on the top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java
code is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code
because it can be run on multiple platforms, i.e., Write Once and Run Anywhere(WORA).
Secured:-
Java is secured because:
*) no explicit pointers
*) Programs run inside a virtual machine sandbox
Robust:-
Java is architecture neutral because there are no implementation dependent features, for example, the size of
primitive types is fixed.
In C Programming int data type occupies 2 bytes where as java occupies 4 bytes.
Portable:-
High-performance:-
Since byte code is “close” to native code still some what slower than a compiled language (eg. C++)
Distributed:-
We can create distributed applications in java. We may access files by calling the methods from any
machine on the internet. (RMI and EJB are used)
Multithread:-
Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit
of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same
time, like While typing, grammatical errors are checked along.
Interpreted:-
In java 1.0 version there is an interpreter for executing the byte code. As interpreter is quite slow when
compared to a compiler, java programs used to execute slowly.
After 1.0 the interpreter was replaced with JIT (Just in time compiler)
JIT compiler converts the byte code into machine code piece by piece and catches them for future use.
Dynamic:-
Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand.
It also supports functions from its native languages, i.e., C and C++.
Data types in java:-
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float,
double, and boolean. These can be put in four groups:
• Integers
This group includes byte, short, int, and long, which are for whole-valued signed numbers.
• Floating-point numbers
This group includes float and double, which represent numbers with fractional precision.
• Characters
This group includes char, which represents symbols in a character set, like letters and numbers.
• Boolean
This group includes boolean, which is a special type for representing true/false values.
Integers:
Java defines four integer types: byte, short, int, and long. All of these are signed,
positive and negative values.
The width and ranges of these integer types vary widely, as shown in this table:
NAME WIDTH RANGE
long 64 –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 – 32,768 to 32,767
byte 8 – 128 to 127
byte:-
The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127.
Byte variables are declared by use of the byte keyword.
Eg. byte b, c;
short:-
short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is least-used Java type. This type is
mostly applicable to 16-bit computers
Eg. short s;
int:-
The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2,147,483,648
to 2,147,483,647
Eg. int a;
long:-
long is a signed 64-bit type and is useful for those occasions where an int type is not
large enough to hold the desired value. The range of a long is quite large. This makes it
useful when big, whole numbers are needed.
// Write a java program to compute distance light travels using long variables
class Light
{
public static void main(String args[])
{
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
Expected output:
In 1000 days light will travel about 16070400000000 miles.
Floating Point Types:-
Floating-point numbers, also known as real numbers, are used when evaluating expressions that require
fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine,
result in a value whose precision requires a floating-point type. There are two kinds of floating-point types,
float and double,
which represent single- and double-precision numbers, respectively.
Float:-
The type float specifies a single-precision value that uses 32 bits of storage. Single precision.
Variables of type float are useful when you need a fractional component
For example:
float hightemp, lowtemp;
double:-
double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually
faster than single precision on some modern processors that have been optimized for high-speed mathematical
calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When
you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers,
double is the best choice.
// Write a java program to compute the area of a circle.
class Area
{
public static void main(String args[])
{
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
Characters:-
In Java, the data type used to store characters is char. However char in Java is not the same as char in C or
C++. In C/C++, char is an integer type that is 8 bits wide. This is not the case in Java. Instead, Java uses
Unicode to represent characters. Unicode defines a fully international character set that can represent all of the
characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek,
Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits.