You are on page 1of 31

A course on Core Java

A B-Tech Internship Report


Submitted in partial fulfilment of
the requirements for the
Degree of Bachelor of Technology

Submitted by
Subhransu Sekhar Swain ECE201810346

2020-2021

Under the guidance of

Mr. Harikrushna Gantayat

NATIONAL INSTITUTE OF SCIENCE & TECHNOLOGY


Palur Hills, Berhampur, Odisha - 761008, India
B.Tech. 2018-2022

ABSTRACT

JAVA is Object Oriented Programing Language that is class based and designed
to have few implementation dependencies as possible. One thing that distinguished java
from other language is its ability to run the same compile code across multiple operating
system. In java JVM is a software implementation that execute program like real
machine. JAVA programs are compiled by the java compiler into byte code. The jvm
interprets this byte code and executes the java program. There are many applications of
java in real world. It is used in android apps, server apps, software tools, trading
applications, big data technologies high frequency trading space etc.

There are three access modifiers key words available in java they are (1)Public
(2)Private (3)Protected and four access levels (1)Public (2)Private (3)Protected
(4)Default. They define how the corresponding elements is visible to other components.

i
B.Tech. 2018-2022

ACKNOWLEDGEMENT

It is our proud privilege to epitomize our deepest sense of gratitude and


indebtedness to our advisor, Mr. Harikrushna Gantayat for his valuable guidance, keen
and sustained interest, intuitive ideas and persistent endeavour. His inspiring assistance,
laconic reciprocation and affectionate care enabled us to complete our work smoothly and
successfully.

We would also like to thank Dr. Sukant K. Mohapatra (Chairman, NIST),


Prof. Sangram Mudali (Director, NIST)and Prof. Geetika Mudali (Placement
Director, NIST)for having been the ultimate source of inspiration and moral support.

We would also like to extend our heartfelt gratitude to our parents and friends for
their unflinching support and help.

Subhransu Sekhar Swain

ii
B.Tech. 2018-2022

TABLE OF CONTENTS

1. INTRODUCTION TO JAVA
1. 1. CHARACTERISTICS
1. 2. JVM
1. 3. APPLICATIONS OF JAVA
2. PREREQUISITES
2.1. PACKAGES
2.2. CLASS AND OBJECT
2.3. VARIABLES AND METHODS
2.4. ACCESS MODIFIERS AND IMPORT STATEMENTS
2.5. CONSTRUCTOR
3. INHERITANCE
3.1. WHAT IS INHERITANCE?
3.2. WHY IT IS USED?
3.3. WHERE IT IS USED?
3.4. HOW IT IS DONE?
4. PROS AND CONS OF JAVA
5. CONCLUSION
6. REFERENCE

iii
B.Tech. 2018-2022

INTRODUCTION

With the invention of microprocessors, the world is scientifically


developed with sophisticated equipments, systems, and devices. Microprocessors
are used in computers, televisions, and fax machines. Even the hand-held devices
such as pagers, PDAs (Personal Digital Assistant), and cell phones make use of
microprocessors. All these electronic devices are helpful because of their
communication capabilities. With the increasing capabilities and decreasing cost of
information processing and networking technologies, the network is growing
rapidly for transmitting information through electronic systems.

Internet is the network of networks between different types of computers


located at different places to transmit information. Information can reach to any
place in the world quickly at a cheaper rate through the Internet. Thus, the Internet
has made the world a global village for information exchange. The emerging
infrastructure of electronic devices and interconnected computer networks create
an environment that presents new challenges to software industries. for this
emerging computing environment, Java process to be a well – suited programming
language. it is found suitable for networked environments involving a great variety
of computer and devices.

Java has many characteristics that have contributed to its popularity:

 Platform independence - Many languages are compatible with only one


platform. Java was specifically designed so that it would run on any
computer, regardless if it was running Windows, Linux, Mac, Unix or any
of the other operating systems.

 Simple and easy to use - Java's creators tried to design it so code could be
written efficiently and easily.

iv
B.Tech. 2018-2022

 Multi-functional - Java can produce many applications from command-


line programs to applets to Swing windows (basically, sophisticated
graphical user interfaces).

Java does have some drawbacks. Since it has automated garbage collection, it
can tend to use more memory than other similar languages. There are often
implementation differences on different platforms, which have led to Java being
described as a "write once, test everywhere" system. Lastly, since it uses an
abstract "virtual machine", a generic Java program doesn't have access to the
Native API's on a system directly. None of these issues are fatal, but it can mean
that Java isn't an appropriate choice for a particular piece of software.

Java Virtual machine :-

 The Java virtual machine (JVM) is a software implementation of a computer


that executes programs like a real machine.

 The Java virtual machine is written specifically for a specific operating


system, e.g. for Linux a special implementation is required as well as for
Windows.

 Java programs are compiled by the Java compiler into bytecode. The Java
virtual machine interprets this bytecode and executes the Java program.

Java Runtime Environment vs. Java Development Kit

 A Java distribution comes typically in two flavors, the Java Runtime


Environment (JRE) and the Java Development Kit (JDK).

 The Java runtime environment (JRE) consists of the JVM and the Java class
libraries and contains the necessary functionality to start Java programs.

v
B.Tech. 2018-2022

The JDK contains in addition the development tools necessary to create Java programs.
The JDK consists therefore of a Java compiler, the Java virtual machine, and the Java class
libraries.

Application Of Java :-
There are many places where Java is used in real world, starting from commercial e-
commerce website to android apps, from scientific application to financial applications like
electronic trading systems, from games like Minecraft to desktop applications like Eclipse,
Netbeans, and IntelliJ, from an open source library to J2ME apps etc. Let's see each of them in
more detail.

1) Android Apps

If you want to see where Java is used, you are not too far away. Open your Android
phone and any app, they are actually written in Java programming language, with Google's
Android API, which is similar to JDK. Couple of years back Android has provided much needed
boost and today many Java programmers are Android App developer. By the way android uses
different JVM and different packaging, as we have seen in our previous article about  how
Android app works, but code is still written in Java.

2) Server Apps at Financial Services Industry

Java is very big in Financial Services. Lots of global Investment banks like Goldman
Sachs, Citigroup, Barclays, Standard Charted and other banks use Java for writing front and back
office electronic trading system, writing settlement and confirmation systems, data
processing projects and several others. Java is mostly used to write server side application, mostly
without any front end, which receives data form one server (upstream), process it and sends it
other process (downstream). Java Swing was also popular for creating thick client GUIs for
traders, but now C# is quickly gaining market share on that space and Swing is out of its breath.

3) Java Web applications

Java is also big on E commerce and web application space. You have a lot of  RESTfull
services being created using Spring MVC, Struts 2.0 and similar frameworks. Even simple
Servlet, JSP and Struts based web applications are quite popular on various government projects.

vi
B.Tech. 2018-2022

Many of government, healthcare, insurance, education, defense and several other department have
their web application built in Java.

4) Software Tools

Many useful software and development tools are written and developed in Java e.g.
Eclipse, InetelliJ Idea and Netbans IDE. I think they are also most used desktop .applications
written in Java. Though there was time when Swing was very popular to write thick client, mostly
in financial service sector and Investment banks. Now days, Java FX is gaining popularity but still
it is not a replacement of Swing and C# has almost replaced Swing in Finance domain.

5) Trading Application

Third party trading application, which is also part of bigger financial services industry,
also use Java. Popular trading application like Murex, which is used in many banks for front to
bank connectivity, is also written in Java.

6) J2ME Apps

Though advent of iOS and Android almost killed J2ME market, but still there is large
market of low end Nokia and Samsung handset which uses J2ME. There was time when almost
all games, application, which is available in Android are written using MIDP and CLDC, part of
J2ME platform. J2ME is still popular on products like Blu-ray, Cards, Set top boxes etc. One of
the reason of WhatsApp being so popular is because it is also available in J2ME for all those
Nokia handset which is still quite big.

7) Embedded Space

Java is also big in the embedded space. It shows how capable the platform is, you only
need 130 KB to be able to use Java technology (on a smart card or sensor). Originally Java was
designed for embedded devices. In fact, this is the one area, which was part of Java's initial
campaign of "write once, run anywhere" and looks like it is paying up now.

8) Big Data technologies

Hadoop and other big data technologies are also using Java in one way or other e.g.
Apache's Java-based HBase and Accumulo (open source), and  ElasticSearch as well. By the Java
is not dominating this space, as there are technologies like MongoDB which is written in C++.
Java has potential to get major share on this growing space if Hadoop or ElasticSearch goes big.

9) High Frequency Trading Space

vii
B.Tech. 2018-2022

Java platform has improved its performance characteristics a lot and with modern JITs, its
capable of delivering performance at C++ level. Due to this reason, Java is also popular on
writing high performance systems, because Though performance is little less compared to native
language, but you can compromise safety, portability and maintainability for more speed and it
only takes one inexperienced C++ programmer to make an application slow and unreliable.

10) Scientific Applications

Nowadays Java is often a default choice for scientific applications, including natural
language processing. Main reason of this is because Java is more safe, portable, maintainable and
comes with better high-level concurrency tools than C++ or any other language.

viii
B.Tech. 2018-2022

PREREQUISITES:-
Package
Java groups classes into functional packages.

Packages are typically used to group classes into logical units. For example all graphical
views of an application might be placed in the same package called
com.vogella.webapplication.views.

It is common practice to use the reverse domain name of the company as top level
package. For example the company might own the domain, vogella.com and in this example the
Java packages of this company starts with com.vogella.

Other main reason for the usage of packages is to avoid name collisions of classes. A
name collision occurs if two programmers give the same fully qualified name to a class. The
fully qualified name of a class in Java consists out of the package name followed by a dot (.) and
the class name.

Without packages, a programmer may create a Java class called Test. Another
programmer may create a class with the same name. With the usage of packages you can tell the

system which class to call. For example if the first programmer puts the Test class into package

report and the second programmer puts his class into package xmlreader you can distinguish

between these classes by using the fully qualified name, e.g. xmlreader.Test or report.Test.

Class

Def.: Template that describes the data and behavior associated with an instance of that class.

In Java source code a class is defined by the class keyword and must start with a capital
letter. The body of a class is surrounded by {}.

package test;

class MyClass {

ix
B.Tech. 2018-2022

}
The data associated with a class is stored in variables ; the behavior associated to a class
or object is implemented with methods.

A class is contained in a Java source file with the same name as the class plus the .java
extension.

Object

Def.: An object is an instance of a class.

The object is the real element which has data and can perform actions. Each
object is created based on the class definition.

Variables and methods


 Variable
Variables allow the Java program to store values during the runtime of the
program.

A variable can either be a primitive variable or a reference variable. A primitive


variable contains value while the reference variable contains a reference (pointer) to the
object. Hence if you compare two reference variables, you compare if both point to the
same object. To compare objects use the object1.equals(object2) method call.

 Instance variable
Instance variable is associated with an instance of the class (also called object).
Access works over these objects.

Instance variables can have any access control and can be marked final or transient.
Instance variables marked as final can not be changed after assigned to a value.

x
B.Tech. 2018-2022

 Local variable
Local (stack) variable declarations cannot have access modifiers.

final is the only modifier available to local variables. This modifier defines that the
variable can not be changed after first assignment.

Local variables do not get default values, so they must be initialized before use.

 Methods
A method is a block of code with parameters and a return value. It can be called on
the object.

public class MyMethodExample { void tester(String s) {

System.out.println("Hello World");

Method can be declared with var-args. In this case the method declares a parameter
which accepts from zero to many arguments (syntax: type .. name;) A method can only
have one var-args parameter and this must be the last parameter in the method.

Overwrite of a superclass method: A method must be of the exact same return


parameter and the same arguments. Also the return parameter must be the same.
Overload methods: An overloaded method is a method with the same name, but different
arguments. The return type can not be used to overload a method.

xi
B.Tech. 2018-2022

 Main method

A public static method with the following signature can be used to start a Java
application. Such a method is typically called main method.

public static void main(String[] args){

 Constructor
A class contains constructors that are invoked to create objects based on the class
definition. Constructor declarations look like method declarations except that they use the
name of the class and have no return type. A class can have several constructors with
different parameters. Each class must define at least one constructor. In the following
example the constructor of the class expects a parameter.

package com.vogella.javaintro.base;

public class MyConstructorExample2 {

String s;

public MyConstructorExample2(String s) {

this.s = s;

}
xii
B.Tech. 2018-2022

If no explicit constructor is defined the compiler adds implicitly a constructor. If the


class is sub-classed then the constructor of the super class is always implicitly called in
this case.

In the following example the definition of the constructor without parameters (also
known as the empty constructor) is unnecessary. If not specified the compiler would
create one.

package com.vogella.javaintro.base;

public class MyConstructorExample {

// Unnecessary, would be created by the compiler if left out

public MyConstructorExample() {

The naming conversion for creating a constructor is the following: classname


(Parameter p1, ..) {} .

Every object is created based on a constructor. This constructor method is the first
statement called before anything else can be done with the object.

 Access modifiers
There are three access modifiers keywords available in Java. public, protected and
private. There are four access levels: public, protected, default and private. They define
how the corresponding element is visible to other components. If something is declared
public, e.g. classes or methods can be freely created or called by other Java objects. If
something is declared private, e.g. a method, it can only be accessed within the class in

xiii
B.Tech. 2018-2022

which it is declared. protected and default are similar. A protected class can be accessed
from the package and sub-classes outside the package while a default class can get only
accessed via the same package. The following table describes the visibility:

TABLE : ACCESS LEVEL

Modifie Clas Packag Subclas World


r s e s

Public Y Y Y Y

protected Y Y Y N

Modifie Clas Packag Subclas World


r s e s

no
Y Y N N
modifier

Private Y N N N

Import statements
 Usage of import statements

In Java you have to access a class always via its full-qualified name, e.g. the package

name and the class name. You can add import statements for classes or packages into your class
file, which allow you to use the related classes in your code without the package qualifier.

 Static imports

Static import is a feature that allows members (fields and methods) which are
defined in a class with the public static access modifier to be used in Java code without
specifying the class in which the member is defined. The feature provides a typesafe

xiv
B.Tech. 2018-2022

mechanism to include constants into code without having to reference the class that
originally defined the field.

xv
B.Tech. 2018-2022

INHERITANCE :-
 What is inheritance?

A class can be derived from another class. In this case this class is called a
subclass. Another common phrase is that a class extends another class.

The class from which the subclass is derived is called a superclass.

Inheritance allows a class to inherit the behavior and data definitions of another
class. The following codes demonstrates how a class can extend another class. In Java a
class can extend a maximum of one class.

package com.vogella.javaintro.base;

class MyBaseClass {

void hello(){

System.out.println("Hello from MyBaseClass");

 Why inheritance is used?


 For Method Overriding (so runtime polymorphism can be achieved).

 For Code Reusability.

 Where do we use inheritance in java?


The inheritance mechanism is very useful in code reuse. Classes can be derived
from classes that are derived from classes that are derived from classes, and so on.

xvi
B.Tech. 2018-2022

In java, by default, the Object class is the parent class. In Java inheritance is
declared using the extends keyword.

 Important terminology:

 Super Class: The class whose features are inherited is known as super class(or a
base class or a parent class).

 Sub Class: The class that inherits the other class is known as sub class(or a
derived class, extended class, or child class). The subclass can add its own fields
and methods in addition to the superclass fields and methods.

 Reusability: Inheritance supports the concept of “reusability”, i.e. when we want


to create a new class and there is already a class that includes some of the code
that we want, we can derive our new class from the existing class. By doing this,
we are reusing the fields and methods of the existing class.

 How it is done?

On the basis of class, there can be three types of inheritance in java: single,
multilevel and hierarchical. In java programming, multiple and hybrid inheritance is
supported through interface only. We will learn about interfaces later.

xvii
B.Tech. 2018-2022

1. Single Inheritance : In single inheritance, subclasses inherit the features of one
superclass. In image below, the class A serves as a base class for the derived class
B.

//Java program to illustrate the 

// concept of single inheritance

import java.util.*;

import java.lang.*;

xviii
B.Tech. 2018-2022

import java.io.*;

  

class one

    public void print_geek()

    {

        System.out.println("Geeks");

    }

  

class two extends one

    public void print_for()

    {

        System.out.println("for");

    }

// Driver class

public class Main

    public static void main(String[] args)

    {
xix
B.Tech. 2018-2022

        two g = new two();

        g.print_geek();

        g.print_for();

        g.print_geek();

    }

Output:

Geeks

for

Geeks

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 below image, the 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. In Java, a class
cannot directly access the grandparent’s members.

xx
B.Tech. 2018-2022

// Java program to illustrate the 

// concept of Multilevel inheritance

import java.util.*;

import java.lang.*;

import java.io.*;

  

class one

    public void print_geek()

    {

        System.out.println("Geeks");

    }

xxi
B.Tech. 2018-2022

  

class two extends one

    public void print_for()

    {

        System.out.println("for");

    }

  

class three extends two

    public void print_geek()

    {

        System.out.println("Geeks");

    }

  

// Drived class

public class Main

xxii
B.Tech. 2018-2022

    public static void main(String[] args)

    {

        three g = new three();

        g.print_geek();

        g.print_for();

        g.print_geek();

    }

Output:

Geeks

for

Geeks

3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a


superclass (base class) for more than one sub class.In below image, the class A
serves as a base class for the derived class B,C and D.

xxiii
B.Tech. 2018-2022

// Java program to illustrate the 

// concept of Hierarchical inheritance

import java.util.*;

import java.lang.*;

import java.io.*;

  

class one

    public void print_geek()

    {

        System.out.println("Geeks");

    }

  

class two extends one

    public void print_for()

    {

        System.out.println("for");

    }

xxiv
B.Tech. 2018-2022

  

class three extends one

    /*............*/

  

// Drived class

public class Main

    public static void main(String[] args)

    {

        three g = new three();

        g.print_geek();

        two t = new two();

        t.print_for();

        g.print_geek();

    }

Output:

Geeks

xxv
B.Tech. 2018-2022

for

Geeks

xxvi
B.Tech. 2018-2022

ADVANTAGES OF JAVA

Simple:  Java was designed to be easy to use, write, compile, debug, and learn than other
programming languages. Java is much simpler than C++ because Java uses automatic
memory allocation and garbage collection.

Object-Oriented: Object oriented programming is associated with concepts like class,


object, inheritance, encapsulation, abstraction, polymorphism, etc. which allows you to create
modular programs and reusable code. You can declare classes, create objects inside classes,
and interact between two objects.

Platform-Independent:  Java offers the comfort of write program once and run on any
hardware and software platform and any Java compatible browser. This gives the ability to
move easily from one computer system to another.

Distributed:  Java has great networking capability, it is designed to make distributed


computing easy with the networking capability that is inherently integrated into it.

Secure:  Java is the first programming language to include security an integral part of the
design. Java’s compiler, interpreter, and runtime environment were each developed with
security in mind. Java Virtual Machine has a unique identifier that identifies the bytecode and
verifies it before running it.

Allocation: Java has the feature of Stack allocation system. It follows LIFO (Last in First
Out) which helps the data to be stored and retrieved easily.

Multithreaded:  Java is one of the programming languages to support Multithreading.


Multithreading is the capability for a program to perform several tasks simultaneously within
a program.

Rich APIs: Java offers various APIs for application development. Java APIs (Application
Programming Interface) is the set of commands or methods of communication among various
activities like Database connection, networking, I/O, XML parsing, utilities, and much more.

xxvii
B.Tech. 2018-2022

Powerful Open source Rapid Development Tools: Over the year’s several open source
development tools i.e., IDEs such as Eclipse and Netbeans, have been created with Java as a
base which makes Java more powerful for application development. IDEs makes application
development simpler with powerful coding and debugging features.

Robust: Java is one of the most robust programming languages, that is Java is more reliable.
Java compilers can detect any errors in the coding. There are also other features like
exception handling and garbage collection which makes Java more robust.

Resource Availability: There are tons of online java training courses available to learn java.
You don’t have to spend a fortune to learn java programming.

DISADVANTAGES OF JAVA

Performance: SIgnificantly slower and more memory-consuming than natively compiled


languages such as C or C++.

Look and feel: The default look and feel of GUI applications written in Java using the Swing
toolkit is very different from native applications.

Single-paradigm language: The addition of static imports in Java 5.0 the procedural


paradigm is better accommodated than in earlier versions of Java.

xxviii
B.Tech. 2018-2022

CONCLUSION

This report covers the common advantages and disadvantages to give you an
overview of the Java programming language. Understanding this will help you to learn Java
programming easily. In our Java programming tutorial, we discuss more on how to utilize
these advantages to create wonderful front-end of back-end applications using Java. To learn
more about Java, select from a list of Online Java training courses available and listed below
are some of the best java programming books for beginners and advanced users to maximize
the advantages of Java Programming.

xxix
B.Tech. 2018-2022

REFERENCES
[1] Horstmann, Cay S., and Gary Cornell. Core Java 2: Volume I, Fundamentals. Pearson Education,
2002.
[2] Jeffrey, Alan, and Julian Rathke. "Java Jr: Fully abstract trace semantics for a core Java language."
In European symposium on programming, pp. 423-438. Springer, Berlin, Heidelberg, 2005.
[3] DeCarmo, Linden. Core Java media framework. Prentice Hall PTR, 1999.
[4] Tyagi, Sameer. Core java data objects. Prentice Hall Professional, 2003.

xxx

You might also like