JAVA
JAVA
ON
“CORE JAVA”
Submitted to,
Krishna Pandey Sir
Industrial Training at :
Training Certificate:
Table of Content:
4. Conclusion………...………………………………………………………………………22
Core Java
D. DATA TYPES
logical OR ||
boolean false 1 bit
Ternary ternary ?:
additive +-
II. BASIC CONCEPTS IN JAVA
Shift shift << >> >>> A. ITERATORS
Relational comparison < > <= >=
instanceof
i. For-Loop
equality == != The Java for loop is used to
Bitwise bitwise AND & iterate a part of the program
several times. If the number of
bitwise ^
exclusive OR iteration is fixed, it is
recommended to use for loop.
bitwise |
inclusive OR
for (initialization; condition; iii. Do-while Loop
The Java do-while loop is used
increment/decrement) {
to iterate a part of the program
//statement or code to be executed
repeatedly, until the specified
} condition is true. If the number
of iteration is not fixed and you
For-each loop
must have to execute the loop
The for-each loop is used to traverse array or at least once, it is recommended
collection in Java. It is easier to use than to use a do-while loop.
simple for loop because we don't need to
increment value and use subscript notation. It Java do-while loop is called an
works on the basis of elements and not the exit control loop. Therefore,
index. It returns element one by one in the unlike while loop and for loop,
defined variable. the do-while check the
C. STRING
In Java, string is basically an object that
represents sequence of char values. An
array of characters works same as Java
string. For example:
i Method Signature: Every method iv Method Name: It is a unique name
has a method signature. It is a part that is used to define the name of a
of the method declaration. It method. It must be corresponding
includes the method name and to the functionality of the method.
parameter list. Suppose, if we are creating a
method for subtraction of two
ii Access Specifier: Access specifier numbers, the method name must be
or modifier is the access type of the subtraction(). A method is invoked
method. It specifies the visibility of by its name.
the method. Java provides four
types of access specifier: v Parameter List: It is the list of
o Public: The method is accessible parameters separated by a comma
by all classes when we use public and enclosed in the pair of
specifier in our application. parentheses. It contains the data
o Private: When we use a private type and variable name. If the
access specifier, the method is method has no parameter, left the
accessible only in the classes in parentheses blank.
which it is defined.
o Protected: When we use protected vi Method Body: It is a part of the
access specifier, the method is method declaration. It contains all
accessible within the same package the actions to be performed. It is
or subclasses in a different enclosed within the pair of curly
package. braces.
o Default: When we do not use any
access specifier in the method Naming a Method
declaration, Java uses default
While defining a method, remember that the
access specifier by default. It is
method name must be a verb and start with a
visible only from the same package
lowercase letter. If the method name has more
only.
than two words, the first name must be a verb
iii Return Type: Return type is a data
followed by adjective or noun. In the multi-word
type that the method returns. It may
method name, the first letter of each word must be
have a primitive data type, object,
in uppercase except the first word. For example:
collection, void, etc. If the method
does not return anything, we use Single-word method name: sum(), area()
statement 3;
ii. catch
The "catch" block is used to handle the
exception. It must be preceded by try
block which means we can't use catch
block alone. It can be followed by
finally block later.
iii. finally
The "finally" block is used to execute
the necessary code of the program. It is
executed whether an exception is
handled or not.
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
As you can see in the above figure, object gets the
e3.insert(103,"nakul",55000);
memory in heap memory area. The reference
variable refers to the object allocated in the heap e1.display();
memory area. Here, s1 and s2 both are reference e2.display();
variables that refer to the objects allocated in
e3.display();
memory.
}
}
iii. By constructor
class Employee{
id = i; int age;
System.out.println(id+" "+name); }
s2.display(); }
The constructor is The method is invoked o Class: A class is a group of objects which
invoked implicitly. explicitly.
have common properties. It is a template
The Java compiler The method is not or blueprint from which objects are
provides a default provided by the created.
constructor if you compiler in any case.
o Sub Class/Child Class: Subclass is a class
don't have any
constructor in a which inherits the other class. It is also
class. called a derived class, extended class, or
Programmer p=new Programmer(); On the basis of class, there can be three types of
inheritance in java: single, multilevel and
System.out.println("Programmer salary
hierarchical.
is:"+p.salary);
System.out.println("Bonus of Programmer
is:"+p.bonus);
class Animal{
void eat(){
System.out.println("eating...");
}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");
}
}
class TestInheritance{
public static void main(String args[]){
As displayed in the above figure, Programmer is
Dog d=new Dog();
the subclass and Employee is the superclass. The
d.bark();
relationship between the two classes is
d.eat();
Programmer IS-A Employee. It means that
}
Programmer is a type of Employee.
}
2) Multilevel Inheritance Dog and Cat classes inherits the Animal
When there is a chain of inheritance, it is class, so there is hierarchical inheritance.
known as multilevel inheritance. As you
can see in the example given below, class Animal{
BabyDog class inherits the Dog class void eat(){
which again inherits the Animal class, so System.out.println("eating...");
there is a multilevel inheritance. }
}
class Animal{ class Dog extends Animal{
void eat(){ void bark(){
System.out.println("eating..."); System.out.println("barking...");
} }
} }
class Dog extends Animal{ class Cat extends Animal{
void bark(){ void meow(){
System.out.println("barking..."); System.out.println("meowing...");
} }
} }
class BabyDog extends Dog{ class TestInheritance3{
void weep(){ public static void main(String args[]){
System.out.println("weeping..."); Cat c=new Cat();
} c.meow();
} c.eat();
class TestInheritance2{ }
public static void main(String args[]){ }
BabyDog d=new BabyDog();
d.weep();
d.bark(); D. POLYMORPHISM
d.eat(); There are two types of Polymorphism:
} i. Runtime Polymorphism
} Example: Method Overriding,
Upcasting
3) Hierarchical Inheritance Method Overriding
When two or more classes inherits a single If subclass (child class) has the same
class, it is known as hierarchical method as declared in the parent class,
inheritance. In the example given below, it is known as method overriding in
Java.
In other words, If a subclass provides ii. Compile time Polymorphism
the specific implementation of the Example: Method Overloading,
method that has been declared by one Constructor Overloading
of its parent class, it is known as
method overriding. Method Overloading
If a class has multiple methods having
//Java Program to illustrate the use of Java Method
same name but different in parameters,
//Overriding it is known as Method Overloading.
} return a+b;
void run(){ }
obj.run(); System.out.println(Adder.add(12.3,12.6));
//calling method }
} }
}
Constructor Overloading System.out.println("running safely");
Constructor overloading in Java is a
}
technique of having more than one
constructor with different parameter public static void main(String args[]){
lists. They are arranged in a way that Bike obj = new Honda4();
each constructor performs a different
obj.run();
task. They are differentiated by the
compiler by the number of parameters }
in the list and their types.
}
void run(){
interface Printable{
Abstract class Interface
void print();
1) Abstract class Interface can have only
} can have abstract abstract methods.
and non- Since Java 8, it can
abstract methods. have default and
static methods also.
interface Showable extends Printable{
2) Abstract Interface supports
void show();
class doesn't multiple inheritance.
} support multiple
inheritance.
I also learnt about Object Oriented Programming At last, I end up thanking everyone who helped in
in Java, in which I learnt Interface, Classes & doing this Industrial Training.
Objects, Polymorphism, Inheritance & Abstract
Class.