Java Unit 1
Java Unit 1
Introduction to JAVA: JAVA Evolution: Java History, Java Features, How Java Differs from C and C++,
Java and Internet, Java and World Wide Web, Web Browsers, Hardware and Software
Requirements, Java Support Systems, Java Environment. Overview of JAVA Language: Introduction,
Simple Java program, More of Java Statements, Implementing a Java Program, Java Virtual
Machine, Command Line Arguments, Programming Style. Constants, Variables, and Data Types:
Introduction, Constants, Variables, Data Types, Declaration of Variables, Giving Values to Variables,
Scope of Variables, Symbolic Constants, Type Casting, Getting Values of Variables, Standard Default
Values, Operators and Expressions: Introduction, Arithmetic Operators, Relational Operators
Logical Operators, Assignment Operators, Increment and Decrement Operators, Conditional
Operators, Bitwise Operators, Special Operators, Arithmetic Expressions, Evaluation of Expressions,
Precedence of Arithmetic Operators, Type Conversion and Associativity, Mathematical Functions.
Decision Making and Branching: Introduction, Decision Making with if Statement, Simple if
Statement, The if…..else Statement, Nesting of if………Else Statements, The else if Ladder, The
Switch Statement, The ?: Operator. Decision Making and Looping: Introduction. The while
Statement, The do Statement, Thefor Statement, Jumps in Loops Labeled Loops.
Classes, Arrays, Strings and Vectors: Classes, Objects and Methods: Introduction, Defining a Class,
Adding Variables, Adding Methods, Creating Objects, Accessing Class Members, Constructors,
Methods Overloading, Static Members, Nesting of Methods, Inheritance: Extending a Class
Overriding Methods, Final Variables and Methods, Finalizer methods, Abstract Methods and
Classes, Visibility Control. Arrays, Strings and Vectors: Arrays, One-dimensional Arrays, Creating an
Array, Two -Dimensional Arrays, Creating an Array, Two – dimensional Arrays, Strings, Vectors,
Wrapper Classes.
Managing Exceptions, Applet Programming: Managing Errors and Exception: Introduction, Types of
Exception Handling Code, Multiple Catch Statements, Using Finally Statement, Throwing Our Own
Exceptions, Using Exceptions for Debugging. Applet Programming: Introduction, How Applets Differ
from Applications, Preparing to Write Applets, Building Applet Code, Applet Life Cycle, Creating an
Executable applet, Designing a Web Page, Applet Tag, Adding Applet to HTML File, running the
Applet, More About HTML Tags, Displaying Numerical Values, Getting Input from the User.
Text Books:
Reference Books:
2. Jefry Dwight et al, Using CGI, Second Edition, Prentice Hall, India, 1997.
3. Patrick Naughton & Herbert Schildt, JAVA 2: The Complete Reference, THM, 1999.
BLUE PRINT
Section – B: Contains 8 questions, out of which a student has to answer 5 questions. Includes sub-
question as (a) & (b).
Section – A Section – B
UNIT CHAPTERS
(2 Marks) (10 Marks)
Introduction to java 1 -
Overview of Java Language - 1
I Constants, Variables and Data Types 1 1
Operators and Expressions 1 1
Decision Making and Branching - -
Classes, Objects and Methods 1 1
Inheritance - 1
II
Arrays, strings and Vectors 1 1
Wrapper Class - -
Interfaces 1 1
III Packages 1 1
Multi-Threaded Programming 1 2
Managing Exceptions 1 1
IV
Applet Programming 1 2
Graphics Programming 1 2
V
Managing Input/output Files in JAVA 1 1
TOTAL 12 16
Answer any 10 Answer any 5
TOTAL MARKS 20 50
UNIT-I
Byte 0 1 byte
Short 0 2 byte
Int 0 4 byte
Long 0L 8 byte
default:
Java is platform Independent because java compiler converts source code into byte code
and it can be executed on any platform(OS) using JVM.
9. What is ‘labelled break’ and ‘labelled continue’?
Labelled break: the break statement breaks out the closest loop or switch
Statement.
Labelled Continue: the continue statement transfers the control to the closest
enclosing loop.
10. Mention the data types in java?
Data types: Data types specifies which type of data/ value a variable can store.
1. Primitive Data types: it is a built-in data type specifies the size and type of variable
values.
2. Non-primitive Data types: These data types are not predefined in java.
[Link]: It is the input stream used to read input from the keyboard or user.
Ex: InputStreamReader inp = new InputStreamReader([Link]);
[Link]: It is the output stream means to print the output on the console.
Ex: [Link](“Hello world”);
[Link]: It works same as the [Link] used to output error texts.
Ex: [Link](“Invalid Output”);
SECTION B ( 5 Marks Questions)
3. Write a program to display all prime numbers between two limits using command line
Argument.
public class PrimeNum
{
public static void main (String[] args)
{
int lower = 1, upper = 20;
for (int i = lower; i <= upper; i++)
if (isPrime (i))
[Link] (i);
}
static boolean isPrime (int n)
{
int count = 0;
// 0, 1 negative numbers are not prime
if (n < 2)
return false;
Main() method: It is name of the method, it is the entry point for JVM to start
execution of java program.
Public: public keyword is an access modifier, main() must be declared as public so
that JVM can access main() method and execute outside the class.
Static: It is a keyword, if main() method is static, so that it can be accessed and
executed without creating the object of a Class.
Void: It is a keyword used to define the return type of the main() method. Means
method will not return any value..
String[] args: args is name of the string array, it is not fixed you can still change the
name from args to anything. Which is used to pass the command line argument to
the main method.
Different ways of writing main method.
public static void main(String[] args)
public static void main(String args[])
public static void main(String jbt[])
public static void main(String… args)
5. Explain the history and evolution of java.
1. James Gosling, Mike and Patrick initiated the Java language project in June 1991. The
small team of sun engineers called Green Team.
2. Initially it was designed for small, embedded systems in electronic appliances like set-
top boxes, interactive television.
3. Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4. After that, it was called Oak and was developed as a part of the Green project.
5. The team gathered to choose a new name. The suggested words were "dynamic",
"revolutionary", "Silk", "jolt", "DNA", etc. They wanted something that revolutionary,
dynamic, cool, unique, and easy to spell, and fun to say.
6. According to James Gosling, "Java was one of the top choices along with Silk".
7. Java is an island in Indonesia where the first coffee was produced (called Java coffee),
java name was chosen while having a coffee nearby his office.
8. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
9. Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of
Oracle Corporation) and released in 1995.
10. In 1995, Time magazine called Java one of the Ten Best Products of 1995.
11. JDK 1.0 was released on January 23, 1996. After the first release of Java.
Static method is a method which belongs to the class and not to the object.
The static methods are also called as class members.
A static method can access only static data.
The most common example of a static member is main(). The main() is declared as
static because it must be called by the operating system when our program begins.
Static methods can be accessed directly by the class name and does not need any
object.
Example:
staticdemo.method1();
Declaration:
class staticdemo
{
int x,y;
static int z;
}
Bitwise Compliment ~ ~ op
It is a binary operator denoted by the symbol &. It returns 1 if and only if both bits are 1, else
returns 0.
It is a binary operator denoted by the symbol ^ (pronounced as caret). It returns 0 if both bits are
the same, else returns 1.
It is a binary operator denoted by the symbol | (pronounced as a pipe). It returns 1 if either of the
bit is 1, else returns 0.
It is a unary operator denoted by the symbol ~ (pronounced as the tilde). It returns the inverse or
complement of the bit. It makes every 0 a 1 and every 1 a 0.
Bit Shift Operators
Shift operator is used in shifting the bits either right or left. We can use shift operators if we divide
or multiply any number by 2.
Components of JVM
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever
we run the java program, class loader loads it first.
Class method area: It is one of the Data Area in JVM, in which Class data will be stored.
Static Variables,Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size
while the application runs.
Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which
is created for a single execution thread.
Native stack: It subsumes all the native methods used in your application.
Execution Engine:
JIT compiler
Garbage collector
JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps
in improving the performance of Java applications by compiling bytecodes to machine code
at run time.
Garbage collector: As the name explains that Garbage Collector means to collect the
unused material.
Garbage collector works in two simple steps known as Mark and Sweep:
Mark – it identifies which piece of memory is in use and which are not
Sweep – it removes objects identified during the “mark” phase
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
Example:
== Equal to x == y
!= Not equal x != y
Logical operators: Logical operators are used to determine the logic between variables or values
&& Logical Returns true if both statements are x < 5 && x < 10
AND true
! Logical Reverse the result, returns false if !(x < 5 && x < 10)
NOT the result is true
Assignment operator:
Assignment operators are used to assign values to variables.
we use the assignment operator (=) to assign the value to a variable.
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
01. abstract
02. boolean
03. byte
04. Break
05. Class
06. Float
07. For
Identifier: Identifiers are used to name a variable, constant, function, class, and array. It usually
defined by the user.
Example:
PhoneNumber
PRICE
radius
a
a1
_phonenumber
$circumference
jagged_array
12radius //invalid
Literals:
literal is a notation that represents a fixed value (constant) in the source code.
Once it has been defined cannot be changed
Java provides five types of literals are as follows:
Integer
Floating Point
Character
String
Boolean
Literal Type
23 int
9.86 double
"javatpoint" String
Operators:
Operators are the special symbol that tells the compiler to perform a special operation.
There are eight types of operators in Java, are as follows:
Arithmetic Operators
Assignment Operators
Relational Operators
Unary Operators
Logical Operators
Ternary Operators
Bitwise Operators
Shift Operators
Operator Symbols
Arithmetic +,-,/,*,%
Unary ++ , - - , !
Assignment = , += , -= , *= , /= , %= , ^=
Logical && , ||
Bitwise &,|,^,~
Shift << , >> , >>>
Separators:
The separators in Java are also known as punctuators.
There are nine separators in Java, are as follows:
separator <= ; | , | . | ( | ) | { | } | [ | ]
Square Brackets []: It is used to define array elements. A pair of square brackets represents
the single-dimensional array, two pairs of square brackets represent the two-dimensional
array.
Parentheses (): It is used to call the functions and parsing the parameters.
Curly Braces {}: The curly braces denote the starting and ending of a code block.
Comma (,): It is used to separate two values, statements, and parameters.
Assignment Operator (=): It is used to assign a variable and constant.
Semicolon (;): It is the symbol that can be found at end of the statements. It separates the
two statements.
Period (.): It separates the package name form the sub-packages and class. It also separates
a variable or method from a reference variable.
Comments:
Comments allow us to specify information about the program inside our Java code.
Java compiler recognizes these comments as tokens but excludes it form further processing.
The Java compiler treats comments as whitespaces.
for loop: Used to execute block of statements repeatedly until certain condition is reached.
Syntax:
While Loop: While loop first check the condition and if the condition is true then it execute block of
statements and continue to execute block of statement as long as condition is true.
Syntax:
while(condition)
{
//Block Of Statement
}
Example
public class demo
{
public static void main(String[] args)
{
int i = 0;
while(i<10)
{
[Link](i);
i++;
}
}
}
Do-while loop:
The Java do-while loop is used to iterate a part of the program repeatedly, until the
specified condition is true.
Java do-while loop is called an exit control loop.
Syntax:
do{
/ / loop body
//update statement
}while (condition);
Example
public class DoWhileExample
{
public static void main(String[] args)
{
int i=1;
do
{
[Link](i);
i++;
}while(i<=10);
}
}