Documente Academic
Documente Profesional
Documente Cultură
PART-B
Table of Contents
Java if Statement
o How if (if...then) statement works?
o Example
Java if...else Statement
o How if...else statement works?
o Example
Java if...else...if Statement
o Example
Nested if...else Statement
o Example
In programming, it's often desirable to execute a certain section of code based upon
whether the specified condition is true or false (which is known only during the run time).
For such cases, control flow statements are used.
if (expression)
{
// statements
}
If the expression is evaluated to false, statement(s) inside the body of if are skipped
from execution.
class ex2
{
public static void main(String[] args)
{
if (number > 0)
{
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}
if (expression)
{
// codes
}
else
{
// some other code
}
class ex3
{
public static void main(String[] args)
{
int number = 10;
if (number > 0)
{
System.out.println("Number is positive.");
}
else
{
System.out.println("Number is not positive.");
}
System.out.println("This statement is always executed.");
}
}
if (expression1)
{
// codes
}
else if(expression2)
{
// codes
}
else if (expression3)
{
// codes
}
.
.
else
{
// codes
}
The if statements are executed from the top towards the bottom. As soon as the test
expression is true, codes inside the body of that if statement is executed. Then, the
control of program jumps outside if-else-if ladder.
If all test expressions are false, codes inside the body of else is executed.
Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java
we have three types of basic loops: for, while and do-while. In this tutorial we will learn how to use “for
loop” in Java.
class ex6 {
public static void main(String[] args) {
while (testExpression)
{
// codes inside body of while loop
}
class ex7 {
public static void main(String[] args)
{
int i = 1;
do {
// codes inside body of do while loop
} while (testExpression);
EX:
class DoWhileLoopExample
{
public static void main(String args[]){
int i=10;
do
{
System.out.println(i);
i--;
}
while(i>1);
}
}
Syntax of for loop:
EX:
class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--){
System.out.println("The value of i is: "+i);
}
}
}
A class, in the context of Java, are templates that are used to create objects, and to define
object data types and methods. Core properties include the data types and methods that
may be used by the object.
A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type. In
general, class declarations can include these components, in order:
Modifiers : A class can be public or has default access (Refer this for details).
Class name: The name should begin with a initial letter (capitalized by convention).
Superclass(if any): The name of the class’s parent (superclass), if any, preceded by
the keyword extends. A class can only extend (subclass) one parent.
Object
It is a basic unit of Object Oriented Programming and represents the real life entities. A
typical Java program creates many objects, which as you know, interact by invoking
methods. An object consists of :
State : It is represented by attributes of an object. It also reflects the properties of an
object.
Behavior : It is represented by methods of an object. It also reflects the response of
an object with other objects.
Identity : It gives a unique name to an object and enables one object to interact with
other objects.
Java constructor:
class Programming {
//constructor method
Programming() {
System.out.println("Constructor method called.");
}
Method Overloading is a feature that allows a class to have more than one method having
the same name, if their argument lists are different. It is similar to constructor overloading
in Java, that allows a class to have more than one constructor having different argument
lists.
let’s get back to the point, when I say argument list it means the parameters that a method
has: For example the argument list of a method add(int a, int b) having two parameters is
different from the argument list of the method add(int a, int b, int c) having three
parameters.
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
EX:
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
The process by which one class acquires the properties(data members) and
functionalities(methods) of another class is called inheritance. The aim of inheritance is to
provide the reusability of code so that a class has to write only the unique features and rest
of the common properties and functionalities can be extended from the another class.
Child Class:
The class that extends the features of another class is known as child class, sub class or
derived class.
Parent Class:
The class whose properties and functionalities are used(inherited) by another class is
known as parent class, super class or Base class.
Syntax:
Inheritance in Java
To inherit a class we use extends keyword. Here class XYZ is child class and class ABC is
parent class. The class XYZ is inheriting the properties and methods of ABC class.
class Teacher {
String designation = "Teacher";
String collegeName = "Beginnersbook";
void does(){
System.out.println("Teaching");
}
}
Beginnersbook
Teacher
Physics
Teaching
UNIT-II
PART-A
switch (variable/expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.. .. ...
.. .. ...
default:
// statements
}
4. Define looping.
It repeats a statement or a group of statements while a given condition is true. It tests the
condition before executing the loop body. 2. for loop. It executes a sequence of statements
multiple times and abbreviates the code that manages the loop variable.
Variables represent its state. Class can have static and instance variables. Methods provide
the logic that constitutes the behavior defined by a class. Class can have static and instance
methods. Constructors initialize the state of a new instance of a class.
9. What is a constructor?
A constructor in Java is a block of code similar to a method that's called when an instance of
an object is created. Here are the key differences between a constructor and a method: A
constructor doesn't have a return type. ... Unlike methods, constructors are not considered
members of a class.