Java UNIT – I
Write about the History of Java Language?
James Gosling, Mike Sheridan, and Patrick Naughton started the Java language project in June
1991. This small team of sun engineers called Green Team.
1. Originally Java designed for small, embedded systems like set-top boxes, televisions.
2. First, Java was called "Green talk" by James Gosling and file extension was .gt
3. After that, it was called Oak.
4. In 1995, Oak was renamed as "Java" .
The latest release of the Java Standard Edition is Java SE 8.
Features of Java - Important Big Question
Question : Explain the Features of Java?
1. Simple
2. Platform Independent
3. Portable
4. Object – Oriented
5. Secure
6. Robust
7. Distributed
8. Dynamic and Extensible
9. Multithreaded and Interactive
10. High Performance
Simple
Complex concepts (like pointers, multiple inheritance) are removed from java language. So Java
language is easy to learn and user.
1
Platform Independent
Java Compiler, converts the Java program into bytecode.
This bytecode convert into Machine code / Binary code by Java Interpreter.
This bytecode is a platform independent code and run on any platforms like Windows,
Linux, Unix. i.e. Write Once and Run Anywhere(WORA).
Portable
Java programs can be easily moved from one computers system to another, anywhere and
anytime. Changes and upgrades in operating system resources will not force any changes in java
programs.
Object - Oriented:
Java supports object oriented model.
So, Java supports all the features of object oriented model like:
I. Object
II. Class
III. Inheritance
IV. Polymorphism
V. Abstraction
VI. Encapsulation
Secure:
Java provides data security through encapsulation.
Applet Program provides internet security.
Security Manager tool in Java Virtual Machine (JVM) provides the. Java System Security.
Robust
Java is a Robust language and it provides the following features.
Type checking : Java checks the Data Types in Compile Time and Run Time.
Garbage Collection: It is used to free the unused object's memory automatically.
Exception Handling : It is used to handle the errors during the program execution.
2
Distributed
Java is designed as a distributed language.
It is used to create the applications on networks (Network applications).
Remote Method Invocation (RMI) concept is used to run the Distributed Java
Applications.
Dynamic and Extensible
Java is a dynamic language.
Java language dynamically linking the new class libraries, method, and objects.
Java supports native methods.
ie. Java programs support functions written in other languages such as C and C++.These
functions are known as native methods.
Multithreaded and Interactive:
Thread is a process / task ie. a small task / job in large program.
Java is a Multithreaded Language.
Multithread means partitioning the big task/job into smaller task/job.
Multithread help us to run the multiple jobs simultaneously.
Multithreading increases CPU efficiency.
Java used to develop the interactive programs.
High Performance:
Java processing speed is high compare to C and C++.
Java architecture helps to the reduce overheads problem during runtime.
How Java Differs From C:
Java is a Object Oriented Language and C is Procedure Oriented Language.
Java does not support the C data types struct and union.
Java does not support the pointers.
3
Java does not support the C modifiers auto, extern, register, signed, and
unsigned.
Java does not have a preprocessors.
Java adds new operators such as instanceof and >>>.
Java does not support the sizeof, and typedef.
Java has many features required for object-oriented programming.
How Java Differs From C++:
Java is a true object-oriented language while C++ is basically C with object-oriented extension.
1. Java does not support the operator overloading and Pointers.
2. Java does not support global variables.
3. Java does not use template classes concepts as in C++.
4. Java has replaced the Destructor function with a finalize() function.
5. C++ supports Multiple inheritances . But Java does not support multiple inheritances
of classes. This Multiple inheritance concept is replaced by “Interface” in java.
Java and Internet
Java is used to create two types of applications. They are
1. Standalone Application
2. Applet
1. Application:
Applications are executable programs that are controlled by operating system. These applications
have an access to the system resources, such as file systems and can read and write to files on
local computers.
2. Applets
Applets are small executable programs that run on a web page.
Internet users can use Java to create applet programs and run them locally using a Java-
enabled browser such as hot Java, Internet Explorer or Google Chrome or Firefox.
4
Internet user can download the Applets from the Remote Server.
Applets have limited access to system resources.
Difference Between Applications and Applets
Application Applet
Application can access the local file system Applet programs has Restricted to access the
and resources. local file system and resources.
Functionality of the applications is very well Functionality of the applications is not known.
known.
Author of the application is known. Author of the application is not known.
Creating and running an application is easy. Creating and running an application is complex
What is Java?
Java is a general-purpose Object Oriented programming language.
Java is concurrent, class-based, object-oriented, and specifically designed to have as few
implementation dependencies as possible.
WORA: "write once, run anywhere" , ie. compiled Java code can run on all platforms
that support Java without the need for recompilation.
Explain about the Java Environment ? (15 Mark Questions)
5
Java Environment contains Java Development Kit (JDK ) and hundreds of classes ( in-
built or user defined) and methods.
These classes and methods called as Java Standard library (JSL). Also, JSL is known
as API (Application Programming Interface ).
Java Development Kit (JDK) is a collection of tools to develop and run the Java program. JDK
Contains the following tools Java compiler, Javadoc, Jar, and a debugger.
Tools of JDK:
Javac : Java compiler converts the source code into byte code.
Java : Java interpreter converts the bytecode into Machine Code.
Jbd (Java debbuger) - Java debugger helps to debug the errors in Java programs.
Appletviewer – It is used to run the Applet Programs.
Java Application Launcher : It is used to launch the Java applications.
Javap : Java disassembler converts the byte code into program description.
Javah (Java Header File Generator) - Create header files when native method are used.
Javadoc – Create HTML documents from java source code.
Java Runtime Environment (JRE)
JRE is part of the Java Development Kit (JDK).
JRE contains collectoin of Classes and Java Virtual Machine (JVM).
JRE = Collectoin of Classes + JVM (Java Virtual Machine)
6
These collection of classes is called as Java Standard Library (JSL).
The other name of Java Standard Library (JSL) is Java API (Application
Programming Interface)
It physically exists.
Java Virtual Machine (JVM) and Bytecode
The java compiler converts the source code into object code. The object code is specific
to a particular CPU. Therefore, it cannot be executed on a different platform.
So the Java Interpreter, converts the source code into bytecode. Bytecode is a plotform
independent code.
Java Virtual Machine (JVM) is used to execute the bytecode.
Explain the Process of building and running Java application programs?
1. Text Editor / Note pad is used to type the Java Program.
2. The typed java program is complied by the Java Compiler (javac) and then executed by
Java interpreter (java).
3. The java debugger (jdb) is used to find errors
4. A compiled Java program can be converted into a source code with the help of java
disassembler( javap ).
Figure : Process of building and running Java application programs
Write about the Hardware & Software Requirements for Java?
7
Hardware Requirement for Java
Minimum hardware requirement to use Java on your Windows operating system as
follows:
Hardware Requirements:
• System: Pentium IV 2.4 GHz.
• Hard Disk `: 40 GB.
• Monitor: 15 VGA Color.
• RAM: 256 Mb.
Software
Operating System (Windows / Unix)
Java SDK or JRE 1.6 or higher
Java Servlet Container (Free Servlet Container available)
Supported Database (SQL, Oracle) and library that supports the database connection
with Java .
First Example Java Program with explanation of Java Program Keywords?
class welcome
{
public static void main(String args[])
{ System.out.println("Welcome to Programming World…");
}
}
class is a keyword used to decalare class name. All class name should start with the Capital
Letter. Welcome is the class of the above program. Class name and java file name should be
same.
public static void main
The word public is a Access specifier (public, private, protected).
The word static is used to call the main method, before creating any class objects.
The word void indicates that a method does not return a value.
The word main is a method; this is a starting point of a Java program.
8
String[] args
Java main method accepts a single argument of type String array. This is also called as java
command line arguments.
Example for String[] args
public class Args
{
public static void main(String[] args)
{
for(String s : args)
{
System.out.println(s);
}
}
}
Passing Numbers:
Passing String:
Command Line Arguments:
The java command-line argument is an argument i.e. passed at the time of running the java
program.
9
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
Output:
compile by > javac CommandLineExample.java
run by > java CommandLineExample aabbcc
Output: Your first argument is: aabbcc
Explain about the Tokens in Java? (Big Question – 15 Marks)
Token is the smallest individual unit of a java program
There are Five types of Tokens in Java
1. Reserved Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
1.Reserved Keywords
Keywords are the reversed words. Java has the 50 keywords.
All keywords should be written in small letters.
Identifiers
Identifiers are used for naming the variables, methods, classes, packages and interfaces.
1. Should not begin with the digit.
2. Have alphabets, digits, underscore, and dollar sign
3. Case Sensitive (Upper & Lower case)
10
Figure : List of Keywords
Literal
A literal represents the constant value. It can be assigned for the variable.
Types of Literals in Java ( 5 Types)
i. Integral literals
ii. Floating
iii. Char literals
iv. .String literals
v. .Boolean literals
Integral literals
Integral literals are used to assign the integer values such as int, long, short and byte
Example:
int x=10;
short x=10;
Floating literals:
Floating literals used to assign the float values
Example:
11
float f=101.222;
Boolean literals
Boolean literals are used to assign the boolean values (true/false)
Example:
boolean b = true;
boolean c = false;
Character Literals
“Char” literal is used to assign the characters within single quotes.
Example:
Char ch = ‘a’;
Char ch = ‘face’;
String literals :
A sequence of characters within double quotes is considered as a string literal.
Example:
Strings="How are you";
Operators
An operator is a symbol that performs an operation. An operator acts on variables
called operands.
Types of Operator in Java
1) Arithmetic Operators
2) Assignment Operators
3) Auto-increment and Auto-decrement Operators / Unary Operators
4) Comparison (relational) operators
5) Logical Operators
6) Bitwise Operators
7) Ternary Operator
Arithmetic operators: These operators are used to perform Arithmetic operations like
12
addition, subtraction, multiplication etc.
Operator Meaning Example Result
+ Addition 3+4 7
- Subtraction 5-7 -2
* Multiplication 5*5 25
/ Division (gives quotient) 14 / 7 2
% Modulus (gives remainder) 20 % 7 6
Example :
int c = a + b;
float pi = 22 / 7;
Assignment operator (=,<, >, <=, >=, +=,-=):
Assignment operators are used to assign values to variables.
Example :
x=y+3;
a+3; ( ie a=a+3);
a-3; ( ie a=a-3);
a<=3;
·
Unary operators (++, --):
Unary operators: unary operator’s act only on one operand.
Operator Meaning Example Explanation
Unary
- minus j = -k; k value is negated and stored into j
b value will be incremented by 1
Increment b++; (called as post incrementation)
++
Operator ++b; b value will be incremented by 1
(called as pre incrementation)
b value will be decremented by 1
Decrement b--; (called as post decrementation)
--
Operator --b; b value will be decremented by 1
(called as pre decrementation)
Relational operators
Relational operators are used to compare the two values.
Operator Meaning Example
Equa
== l x == 3
13
!= Not equal x != 3
< Less than x<3
> Greater than x>3
<= Less than or equal to x <= 3
Example:
If(a==b)
Logical operators
Logical operators: Logical operators are used to construct compound conditions ie. Logical
operators are used to determine the logic between variables or values:
Operator Meaning Example Explanation
&& AND if(a>b && a>c) If a value is greater than b and c then
Operator only yes is displayed
System.out.print(“yes”);
|| operator OR Operator if(a==1 || b==1) If either a value is 1 or b value is 1 then
yes is displayed
System.out.print(“yes”);
! NOT if( !(a==0) ) If a value is not equal to zero then only
Operator yes is displayed
System.out.print(“yes”);
Bitwise operators
These operators act on individual bits (0 and 1) of the operands. They act only on integer data types, i.e. byte,
short, long and int.
Operator Meaning Explanation
& Bitwise AND Multiplies the individual bits of operands
| Bitwise OR Adds the individual bits of operands
^ Bitwise XOR Performs Exclusive OR operation
<< Left shift Shifts the bits of the number towards left a specified
number of positions
>> Right shift Shifts the bits of the number towards right a
specified number of positions sign it.
Special Operators ( instanceof Operator and Dot Operator (.) )
instanceof
The instanceof is an Object Referece Operator.
It return true if the object is belong to a given Class.
Example
person instanceof student
14
It will return true if the object person is belong to the class student.
Dot Operator (.)
It is used to access the methods and instances of a class
Example:
Person1. age ; (Referece to the variable age)
Person1.salary (Referece to the Method salary)
Explain about the Java Comments Statement?
The java comments are statements that are not executed by the compiler and Java
interpreter.
The comments statement used to provide information about the variable, method or
functions etc..
There are 3 types of comments in java.
Types of Java Comments
1. Java Single Line Comment
2. Java Multi Line Comment
3. Java Documentation Comment
1) Java Single Line Comment
Java single line comment is used to comment only one line.
Syntax:
//This is single line comment
2) Java Multi Line Comment
The multi line comment is used to comment multiple lines of code.
Syntax:
/*
This is
multi line comment
*/
3) Java Documentation Comment
15
The documentation comment is used to create documentation API. To create
documentation API, you need to use javadoc tool.
Syntax:
/**
This is
documentation
comment
*/
Example:
/** Java Documentation Comment : The Calculator class used to add the given two
2 numbers and multiply the given two . numbers*/
public class Calculator
{
public static void main(String[] args)
{
/* Java Multi Line Comment : Let's declare the variables
For Addition and Multiplications */
int a,b,add,mul;
a=100;
b=50;
add=a+b;
mul=a*b;
//Single line Comment : Print Addition result
System.out.println(“The Addition value is:+add”);
//Single line Comment : Print Multiplication result
System.out.println(“The Addition value is:+add”);
}
}
Explain about Separators in Java?
Separators are used to define / format / desing the structure of a program.
Symbol Name Description
() Parentheses Used to contain the lists.
16
Used to contains the values of automatically initialized arrays. Also used to define a block
{} Braces
of code, for classes, methods, and local scopes.
[] Brackets Used to declare array types. Also used when dereferencing array values.
; Semicolon Terminates the statements
, Comma Separates the variables
Used to separate packages names from subpackages and classes. Also used to separate a
. Period
variable or method from a reference variable.
:: Colons Used to create a method or constructor reference (Added by JDK 8)
17