Professional Documents
Culture Documents
ON
CORE JAVA
Submitted By
KAPIL KUMAR VERMA,
Submitted To
GOVERNMENT LEATHER INSITITUTE,
AGRA-06
(2018-2019)
Page | 1
Acknowledgement
The Training opportunity I had with D.S.C.S Pvt. Ltd. was a great chance for
learning and professional development. Therefore, I consider myself as a very
lucky individual as I was provided with an opportunity to be a part of it. I am
also grateful for having a chance to meet so many wonderful people and
professionals who led me though this training period.
Bearing in mind previous I am using this opportunity to express my
deepest gratitude and special thanks to the HR of D.S.C.S India who in spite of
being extraordinarily busy with her/his duties, took time out to hear, guide and
keep me on the correct path and allowing me to carry out my project at their
esteemed organization and extending during the training.
Above all no words can express my feelings to my parents, friends all
those persons who supported me during my project.
I am also thankful to all the respondents whose cooperation & support has
helped me a lot in collecting necessary information.
Page | 2
DECLARATION
I sincerely declare that I am the sole writer of this report. The details of training
and experience contain in this report describe my involvement as a trainee in the
field of Computer Science & Engineering. All the information contains in this
report is certain and correct to the knowledge of the author.
Page | 3
)(
()
Page | 4
Preface
I have made this report file on the topic Core Java; I have tried my best to
elucidate all the relevant detail to the topic to be included in the report. While in
the beginning I have tried to give a general view about this topic.
My efforts and wholehearted co-corporation of each and everyone has ended on
a successful note. I express my sincere gratitude to Mr.Avinash Srivatava who
assisting me throughout the preparation of this topic. I thank him for providing
me the reinforcement, confidence and most importantly the track for the topic
whenever I needed it.
Page | 5
Contents
Topic Name Page No
01. Introduction of Java………………………………… 07
02. Feature of java……………………………………… 08
03. Java and C……………………………………….…. 11
04. Java and C++…………………………………….…. 11
05. Object Oriented Principles……………………….…. 12
06. Classes and Objects…………………………….…… 14
07. Data Types……………………………………..……. 15
08. Constructor.....................................................…...... 18
09. Array…………………………………………...……. 22
10. Polymorphism ………………………...……………. 25
11. Inheritance………………………………………….. 26
12. Interface……………………………………………... 30
13. Nested Class………………………………………… 31
14. Java Package………………………………………… 33
15. Wrapper Classes…………………………………….. 38
16. Conclusion……………………………………... 40
Page | 6
INTERODUCTION OF JAVA
Page | 7
Features of JAVA
1.Platform Independence:-
The concept of Write-once-run-anywhere (known as the Platform
independent) is one of the important key feature of java language that
makes java as the most powerful language. Not even a single language is
idle to this feature but java is closer to this feature. The programs written
on one platform can run on any platform provided the platform must have
the JVM.
2. Simple:-
There are various features that makes the java as a simple language.
Programs are easy to write and debug because java does not use the
pointers explicitly. It is much harder to write the java programs that can
crash the system but we can not say about the other programming
languages. Java provides the bug free system due to the strong memory
management. It also has the automatic memory allocation and reallocation
system.
3. Object Oriented:-
To be an Object Oriented language, any language must follow at least the
four characteristics.
a.) Inheritance: It is the process of creating the new classes and using the
behavior of the existing classes by extending them just to reuse the existing code
and adding the additional features as needed.
b.) Encapsulation: It is the mechanism of combining the information and
providing the abstraction.
c.) Polymorphism: As the name suggest one name multiple form,
Polymorphism is the way of providing the different functionality by the
functions having the same name based on the signatures of the methods.
d.) Dynamic binding: Sometimes we don't have the knowledge of objects
about their specific types while writing our code. It is the way of providing the
maximum functionality to a program about the specific type at runtime.
4. Robust:-
Page | 8
Java has the strong memory allocation and automatic garbage collection
mechanism. It provides the powerful exception handling and type checking
mechanism as compare to other programming languages. Compiler checks
the program whether there any error and interpreter checks any run time
error and makes the system secure from crash. All of the above features
makes the java language robust.
5. Distributed:-
The widely used protocols like HTTP and FTP are developed in java.
Internet programmers can call functions on these protocols and can get
access the files from any remote machine on the internet rather than
writing codes on their local system. .
6. Portable:-
The feature Write-once-run-anywhere makes the java language portable
provided that the system must have interpreter for the JVM. Java also have
the standard data size irrespective of operating system or the processor.
These features make the java as a portable language.
7. Performance:-
Java uses native code usage, and lightweight process called threads. In the
beginning interpretation of byte coderesulted the performance slow but the
advance version of JVM uses the adaptive and just in time compilation
technique that improves the performance.
8. Multithreaded:
Java is a Multithreaded programming language. Multithreading means a
single program having different threads executing independently at the
same time. heads execute instructions according to the program code in a
process or a program. Multithreading works the similar way as multiple
processes run on one computer. Multithreading programming is a very
interesting concept in Java. In multithreaded programs not even a single
thread disturbs the execution of other thread. Threads are obtained from
the pool of available ready to run threads and they run on the system
CPUs.
Page | 9
We all know that Java is an interpreted language as well. With an
interpreted language such as Java, programs run directly from the source
code. The interpreter program reads the source code and translates it on the
fly into computations. Thus, Java as an interpreted language depends on an
interpreter program. The versatility of being platform independent makes
Java to outshine from other languages. The source code to be written and
distributed is platform independent. Another advantage of Java as an
interpreted language is its error debugging quality. Due to this any error
occurring in the program gets traced. This is how it is different to work
with Java.
Page | 10
Java and C
Java does not include keywords like goto ,sizeof and typedef.
Java does not contain data types like struct, union and enum.
Java does not define the type modifiers keywords like auto, register,
signed.
Java does not support an explicit pointer type.
Java does not have preprocessor.
Java does not support variables arguments to functions.
Java adds many features required for Object-Oriented Programming.
Page | 11
Object Oriented Principles
The object oriented programming languages provide mechanisms that help you
implement the object-oriented model.
2) Inheritance:-
Inheritance is the process by which one object acquires the properties of another
object. This is important because it supports the concept of hierarchical
Page | 12
classification. Most knowledge is made by hierarchical classification.
Inheritance is a compile-time mechanism in Java that allows you to extend a
class (called the base class or super class) with another class (called the derived
class or subclass).
In Java, inheritance is used for two purposes:
Class inheritance - create a new class as an extension of another class,
primarily for the purpose of code reuse. That is, the derived class inherits
the methods of the base class.
Interface inheritance - create a new class to implement the methods
defined as part of an interface for the purpose of sub typing. That is a class
that implements an interface “conforms” to the interface.
In Java, unlike C++, these two types of inheritance are made distinct by using
different language syntax. For class inheritance, Java uses the keyword extends
and for interface inheritance Java uses the keyword implements. In Java, unlike
C++, only single class inheritance is supported. I.e., for a given class, there is
only one super class.
3) Polymorphism:-
Polymorphism is the capability of an action or method to do different things
based on the object that it is acting upon. This is the third basic principle of
object-oriented programming.
Two types of polymorphism:
Overloading and Overriding.
Dynamic method binding
Page | 13
6. Classes and Objects
A class defines user defined objects and their characteristics. Any concept need
to implement in a java program is encapsulated within a class. A class defines
the attributes and methods of objects of the same type sharing common
characteristics. The main components of classes are:
Data members (attributes)
Methods
Classes contain statements that include the declaration of data members, which
specify the type of data to be stored. Methods of class contain a set of executable
statements that gives a desired output.
Creating Classes in Java:-
Code:
classClassName
{
//Declaration of data members
//Declaration of methods
}
Page | 14
7. Data Types
Variables are nothing but reserved memory locations to store values. This means
that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, you can store integers, decimals, or characters
in these variables.
There are two data types available in Java:
Primitive Data types
Reference/Object Data types
Byte:
o Byte data type is an 8-bit signed two's complement integer
o Default value is 0
o Byte datatype is used to save space in large arrays, mainly in place of
integers, since a byte is four times smaller than an integer
Short:
o Short datatype is a 16-bit signed two's complement integer
Int:
o Int datatype is a 32-bit signed two's complement integer
o Integer is generally used as the default data type for integral values
unless there is a concern about memory.
Long:
o Long datatype is a 64-bit signed two's complement integer
o Default value is 0L
Float:
o Float datatype is a single-precision 32-bit IEEE 754 floating point
o Float is mainly used to save memory in large arrays of floating point
numbers
o Default value is 0.0f
o Float datatype is never used for precise values such as currency
o Example: float f1 = 234.5f
Page | 16
Double:
o double datatype is a double-precision 64-bit IEEE 754 floating point
o This datatype is generally used as the default data type for decimal
values, generally the default choice
Page | 17
8. Constructor
Constructor is the method of special type because it has got the same name of
the class and it is not having any return type.
More over there is no need to call the constructer manually because constructer
gets automatically call while we create an object of the class.
The primary use of the constructer is for initialization.
Types of constructor:
Non-parameterized constructor
Parameterized constructor
Default constructor
Non-parameterized constructor Example:-
classCustmer
{
String name;
int salary;
Custmer()
{
name="rahulsingh";
salary=30000;
}
void display()
{
System.out.println("Hello;my name is "+ name);
System.out.println("My salary"+ salary);
}
public static void main(String args[])
{
Custmerobj = new Custmer();
obj.display();
}
}
Page | 18
Parameterized Constructor:-
Are those constructor those take the parameter. Parameterized constructer
basically use in that time while we pass information at the time of object
creation.
1. In java static thing and function always stored in the class area and in the
method area.
2. Function and function variable (local variable ,stack variable , automatic
variable ) always get execute in the stack variable.
3. In java every object (instance ) and object variable ( instance variable)
always created in the heap and heap variable.
Program for parameterized constructor:-
class Customer
{
String name;
int age;
Customer(String n, int a)
}
void display()
{
System.out.println(“hello my name is ”+name);
System.out.println(“and my age is”+age);
}
static public void main(String args[])
{
Customer obj1 = new Customer(“Rahul singh”,23);
Customer obj2 = new Customer(“kapil”,22);
obj1.display();
obj2.display();
}
}
“This”Keyword :-
Page | 19
This is the pre-define keyboard with the help of that we refer the instance
variable.
name=name (where name can be any name)
this.name = name
Note:-
i. This can be used to refer current class instance variable.
ii. Instance variable are within class or outside the object.
iii. Constructer always refers address of the obj.
iv. This can be use to invoke current class method.
Page | 20
that child classes can be anywhere , it means child class can be
inside the package or outside the package.
(iv) Default:- it is almost similar to the protected access specifier because
default thing is also accessible anywhere only within the same package
but they can’t be accessible outside the package in any situation.
Note- (1).we can only use public and default access specifier before the class
, it means we can use private and protected before the class .
(2).In java class name and file name can be different but we always compile
the program with the file name and execute with the class name.
(3).In java if our class is public in that case file name and class name must be
same.
Non access specifier:-Non access specifier or defined how the data
member (variable and method )are used in other classes .
Commonly use Non access specifier is as follow.
1. static
2. final
3. synchronized
4. abstract
5. Native etc.
Static- Static keyword can be use before the variable, function, static block
and static nested class (inner class).
We cannot use the static keyword before the outer class.
Static Variable and static function:-
There is no need to create an
object of the class to access the static variable and static method.
Page | 21
9. Array
Array is a special kind of variable because in that we can store the more than
one value of similar data type.
1. Array in fix in size
2. Array is reference type.
Types of Array
In java array is in 2 categories.
1. Single dimensional array
2. Multi dimensional array
Three phase of array-
1. Declaration of an array-
int arr[]; []int arr; //wrong
int []arr; Int arr[5]; //wrong
int[] arr;
In java at the of declaration we cannot give the size
2. Instantiation of an array / construction of an array [second
type to array declaration]-
int arr[] = new int[5]; //must
int arr[] = new int[]; // wrong
at the time of construction of array we have to give the size .
3. Initialization (populating)-
int arr[] = {10,20,30,40};
Multi Dimensional Array:- in multi dimension array data store in row and
column.(also known as matrix form)
Syntax:- datatype [][]arr;
datatype[][] arr;
datatype arr[][];
datatype []arr[];
Copy Arrays:- the system class has an arraycopy method that we can use too
efficiently copy data from array into another .
Syntax:-
Page | 22
public static void arraycopy(Object src, intPos, Object dest, intdeskPos , int
length)
String Handling -
string in java are not null terminated arrays of characters . instead
, they are instances of the java.lang.String class. Internally, a string incapsulates
an array of characters .A String is a immutable ,which means that after you
assigned your String object a value , it can not change any more during its life
time.
Note:- string is a class, not a datatype in java.
String creation:- there are two ways to create an String object.
1. Implicitly
2. Explicitly
1. Implicitly:-when you use a String literal , like " Hello World" ,java
automatically creates a String object for you.
String s = "Hello world !"
2. Explicitly:-when we use the new operator to instanciate a String object.
String s = new String("Hello world !");
Important String Operations:-
1. String length:- use the length method from the String class
2. Character array to String conversion-
3. String to array conversions-
Example:-
Class StringArray
{
public static void main(String args[])
{
String s = "hello world";
chararr[] = s.toCharArrayI();
for(int i=0 ; i<arr.length ; i++)
{
System.out.println(arr[i]);
System.out.println();
}
Page | 23
}
}
4. String lower case to upper case:-
Example:-
class Text
{
public static void main(String args[])
{
String s = "Hello World !";
String supper = s.toUpperCase();
String slower = s.toLowerCase();
System.out.println(supper);
System.out.println(slower);
}
}
5.Concatenate to String ( addition of 2 string):-
String s1 = "Hello"; String s2 = "world";
String s3 = "s1.concat(s2)"; // or s1+s2
6. Comparison of two string –
Example:-
Class Ravi
{
public static void main(String...args)
{
String a = "Text";
String b = "Text";
If (a. Equals (b){
System.out.println("a is equal to b");
}
else
{
System.out.println("a is not equal to b");
}
Page | 24
10. Polymorphism
Polymorphism is an important feature of the object oriented programming
language
This word is inherited form two Latin word –
1. Poly-which means many.
2. Morph- which means forms.
With the help of the polymorphism we provide the multiple interface for a single
function.
Types of polymorphism-
1. Static polymorphism(compile time)
2. Dynamic polymorphism ( at run time )
Static polymorphism always happens with reference where as dynamic
polymorphism always happen with object.Reference always created at time of
compilation where as object always is created at run time.
Types of signature-
1. Number of parameter-
void sum(int , int )
void sum(int,int,int)
2. Data type of parameter-
void sum(int,int)
void sum(int,float)
Page | 25
11. Inheritance
Inheritance is an feature of an object oriented programming language
.Inheritance enables a class to inherit the variable and function from the another
class.
The class that inherit the variables and function from the another class is known
as the sub-class or derived class or child class. Whereas the from which the sub
class the inherit the data is known as the super class or base class or parent class.
Types of inheritance-
There are various types of inheritance we have in object oriented programming
language .
1. Single level inheritance:-
When a class is inherited from the single super class the process is known as
single level inheritance.
Class A
Method:-
Class B
Class A
{
}
class B extends class A
{
}
class C extends class A
{
}
class a
class b
class c
Method:-
Class a
{
}
Class extends class a
{
}
Class b extends class b
{
}
Multiple inheritance-
When one class is inherited from the more than one class that process is known
is multiple inheritances.
// in java one class cannot inherit the multiple classes simultaneously.
simultaneously
Class A Class b
Class c
Abstract Keyword:-
Abstract is a keyword it can be use with class, with function but we
can not use the abstract keyword before the variable.
Abstract class can only be inherited they can not be instantiated (it
means we can only inherited abstract class we can not create an
object of it).
Abstract method are those method , these are not having the body ,
they must be followed by semi colon .
Abstract class can have the partial information it means abstract
class can have both method.
1. Abstract method(without body)
2. Non-abstract method (with body)
when a class inherit the abstract class in that case concrete class has to define the
body of the class on abstract method of the abstract class
Final Keyword:-
1. the final keyword can use with fuction , with variable and with class.
2. final class can not be inherited . it can only be instantiated (creating
object).
3. We can not override the final method .
4. We can not change the final variable.
final class Xyz
{
}
Class Text extends Xyz
{
}
Page | 28
Final variable:-to create constant variable
Final method:-prevent method overriding
Final class:- prevent inheritance
Super function:-
Super function always calls parent constructer only.
The constructer first statement always in the super function whether we
write it or not.
In java we do not inherit any class in that time one class automatically
inherited that is object.
finalize method():-
it is the method that the garbage collector always calls just before the
deletion destroying the object which is eligible for garbage collection so as
to perform clean up activity.
Page | 29
12. Interface
Interface is the 100 percent abstract class because it can only contain the
abstract method . it means we can not have the method in the interface
those have the body .Interface can contain only two things
1. Abstract method Class Extends Class
2. Public static Interface Extends Interface
Class Implements interface
final variable To create an interface we use interface keyword
Interface enter
{
Public static final variable;
Public abstract method();
}
o In java all constant (public static final variable) always comes in the
capital letter.
o If a class want to use an interface in that case we use the implements
keyword.
o In java one class can inherit only one class but it can implements
more than one interface .
o If a class is implementing an interface in that case class has to define
the body of the all method.
Interface implements class // wrong
Page | 31
Regular inner class cannot contain the static declaration that is why we
never have the main method in the regular inner class.
In the case of regular inner class inheritance is not allow, it means we
cannot directly inherit the inner class.
Anonymous class-
An inner class declared without a class name is known as anonymous inner
class.
In case of anonymous inner class we declared and instantiated then at same
time generally they are used whenever you need to override the method of a
class or an interface.
Page | 32
14. Java Package
A java package is a group of similar types of classes, interfaces and sub-
packages.
Package in java can be categorized in two form, built-in package and user-
defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.
Here, we will have the detailed learning of creating and using user-defined
packages.
1) Java package is used to categorize the classes and interfaces so that they can
be easily maintained.
Page | 33
How to access package from another package:-
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
1) Using packagename:-
If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
The import keyword is used to make the classes and interface of another
package accessible to the current package.
//save by A.java
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
Programme:-
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
Page | 34
}
}
2) Using packagename.classname:-
If you import package.classname then only declared class of this package will be
accessible.
Package:-
//save by A.java
package pack;
public class A
{
public void msg(){
System.out.println("Hello");
}
}
Programme:-
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Page | 35
3) Using fully qualified name
If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Package:-
//save by A.java
package pack;
public class A{
public void msg()
{
System.out.println("Hello");
}
}
Programme:-
//save by B.java
package mypack;
class B{
public static void main(String args[])
{
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Page | 36
If you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence, you
need to import the subpackage as well.
Note: Sequence of the program must be package then import then class.
Subpackage in java:-
Page | 37
15. Wrapper classes
Wrapper class in java provides the mechanism to convert primitive into object
and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object
and object into primitive automatically. The automatic conversion of primitive
into object is known as autoboxing and vice-versa unboxing.
The eight classes of java.lang package are known as wrapper classes in java.
All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are
subclasses of the abstract class Number.
Boolean Boolean
Char Character
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double
Page | 38
Boxing:-
The conversion of primitive data to object type is called boxing.
Example:-
int a = 200;
Integer b = new Integer(a);
// this is known as boxing.
Unboxing:-
Converting an object of a wrapper type to its corresponding primitive value is
called unboxing. For example conversion of Integer to int. The Java compiler
applies unboxing when an object of a wrapper class is:
Passed as a parameter to a method that expects a value of the corresponding
primitive type.
Assigned to a variable of the corresponding primitive type.
Example:-
C = b.intValue();
// unboxing
Autoboxing:-
Converting a primitive value into an object of the corresponding wrapper class is
called autoboxing. For example, converting int to Integer class. The Java
compiler applies autoboxing when a primitive value is:
Passed as a parameter to a method that expects an object of the
corresponding wrapper class.
Assigned to a variable of the corresponding wrapper class.
Example:-
int a = 200;
Integer b = a;
// this is known as Autoboxing.
Int c = b;
Page | 39
16. CONCLUSION
After complete my industrial training, I had been exposed to an IT
technical and programmer working life. Throughout my internship, I could
understand more about the definition of an IT responsible and innovative
technician and programmer in future. Along my training period, I realize that
observation is a main element to find out the root cause of a problem. Not only
for my project but daily activities too. During my project, I cooperate with my
colleagues and operators to determine the problems Moreover; the project
indirectly helps me to learn.
Independently, discipline myself ,be considerate/patient, self-trust, take
initiative and the ability to solve problems, besides my communication skills is
strengthen as well when communicating with others. During my training period,
I have received criticism and advice from engineers and technician guidance for
me to change myself and avoid myself making the same mistakes again. Apart
from that, I had also Learn my programming skills through various programs
that I had done. This also helps sharpen my skills in html , css since most of the
programs were done with In sum, the activities that I had learned during
industrial training really are useful for me in future to face challenges in a
working environment. Throughout the industrial training, I found that several
things are important.
Time Management
As overall technician and programmer are always racing against tight timeline
and packed schedule, a proper time management will minimize facing overdue
deadlines. An effective time management allows us to do our assignment
Page | 40
efficiently and meet our schedules , Scheduling avoids time wastage and allows
us to plan ahead, and gaining more as a result.
Opposing to a Herculean goal seemed to be reachable at first sight it is
better to sub-divide the goals to a few achievable tasks, so that we will be
gaining more confidence by accomplishing those tasks.
Colleague Interactions
In working environment, teamwork is vital in contributing to a strong
organization. Teamwork is also essential in reaching the goals of the
organization as an entity. Thus, communicating and sharing is much needed in
the working environment. Therefore, together as a team, instead of working
alone. This is because working together as a team is easier in reaching our
targets rather than operating individually.
I would like to once again appreciate everyone who has made my industrial a
superb experience.
(.)
Page | 41