0% found this document useful (0 votes)
7 views

Core Java Training

The document provides a comprehensive overview of Core Java, detailing its history, features, and the differences between Java and C/C++. It explains key concepts such as the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM), along with object-oriented programming principles. Additionally, it includes a sample application program for a Library Management System, demonstrating practical Java coding techniques.

Uploaded by

dotabotbot2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Core Java Training

The document provides a comprehensive overview of Core Java, detailing its history, features, and the differences between Java and C/C++. It explains key concepts such as the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM), along with object-oriented programming principles. Additionally, it includes a sample application program for a Library Management System, demonstrating practical Java coding techniques.

Uploaded by

dotabotbot2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Core Java Training

By:-
Siddhant Rustagi
Overview of programming with Java
Computer Programming is the process of
designing, writing, testing, debugging, and
maintaining the source code of computer
programs. This source code is written in one or
more programming languages (such as Java, C++,
C#, Python, etc.). The purpose of programming is
to create a set of instructions that computers use
to perform specific operations or to exhibit desired
behaviors. The process of writing source code
often requires expertise in many different subjects,
including knowledge of the application domain,
specialized algorithms and formal logic.
Details on Java
 Java was conceived by James Gosling, Patrick Naughton,
Chris Warth, Ed Frank and Mike Sheridan at Sun
Microsystems, Inc in 1991. Initially it was called Oak, later
it was changed to Java in 1995.
 Java's primary motivation was platform independence
and not internet. Platform independence means the ability
to write a program on one platform and run it on any other
platform without worrying about the platform specific
features, which is nothing but a portable software. If it
works on one platform, it should work on all other
platforms. e.g., platforms are Unix, Mac, Windows, Android.
The main motive behind Java invention is to introduce a
feature called Java Portability.
Why Java and not C/C++?
 Platform independence could be achieved for a C/C++
program, by compiling it for every platform. But C/C++
compilers are expensive to build and also there could be
differences between platforms which causes inconsistencies
in the program execution. So achieving portable software
using C/C++ is difficult and error prone. This is also referred
as WOCA = Write Once, Compile Anywhere.
Why Java and not C/C++?
A simpler cost effective solution is to use portable, platform
independent code that runs on variety of CPU's under
different environments. Java uses Java Bytecode, which is a
intermediate set of instructions, that helps achieving this
platform independence. This is also referred as WORA =
Write Once, Run Anywhere.
Java and C/C++ Relation
 Java is not 'Internet version of C++'. Although Java is similar to C
and C++, Java is not designed to replace them. Java uses the syntax
of C and improves on the Object oriented (OO) concepts of C++. But
there is additional support in Java for internet programming. Also
Java and C++, are not code compatible, which means code written in
one language can not be compiled in the other language. So if we
want to change a program from C++ to Java, it has to be completely
rewritten. But programmers experienced with C and C++ can easily
learn Java and rewrite the code.
Java and C/C++ will continue to coexist, since Java solves certain
problems and C/C++ solves certain other problems.
 Java and C# connection Java's innovative features, constructs and
concepts have become part of baseline of any new language. C#
created by Microsoft is very similar to Java. This is a good example
which emphasizes the importance and usage of Java as a computer
programming language.
Certain Terms in Java
 Java Development Kit (JDK):
 Java Development Kit contains two parts. One part contains the
utilities like javac, debugger, jar which helps in compiling the
source code (.java files) into byte code (.class files) and debug the
programs. The other part is the JRE, which contains the utilities
like java which help in running/executing the byte code. If we want
to write programs and run them, then we need the JDK installed.
 Java Compiler (javac):
 javac is the executable/application which compiles the .java source
files into the byte code (.class files). javac is included in JDK.
 Java Run-time Environment (JRE):
 Java Run-time Environment helps in running the programs. JRE
contains the JVM, the java classes/packages and the run-time
libraries. If we do not want to write programs, but only execute the
programs written by others, then JRE alone will be sufficient.
Certain Terms of Java Compiler
 Java Virtual Machine (JVM):
 Java Virtual Machine is important part of the JRE, which actually runs the
programs (.class files), it uses the java class libraries and the run-time
libraries to execute those programs. Every operating system(OS) or platform
will have a different JVM.
 Java Executable(java):
 java is the application which runs/executes the .class files. This internally
calls the JVM for running the programs.
 Just In Time Compiler (JIT):
 JIT is a module inside the JVM which helps in compiling certain parts of
byte code into the machine code for higher performance. Note that only
certain parts of byte code will be compiled to the machine code, the other
parts are usually interpreted and executed.
 Java is distributed in two packages - JDK and JRE. When JDK is installed it
also contains the JRE, JVM and JIT apart from the compiler, debugging tools.
When JRE is installed it contains the JVM and JIT and the class libraries.
Features of Java
Simple
Object Oriented
Robust
Multi Threading
Architecture Neutral
Interpreted and High Performance
Distributed
Dynamic
OOP Concepts and Java Concepts
 Objects and Classes • Exception Handling
 Inheritance
• Sychronization
 Encapsulation
• Java Networking
 Abstraction
 Interfaces
• Collection
 Packages Framework
 Generics • Applets
 Iterators
 Autoboxing and Unboxing
 Enumerations
 Serialization
 Java I/O Stream
 Multithreading
Why multiple inheritance is not supported in Java?

To remove ambiguity.


To provide more maintainable and clear
design.
Things You might not remember
 extends keyword is used to inherit a class or inherit an interface
 Class extends class and interface extends interface, no
class/interface can extend interface/class.
 You cant use this() and super() in a constructor

| Class | Package | Subclass | Subclass | World


| | |(same pkg)|(diff pkg)|
————————————+———————+—————————+——————————+——————————+————————
public | + | + | + | + | +
————————————+———————+—————————+——————————+——————————+————————
protected | + | + | + | + | o
————————————+———————+—————————+——————————+——————————+————————
no modifier | + | + | + | o | o
————————————+———————+—————————+——————————+——————————+————————
private | + | o | o | o | o
+ : accessible
o : not accessible
Method Overloading and Method Overriding
Method Overloading Method Overriding

Parameter must be different and name must be same. Both name and parameter must be same.

Compile time polymorphism. Runtime polymorphism.

Increase readability of code. Increase reusability of code.

Access specifier most not be more restrictive than original


Access specifier can be changed.
method(can be less restrictive).

Q. Can we Override static


method ? Explain with
reasons ?
No, we cannot override
static method. Because
static method is bound to
class whereas method
overriding is associated
with object i.e at runtime
Q. Difference between Static binding and Dynamic
binding in java ?
Static binding in Java occurs during compile
time while dynamic binding occurs during
runtime. Static binding uses type(Class)
information for binding while dynamic
binding uses instance of class(Object) to
resolve calling of method at run-time.
Overloaded methods are bonded using static
binding while overridden methods are bonded
using dynamic binding at runtime.
Sample Application Program

Console based Library Management System

It is a java program used to store information on books in


various libraries and is a very helpful system in case a
university like SRM has libraries distributed in different
states as only one management system can manage the
books in all the libraries.

The current sample only stores ISBN, name of book,


author and price only and uses concepts of OOPS and Java.
The program creates a serializable library file containing
each library’s information.
Book.java
 import java.io.Serializable;

 public class Book implements Serializable{



 private int isbn;
 private String title, author;
 private double price;

 public Book(){
 isbn = 0;
 title = null;
 author = null;
 price = 0;
 }

 public Book(int isbn, String title, String author, double price){
 this.isbn = isbn;
 this.title = title;
 this.author = author;
 this.price = price;
 }

 @Override
 public String toString(){
 return "\nTitle: " + title + "\nAuthor: " + author +
 "\nISBN: " + isbn + "\nPrice: " + price + "\n";
 }
Library.java
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;

 public class Library extends Object implements Serializable {



 private List<Book> collection;

 public Library(){
 collection = new ArrayList<Book>();
 }

 public void addBook(Book book){
 collection.add(book);
 }

 @Override
 public String toString() {
 String total = "\n";
 /*
 for (int i=0; i<collection.size(); i++){
 Book b = collection.get(i);
 total = total + b.toString();

 }
 */
 Iterator<Book> i = collection.iterator();
 while(i.hasNext()){
 Book b = (Book) i.next();
 total = total + b.toString();
 }
 return total;
 }
MainSystem.java
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.Scanner;

 public class MainSystem {

 static String fileName = null;


 static Library lib = new Library();
 static Scanner in = new Scanner(System.in);
 static Boolean running = true;

 public static void main(String[] args) {


 while (running) {
 System.out.println("\nEnter 0 for load a library."
 + "\nEnter 1 for save and quit"
 + "\nEnter 2 for list all books in library"
 + "\nEnter 3 for add book to library");

 int answer = in.nextInt();


 switch (answer) {
 case 0:
 System.out.println("Enter the file name to load");
 loadScript(in.next());
 break;

 case 1:
 saveAndQuit();
 break;
 case 2:
 System.out.println(lib.toString());
 break;
 case 3:
 addBook();
 break;
 private static void addBook() {
 // TODO Auto-generated method stub
 int isbn;
 String title, author;
 double price;

 System.out.println("\nEnter Title: ");


 title = in.next();

 System.out.println("\nEnter Author: ");


 author = in.next();

 System.out.println("\nEnter ISBN: ");


 isbn = in.nextInt();

 System.out.println("\nEnter Price: ");


 price = in.nextDouble();

 Book b = new Book(isbn, title, author, price);


 lib.addBook(b);
 }
 private static void saveAndQuit() {
 // TODO Auto-generated method stub
 System.out.println("Enter file name: ");
 fileName = in.next() + ".ser";
 running = false;
 FileOutputStream fos = null;
 ObjectOutputStream out = null;
 try {
 fos = new FileOutputStream(fileName);
 out = new ObjectOutputStream(fos);
 out.writeObject(lib);
 fos.close();
 out.close();
 } catch (FileNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 private static void loadScript(String name) {
 // TODO Auto-generated method stub
 FileInputStream fis = null;
 ObjectInputStream in = null;
 File file = new File(name + ".ser");
 if (file.exists()) {
 try {
 fis = new FileInputStream(file);
 in = new ObjectInputStream(fis);
 lib = (Library) in.readObject();
 fis.close();
 in.close();

 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 } else {
 System.out.println("\nThe file does not exist!");
 }
 }

 }
Output

You might also like