JAVA NOTES
JAVA NOTES
JAVA NOTES
Repetition Structures
In real life you often do something repeatedly, for example, consider a task such as reading a book,
first you open the book, and then repeatedly – read a page; flip the page – until you get to the end of
the book, then close the book.
Similarly, when writing programs, you might need to perform the same sequence of statements
repeatedly until some condition is met.
The ability of a computer to perform the same set of actions again and again is called looping.
The sequence of statements that is repeated again and again is called the body of the loop.
The test conditions that determine whether a loop is entered or exited is constructed using relational
and logical operators.
A single pass through the loop is called an iteration.
For example, a loop that repeats the execution of the body three times goes through three iterations.
Example program :
int number = 1;
do
{
System.out.println ("Square of a number = " + number*number);
++number;
} while (number <= 5);
In entry control loop condition is checked In exit control loop condition is checked
first last
If the condition is false, loop body will not If the condition is false, loop body will
execute execute at least once
For Statement : The for loop is the most widely used Java loop construct. The structure of the
Java for statement is as below:
for (counter=initial_value; test_condition;change counter)
{
statements
}
Semicolons separate the three parts of a for loop:
The initial_value initializes the value of the loop counter.
The test_condition tests whether the loop should be executed again. The loop is
exited when the test condition fails.
The step updates the counter in each loop iteration.
e.g. Program to print the square of numbers from 1 to 5 using For loop.
int number;
for (number = 1; number<= 5; ++number)
{
System.out.print("Square of "+ number);
System.out.println(" = "+ number*number);
}
Arrays in Java :
Arrays are variables that can hold more than one value, they can hold a list of values of the same
type.
e.g.
double [ ] marks = new double[5];
marks is a variable of type array that can store marks of five students of decimal type. Each element
of the array is indexed by its position starting from 0. So the five elements of the array can be
accessed as marks[0], marks[1], marks[2], marks[3], marks[4]
To print all the marks, we can use a for loop, varying the loop index from 0 to 4.
int i;
for (i = 0; i< 5; i++)
System.out.println(marks[i]);
The following code shows the use of an array to print the class report card for the five students.
double percentage = 0;
double tmarks = 100;
String result = “ “;
for (int i = 0 ; i <marks.length ; i++)
{
percentage = (marks[i]/tmarks)*100;
if (percentage >= 40)
result = "Passed";
else
result = "Failed";
}
In this program length function is used to find out the total numbers of elements in an array.
Array Variable Declaration
A method is called/invoked from another method. When a method is called, control is transferred
from the calling method to the called method. The statements inside the called method's body are
executed. Control is then returned back to the calling method.
For e.g. Book can be a class and different types of books are the objects of class Book.
Class Design :
A class in Java begins with the keyword class followed by the name of the class. The body of the
class is enclosed within curly braces. The body contains the definitions of the data and method
members of the class.
The data members are defined by specifying their type. The method members have access to all the
data members of the class and can use them in their body. The data members of a class are like
global variables – they can be accessed by all the method members of the class.
What is Object?
An object is an entity with state and behaviour, such as a chair, bike, marker, pen, table, or car.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
Constructors
A special method member called the constructor method is used to initialize the data members of
the class (or any other initialization is to be done at time of object creation).
The constructor has the same name as the class, has no return type, and may or may not have
a parameter list. Whenever a new object of a class is created, the constructor of the class is
invoked automatically. We do not call the constructor explicitly.
Access Modifiers : There are three access modifiers Public, Private and Protected.
Private : Members in a class declared as private cannot be accessed outside the class. i.e. they
cannot be accessed through the object of the class but can be accessed by member
methods of the class.
Public : Members declared as public can be accessed outside the class i.e. they can be accessed
through the object of the class using dot operator. By default all the members of the class
are public.
e.g. public class Book {
String title;
String author;
double price;
void display( )
{
System.out.println(“Title “ + title);
System.out.println(“Author “ + author);
System.out.println(“Price “ + price);
}
}
Now to create an object of class book we need to write :
Book book1 = new Book( );
Here Book is the name of the class and book1 is an object of the class Book.
new method is used to allocate memory to the object book1 and Book( ) is the constructor method
to initialize the data members for the object book1.
To access display( ) method of class Book, we need to write
book1.display( );
Sometimes the programs gets terminated unexpectedly with runtime errors at the time of execution.
The errors could have occurred because an array index reference was out of range, or an attempt
was made to divide an integer by zero, or there was insufficient computer memory and so on.
Such an error situation that is unexpected in the program execution and causes it to terminate
unexpectedly is called an exception.
Java provides the following keywords to handle an exception:
try – A try block surrounds the part of the code that can generate exception(s).
catch – The catch blocks follow a try block. A catch block contains the exception handler – specific
code that is executed when the exception occurs. Multiple catch blocks following a try block can
handle different types of exceptions.
Wrapper Classes
Java’s primitive datatypes, including int, float, and others, are typically supplied by value rather
than via reference. Primitive datatypes may occasionally need to be passed by reference. When that
happens, you can use the Java wrapper classes.
These classes encapsulate the primitive datatype in an object. For instance, an int variable is held by
the Integer wrapper class.
int a = 50;
In the first declaration, an int variable is declared and initialized with the value 50.
In the second declaration, an object of the class Integer is created and initialized with the
value 50.
The variable a is a memory location and the variable b is a reference to a memory location that
holds an object of the class Integer.