Module 1
Module 1
Java
Inheritance :
The process by which object of one class gets the
properties of another class.
Benefits of inheritance
Indented tohelp REUSE the existing code with little or no
modification .
Easy to implement real word models.
Inheritance can save time and effort as the main code need
not be written again.
Inheritance provides a clear model structure which is easy
to understand.
An inheritance leads to less development and maintenance
costs.
Polymorphism
Polymorphism refers to the ability of a class to
provide different implementations of a method,
depending on the type of object that is passed to the
method. (Taking more than one form ) Java allows
us to perform the same action in many different
ways.
Ex: Method overriding.
Procedural and OOP paradigm
Procedural Oriented Programming Object-Oriented Programming
Data types specify the different sizes and values that can
be stored in the variable.
There are two types of data types in Java:
Primitive data types: The primitive data types include
Boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types
include classes, interfaces and arrays.
Datatype Size
boolean 1 bite
char 2 byte
byte 1 byte
short 2 byte
Int 4 byte
Long 8 byte
Float 4 byte
Double 8 byte
Boolean : The Boolean data type is used to store only two
possible values: true and false. This data type is used for
simple flags that track true/false conditions.
Ex: boolean one=false
Long datatype: The long data type is a 64-bit two's complement integer. Its
value-range lies between -9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive). Its default value is 0.
Ex: long l=1000000
Flot datatype: The float data type is a single-precision 32-bit IEEE 754
floating point. Its value range is unlimited. It is recommended to use a float
(instead of double) if you need to save memory in large arrays of floating
point numbers. Its default value is 0.0F.
Ex: float f1 = 234.5f
Java variable
A variable is a container which holds the value
while the Java program is executed. A variable is
assigned with a data type.
A variable is the name of a reserved area allocated
in memory. In other words, it is a name of the
memory location.
Declaration of variable
Syntax: datatype variablename;
Ex: int a,b,c;
Types of variable
1. Local variable
2. Class variable (static)
3. Instance variable
1.Local Variable: A variable declared inside the
body of the method is called local variable. You can use
this variable only within that method.
Examples: +,-,*,/,++,--etc
Types of Operators
Arithmetic Operators.
Unary Operators.
Assignment Operators.
Relational Operators.
Logical Operators.
Ternary Operators
Bitwise Operators.
Shift Operators.
Operator hierarchy
Ex: s=a+b/c
Evaluation of Expression
3+4*4+5*(4+3)-1
Type conversion and casting
Within an expression, It is possible to mix
two or more different types of data as long as
they are compatible with each other.
Syntax
if(condition)
{
statement 1; //executes when condition is true
}
Ex :
if(5>2)
{
System.out.println(“5 is greater “);
}
if else statement
}
Switch statement
1. while loop
2. do .. While loop
3. for loop
1. While loop
A while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.
Syn: while(condition)
{
statements;
}
public class Example
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(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. If
the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use a
do-while loop.
Syn:do
{
statements;
}while(condition);
For loop
When you know exactly how many times you want to loop
through a block of code, use the for loop instead of a while loop:
Syn: for(statement1;statement2;statement3)
{
}
Statement 1 is executed (one time) before the execution of the
code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has
been executed.
Ex:
for(int i=0;i<5;i++)
{
System.out.println(i);
}
Jump Statements
Jumping statements are control statements that transfer
execution control from one point to another point in the
program. There are two Jump statements that are provided in
the Java programming language:
1.Break statement.
2.Continue statement.
Break statement
In java the break statement is used to terminate the
execution of the nearest looping statement or switch
statement. The break statement is widely used with the switch
statement, for loop, while loop, do-while loop.
Syntax: break;
or
break label;
Declaration syntax
Syntax :
type[] []….[]name=new type[size1][size2]…[sizeN];
Ex:
int [] [] [] mul=new int [4] [5] [2];
Console input and output