Java Syllabus
Java Syllabus
1. Simple and familiar: it is simple to learn, understand, read, and write. Java
programs are easy to create & implement compared to other programming
languages such as C and C++
2. Object oriented: Java is a fully object-oriented language, unlike C++ which is
semi object-oriented. It supports every OOP concept such as Abstraction,
Encapsulation, Inheritance & Polymorphism.
3. Platform independent: It is means that Java programs compiled on one
machine or operating system can be executed on any other machine or operating
system without modifications.
4. Compiled & interpreted: When a Java program is created, the Java compiler
converts the java source code into byte code.
5. Multi-threaded: Java supports multi-threading programming that means
creating multiple threads to handle multiple tasks at the same time.
6. Dynamic: It allows programmers to dynamically link new class libraries, objects,
and methods.
7. Robust: Java is a robust language that can handle run-time errors as it checks
the code during the compile and runtime.
8. Secure: That ensures programs cannot take the access to memory locations
without authorization.
PRASHANT V KALE-PATIL
1
Explain the programming concept of Core Java.
This is similar to the main() function in c/c++. Every java application program
must include the main method.
This line contains a number of keywords public, static & void.
public: It is a access specifier that means it will be accessed by publicly.
static: It is access modifier that means when the java program is load then
it will create the space in memory automatically.
void: It is a return type i.e it does not return any value.
main (): It is a method or a function name.
Token are,
1. Keywords
2. Identifiers
3. Literals
4. Separators
5. Operators – Operators are Symbols that is use to operate the operations
C) X multiplication
PRASHANT V KALE-PATIL
2
Operators Meaning.
a) < Less than
Operators Meaning
& AND
|| OR
PRASHANT V KALE-PATIL
3
^ EXCLUSIVE OR
<< SHIFT LEFT
>> SHIFT RIGHT
>>> SHIFT RIGHT WITH
ZERO FILL
~ ONES COMPLEMENT
What is a constant?
PRASHANT V KALE-PATIL
4
Constant is nothing but the fixed values that don’t change during the execution
of progress. In java, there are different types of constants.
1. Numeric constants
2. Character constants.
b) Real Constants:
The real constants are also called as floating point constant. eg: 0.856, 0.86,
56.32
2. Character Constants:
There are three types of character constants.
a) Single character constants:
Single character enclosed within a single quote marks e.g: ‘X’, ‘8’, ‘h’
b) String constants:
It contains sequence of characters enclosed within a double quote.
PRASHANT V KALE-PATIL
5
Explain different data types in java?
2. Class variable: Class variables are global to a class & belong to the entire
set of objects that class creates.
3. Local variable: Variable declared & used inside methods are called local
variables.
PRASHANT V KALE-PATIL
6
CONTROL STATEMENT
1. if statement
2. if-else statement
3. if-else-if ladder
4. nested if statement
The Java if statement tests the condition. It executes the if block if condition is
true.
PRASHANT V KALE-PATIL
7
1. If Statement:
Example:
Play Videos
int age=20;
if(age>18)
Output:
PRASHANT V KALE-PATIL
8
2. if-else Statement:
The Java if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.
Syntax:
if(condition){
//code if condition is true
} else{
//code if condition is false
}
Example:
A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.
public class IfElseExample {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
System.out.println("even number");
}
Else {
System.out.println("odd number");
}
}}
PRASHANT V KALE-PATIL
9
Leap Year Example:
A year is leap, if it is divisible by 4 and 400. But not by 100.
public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println("LEAP YEAR");
}
else {
System.out.println("COMMON YEAR");
}
}
}
Output:
LEAP YEAR
PRASHANT V KALE-PATIL
10
3. if-else-if ladder
The if-else-if ladder statement executes one condition from multiple
statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}
else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3)
//code to be executed if condition3 is true
}
...
else {
//code to be executed if all the conditions are false
}
PRASHANT V KALE-PATIL
11
Example:
Java Program to demonstrate the use of “ If else-if ladder.”
//It is a program of grading system for fail, D grade, C grade, B grade, A grade
and A+.
public class IfElseIfExample {
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}
PRASHANT V KALE-PATIL
12
4. Nested if statement
The nested if statement represents the if block within another if block. Here,
the inner if block condition executes only when outer if block condition is
true.
Syntax:
if(condition) {
//code to be executed
if(condition){
//code to be executed
}
}
Example:
Java Program to demonstrate the use of Nested If Statement.
public class JavaNestedIfExample {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}}
Test it now
Output:
You are eligible to donate blood
PRASHANT V KALE-PATIL
13
Java Switch Statement
The Java switch statement executes one statement from multiple conditions. It
is like if-else-if Ladder statement.
The switch statement works with byte, short, int, long, enum types, String. In
other words, the switch statement tests the equality of a variable against
multiple values.
Points to Remember
There can be one or N number of case values for a switch expression.
Syntax:
switch (expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
PRASHANT V KALE-PATIL
14
Example:
PRASHANT V KALE-PATIL
15
Java WHILE LOOP
The Java while loop is used to repeated a part of the program until the specified
Boolean condition is true. As soon as the Boolean condition becomes false, the
loop automatically stops. It is ENTRY LOOP.
Syntax:
while (condition){
//code to be executed
Increment / decrement statement
}
Example:
int i=1;
while (i<=10){
System.out.println(i);
i++;
PRASHANT V KALE-PATIL
16
Java 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.
Therefore, unlike while loop and for loop, the do-while check the condition at the
end of loop body.
Syntax:
do {
//update statement
while (condition);
Example:
int i=1;
do {
System.out.println(i);
i++;
} while(i<=10);
PRASHANT V KALE-PATIL
17
Java BREAK Statement
Example:
if(i==5){
break;
System.out.println(i);
PRASHANT V KALE-PATIL
18
Java CONTINUE Statement
The Java CONTINUE statement is used to continue the loop. It continues the
current flow of the program.
Example.
//for loop
if (i==5){
System.out.println (i);
PRASHANT V KALE-PATIL
19
PALINDROME Program in Java
Example:
PRASHANT V KALE-PATIL
20
ARMSTRONG Number in Java
Example:
3: 31 = 3
PRASHANT V KALE-PATIL
21
OOP’s (Object-Oriented Programming System)
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Object: Any entity that has state and behaviour is known as an object.
An Object can be defined as an instance of a class. An object contains an
address and takes up some space in memory.
Example: public class CreateObject
{
void show()
{
System.out.println("Welcome");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObject obj = new CreateObject ();
//invoking method using the object
obj.show();
}
}
PRASHANT V KALE-PATIL
22
https://www.javatpoint.com/java-constructor
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
PRASHANT V KALE-PATIL
23
Question: why constructor overriding is not possible in java?
Constructor Overriding is never possible in Java. This is because,
Constructor looks like a method but name should be as class name
and no return value. Overriding means what we have declared in Base
class, that exactly we have to declare in Derived class it is called
Overriding.
When two or more methods in the same class have the same name but
different parameters, it’s called Overloading. When the method name and
parameters are the same in the superclass and the child class, it’s called
Overriding.
PRASHANT V KALE-PATIL
24
Objects and Classes in Java
Object Definitions:
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
PRASHANT V KALE-PATIL
25
Instance variable in Java
A variable which is created inside the class but outside the method is known as
an instance variable. Instance variable doesn't get memory at compile time. It
gets memory at runtime when an object or instance is created
Method in Java
Advantage of Method
o Code Reusability
o Code Optimization
The new keyword is used to allocate memory at runtime. All objects get memory
in Heap memory area.
1. By reference variable
2. By method
3. By constructor
o By new keyword
o By newInstance() method
o By clone() method
o By deserialization
o By factory method etc
PRASHANT V KALE-PATIL
26
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object.
PRASHANT V KALE-PATIL
27
The problem without this keyword
Let's understand the problem if we don't use this keyword by the example given
below:
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
class TestThis1{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Output:
0 null 0.0
0 null 0.0
PRASHANT V KALE-PATIL
28
Solution of the above problem by this keyword
Here parameters (formal arguments) and instance variables are same. So, we
are using this keyword to distinguish local variable and instance variable.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Output:
PRASHANT V KALE-PATIL
29
Program where this keyword is not required
Here local variables (formal arguments) and instance variables are different,
there is no need to use this keyword like in the following program
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Output:
You may invoke the method of the current class by using the this keyword. If
you don't use the this keyword, compiler automatically adds this keyword while
invoking the method.
PRASHANT V KALE-PATIL
30
class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}}
Output:
hello n
hello m
The this() constructor call can be used to invoke the current class constructor.
class A{
A(){System.out.println("hello a");}
A(int x) {
this();
System.out.println(x);
} }
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
Hello
10
PRASHANT V KALE-PATIL
31
this: to pass as an argument in the method
The this keyword can also be passed as an argument in the method. It is mainly
used in the event handling.
class S2{
void m(S2 obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
S2 s1 = new S2();
s1.p();
}
}
Output:
method is invoked
PRASHANT V KALE-PATIL
32
Java static keyword
The static keyword is used for memory management mainly. We can apply
static keyword with variables.
The static keyword belongs to the class than an instance of the class.
o The static variable can be used to refer to the common property of all
objects (which is not unique for each object), for example, the company
name of employees, college name of students, etc.
o The static variable gets memory only once in the class area at the time of
class loading.
PRASHANT V KALE-PATIL
33
Example of static variable
Output:
PRASHANT V KALE-PATIL
34
Program of the counter without static variable
Counter(){
count++;//incrementing value
System.out.println(count);
}
Output:
1
1
1
PRASHANT V KALE-PATIL
35
https://www.geeksforgeeks.org/inheritance-in-java/
Inheritance in Java
Inheritance is an important pillar of OOP (Object-Oriented Programming). It is
the mechanism in java by which one class is allowed to inherit or access the
features (fields and methods) of another class.
Important terminology:
PRASHANT V KALE-PATIL
36
Types of Inheritance in Java
Below are the different types of inheritance which are supported by Java.
PRASHANT V KALE-PATIL
37
2. Multilevel Inheritance: In Multilevel Inheritance, A derived class will be
inheriting a base class and as well as the derived class also act as the base
class to other class. In the below image, class A serves as a base class for
the derived class B, which in turn serves as a base class for the derived class
C
PRASHANT V KALE-PATIL
38
3. Hierarchical Inheritance: In Hierarchical Inheritance, one class
acted as a superclass (base class) & there is more than one subclass.
In the below image class A serves as a base class for the derived class
B, C and D.
PRASHANT V KALE-PATIL
39
4. Multiple Inheritances (Through Interfaces): In Multiple inheritances, one
class can have more than one superclass and inherit features from all parent
classes. Please note that Java does not support multiple inheritances with
classes. In java, we can achieve multiple inheritances only through
Interfaces. In the image below, Class C is derived from interface A and B.
PRASHANT V KALE-PATIL
40
5. Hybrid Inheritance (Through Interfaces): It is a combination of Multilevel
and Hierarchical inheritances. Since java doesn’t support multiple
inheritances with classes, hybrid inheritance is also not possible with
classes. In java, we can achieve hybrid inheritance only through Interfaces.
PRASHANT V KALE-PATIL
41
Java IS-A type of Relationship.
IS-A is a way of saying: This object is a type of that object. Let us see how the
extends keyword is used to achieve inheritance.
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.
PRASHANT V KALE-PATIL
42
POLYMORPHISM
The word "poly" means many and "morphs" means forms. So Polymorphism
means "many forms" and it occurs when we have many classes that are related
to each other by inheritance. We can perform a single action in different ways.
Compile-time Polymorphism
Runtime Polymorphism.
If a class has multiple methods having same name but different in parameters, it
is known as Method Overloading.
In this example, we have created two methods, first add() method performs
addition of two numbers and second add method performs addition of three
numbers.In this example, we are creating static methods so that we don't need
to create instance for calling methods.
class Adder {
static int add(int a,int b) {return a+b;}
static int add(int a,int b,int c) {return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args) {
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
PRASHANT V KALE-PATIL
43
Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in data type. The first
add method receives two integer arguments and second add method receives
two double arguments.
class Adder{
}
class TestOverloading2{
System.out.println(Adder.add(11,11));
System.out.println (Adder.add(12.3,12.6));
}}
In java, method overloading is not possible by changing the return type of the
method only because of ambiguity.
class Adder{
static int add (int a,int b) {return a+b;}
static double add(int a,int b) {return a+b;}
}
class TestOverloading3 {
public static void main(String[] args) {
System.out.println (Adder.add(11,11)); //ambiguity
}}
Output:
Yes, by method overloading. You can have any number of main methods in a
class by method overloading. But JVM calls main() method which receives string
array as arguments only.
PRASHANT V KALE-PATIL
44
Method Overriding in Java
If subclass (child class) has the same method as declared in the Superclass
(parent class), it is known as method overriding in Java.
Output:
Vehicle is running
PRASHANT V KALE-PATIL
45
Can we override static method?
It is because the static method is bound with class whereas instance method is
bound with an object. Static belongs to the class area, and an instance belongs
to the heap area.
PRASHANT V KALE-PATIL
46
Super Keyword in Java
We can use super keyword to access the data member or field of parent
class. It is used if parent class and child class have same fields.
class Animal {
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
class TestSuper1{
d.printColor();
}}
Output:
White
Black
PRASHANT V KALE-PATIL
47
Instance Initializer block is used to initialize the instance data member. It run
each time when object of the class is created.
There are mainly three rules for the instance initializer block. They are as follows:
class A {
A() {
System.out.println("parent class constructor invoked");
}
}
class B2 extends A{
B2(){
super();
System.out.println("child class constructor invoked");
}
{System.out.println("instance initializer block is invoked");
}
public static void main(String args[]){
B2 b=new B2();
}
}
Output: parent class constructor invoked
instance initializer block is invoked
child class constructor invoked
PRASHANT V KALE-PATIL
48
The final keyword in java is used to restrict the user. The java final keyword
can be used in many contexts. Final can be:
1. variable
2. method
3. class
If you make any variable as final, you cannot change the value of final variable
(It will be constant).
class Bike{
final void run(){System.out.println("running");}
}
PRASHANT V KALE-PATIL
49
Runtime Polymorphism in Java
Upcasting
If the reference variable of Parent class (Super class) refers to the object of
Child class, it is known as upcasting.
class A{}
class B extends A{}
class Bike{
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("Running safely with 60km");}
Output:
PRASHANT V KALE-PATIL
50
There are the following differences between compile-time polymorphism and
runtime polymorphism.
It is also known as static binding, early It is also known as dynamic binding, late binding, overrid
binding, or overloading. dynamic method dispatch.
It provides fast execution because the type It provides slower execution as compare to compile-t
of an object is determined at compile-time. the type of an object is determined at run-time.
Compile-time polymorphism provides less Run-time polymorphism provides more flexibility bec
flexibility because all the things are resolved things are resolved at runtime.
at compile-time.
PRASHANT V KALE-PATIL
51
ABSTRACTION
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the
body of the method.
PRASHANT V KALE-PATIL
52
Abstract class having constructor, data member & methods
An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor and even main () method.
PRASHANT V KALE-PATIL
53
Interface in Java
There are mainly three reasons to use interface. They are given below.
PRASHANT V KALE-PATIL
54
Java Interface Example
interface printable {
void print();
}
class A6 implements printable {
public void print()
{
System.out.println("Hello");
}
public static void main(String args[]) {
A6 obj = new A6();
obj.print();
}
}
PRASHANT V KALE-PATIL
55
interface Printable {
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");}
Output:
Hello
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface3 implements Printable, Showable
{
public void print(){System.out.println("Hello");}
public static void main(String args[]) {
PRASHANT V KALE-PATIL
56
TestInterface3 obj = new TestInterface3();
obj.print();
} }
Output:
Hello
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Output:
drawing rectangle
27
PRASHANT V KALE-PATIL
57
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
PRASHANT V KALE-PATIL
58
Encapsulation in Java
https://www.javatpoint.com/access-modifiers
We can create a fully encapsulated class in Java by making all the data members
of the class private. Now we can use setter and getter methods to set and get
the data in it.
1. By providing only a setter or getter method, we can make the class read-
only or write-only.
2. It provides you the control over the data.
3. It is a way to achieve data hiding in Java because other class will not be
able to access the data
4. The encapsulate class is easy to test. So, it is better for unit testing.
5. It is easy and fast to create an encapsulated class in Java.
PRASHANT V KALE-PATIL
59
Let's see another example of encapsulation that has only four fields with its
setter and getter methods.
PRASHANT V KALE-PATIL
60
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
String[] cars;
package Array_Class;
System.out.println("***********");
System.out.println(B.trim());
System.out.println("***********");
String C= "Prashant_Sanika";
System.out.println(C.startsWith("P"));
System.out.println(C.endsWith("a"));
System.out.println(C.charAt(5)); // That defined the number the letter.
System.out.println("***********");
PRASHANT V KALE-PATIL
61
Loop Through an Array
Loop through the array elements with the for loop, and use the length property
to specify how many times the loop should run.
Example
System.out.println(cars[i]);
Multidimensional Arrays
To create a two-dimensional array, add each array within its own set of curly
braces:
Example
System.out.println(myNumbers[i][j]);
}
}
PRASHANT V KALE-PATIL
62
Sort Array in Ascending Order
The ascending order arranges the elements in the lowest to highest order.
import java.util.Arrays;
public class SortArrayExample1
{
public static void main(String[] args)
{
int [] array = new int [] {90, 23, 5, 109, 12, 22, 67, 34};
Arrays.sort(array);
System.out.println("Elements of array sorted in ascending order: ");
//prints array using the for loop
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
}
Output:
PRASHANT V KALE-PATIL
63
Sort Array in Descending Order
The descending order arranges the elements in the highest to lowest order.
import java.util.Arrays;
import java.util.Collections;
public class SortArrayExample4
{
public static void main(String[] args)
{
Integer [] array = {23, -9, 78, 102, 4, 0, -1, 11, 6, 110, 205};
// sorts array[] in descending order
Arrays.sort(array, Collections.reverseOrder());
System.out.println("Array descending order: " +Arrays.toString(array));
}
}
Output:
Array descending order: [205, 110, 102, 78, 23, 11, 6, 4, 0, -1, -9]
PRASHANT V KALE-PATIL
64
To find the maximum and minimum value
package Array_Class;
}
static int getMax(int arr[], int n)
{
int r = arr[0];
for (i=1; i<n; i++)
r= Math.max(r, arr[i]);
return r;
}
public static void main(String[] args) {
int arr[]= {12, 1234, 14, 34};
int n = arr.length;
Output:
Min = 12
Max = 1234
PRASHANT V KALE-PATIL
65