CORE JAVA
Submitted To:- Submitted By:-
Mr. Sanjay Tiwari Astha Rawat
(Head OF Department) 16EAOCS009
Mr. Arpit Sharma
(Seminar Co-ordinator)
WHAT IS JAVA?
Java is a programming language that produces
software for multiple platform.
Java syntax is similar to c++,but strictly an object-
oriented programming language.
Java is a pure programming language.
A programming language
Fully buzzword-compliant:
A simple, object oriented, distributed, interpreted,
robust, secure, architecture neutral, portable, high
performance, multithreaded, dynamic language.
From: Java: An Overview
James Gosling, Sun Microsystems, February 1995.
WHY JAVA?
much more language support
error handling no pointers! (garbage collection)
threads are part of the language.
dynamic class loading and secure sandbox execution for remote code.
source code and bytecode-level portability.
Write once run anywhere.
Every object inherits from java.lang.Object
No code outside of class definition!
No global variables.
All classes are defined in .java files
one top level public class per file
HISTORY OF JAVA
Java was originally developed by James
Gosling at Sun Microsystems and
released in 1995 as a core component of
Sun Microsystems java platform.
FEATURES OF JAVA
It must be simple, object-oriented, and familiar.
It must be robust and secure.
It must be architecture-neutral and portable.
It must execute with high performance.
APPLICATION AREA OF JAVA
DATATYPES
FLOW CONTROL
Basically, it is exactly like c/c++.
do/while
switch
if/else
int i=5;
do { char
If(x==4) { // act1 c=IN.getChar();
// act1 i--; switch(c) {
} else { } while(i!=0); case ‘a’:
// act2 case ‘b’:
} for // act1
break;
int j;
default:
for(int i=0;i<=9;i++)
// act2
{
}
j+=i;
}
WHAT IS JDK
Java Development Kit is the core component of Java Environment and
provides all the tools, executables and binaries required to compile, debug
and execute a Java Program. JDK is a platform-specific software and that’s
why we have separate installers for Windows, Mac, and Unix systems. We
can say that JDK is the superset of JRE since it contains JRE with Java
compiler, debugger, and core classes. The current version of JDK is 11 also
known as java 11.
WHAT IS JVM?
A Java virtual machine (JVM) is a virtual machine that enables a computer to run
Java programs as well as programs written in other languages that are also compiled
to Java bytecode. The JVM is detailed by a specification that formally describes what
is required in a JVM implementation.
WHAT IS JRE?
JRE is the implementation of JVM, it provides a platform to execute java
programs. JRE consists of JVM and java binaries and other classes to execute
any program successfully. JRE doesn’t contain any development tools like java
compiler, debugger etc. If you want to execute any java program, you should
have JRE installed but we don’t need JDK for running any java program .
MAIN CONCEPTS
Object
Class
Inheritance
Encapsulation
OBJECTS
An object has identity
(each object is a distinct individual).
An object has state
(it has various properties,
which might change).
An object has behavior
(it can do things and
can have things done to it).
CLASS
Class is a blue print which is containing only list of variables and method and no
memory is allocated for them. A class is a group of objects that has common properties.
A class in java contains:
Data Member
Method
Constructor
Block
object is an instance of class
class groups similar objects
same (structure of) attributes
same services
object holds values of its class’s attributes
INHERITANCE
Class hierarchy
Java supports all types of Inheritance except multiple inheritance
class Base {
INHERITANCE Base(){}
Base(int i) {}
protected void foo() {…}
Base }
class Derived extends Base {
Derived() {}
protected void foo() {…}
Derived(int i) {
super(i);
Derived …
super.foo();
}
}
As opposed to C++, it is possible to inherit only from ONE class.
Pros avoids many potential problems and bugs.
Cons might cause code replication
ENCAPSULATION
Separation between internal state of the object and its external aspects
Encapsulation= Abstraction + Data hiding
Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates
How ?
control access to members of the class
POLYMORPHISM
The process of representing one form in multiple forms is known as Polymorphism
Polymorphism principal is divided into two sub principal they are:
Static or Compile time polymorphism
(Overloading)
Dynamic or Runtime polymorphism
(Overriding)
HELLO WORLD
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println(“Hello World !!!”); }
}
C:\javac Hello.java ( compilation creates Hello.class )
C:\java Hello (Execution on the local JVM)
ARRAYS
• Array is an object
•Collection of homogeneous data types
•Contiguous memory allocation
• Array size is fixed
•Elements are stored on index basis
Animal[] arr; // nothing yet …
arr = new Animal[4]; // only array of pointers
for(int i=0 ; i < arr.length ; i++) {
arr[i] = new Animal();
// now we have a complete array
ARRAYS - MULTIDIMENSIONAL
In C++
Animal arr[2][2]
Is:
• In Java
Animal[][] arr=
new Animal[2][2]
What is the type of
the object here ?
STRING IS AN OBJECT
• Constant strings as in C, does not exist
•String is a sequence of characters enclosed within double quotes (" ") is known as String.
Example: "Java Programming".
PACKAGES
Java code has hierarchical structure.
The environment variable CLASSPATH contains the directory names of the
roots.
Every Object belongs to a package ( ‘package’ keyword)
Object full name contains the name full name of the package containing it .
ACCESS CONTROL
Access modifiers are those keywords which are applied before data
members or methods of a class. These are used to where to access
and where not to access the data members or methods. In java
programming we have four access modifiers they are:
.
ABSTRACT
abstract member function, means that the function does not have an implementation.
abstract class, is class that can not be instantiated.
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error
NOTE:
An abstract class is not required to have an abstract method in it.
But any class that has an abstract method in it or that does
not provide an implementation for any abstract methods declared
in its superclasses must be declared as an abstract class.
Example
ABSTRACT - EXAMPLE
package java.lang;
public abstract class Shape {
public abstract void draw();
public void move(int x, int y) {
setColor(BackGroundColor);
draw();
setCenter(x,y);
setColor(ForeGroundColor);
draw();
}
}
package java.lang;
public class Circle extends Shape {
public void draw() {
// draw the circle ...
}
}
STATIC KEYWORD
The static keyword in Java is used for memory management mainly. We
can apply java static keyword with variables, methods, blocks and nested
class. The static keyword belongs to the class than an instance of the class.
It is used for a constant variable or a method that is the same for every
instance of a class.
The main method of a class is generally labelled static.
THIS KEYWORD
refers to “this” object (object in which it is used
Usage
with an instance variable or method of “this” class
as a function inside a constructor of “this” class
as “this” object, when passed as parameter
SUPER KEYWORD
reference variable that is used to refer parent class object. Super is an
implicit keyword created by JVM and supply each and every java program
for performing important role in three places.
At variable level
At method level
At constructor level
INTERFACE
Interfaces are useful for the following:
· Capturing similarities among unrelated classes without artificially forcing a
class relationship.
· Declaring methods that one or more classes are expected to implement.
· Revealing an object's programming interface without revealing its class.
INTERFACE
abstract “class”
Helps defining a “usage contract” between classes
All methods are public
Java’s compensation for removing the multiple inheritance. You can “inherit”
as many interfaces as you want.
Perfect tool for encapsulating
the classes inner structure.
Only the interface will be
exposed
*
- The correct term is “to implement” Example
an interface
INTERFACE
interface IChef {
void cook(Food food);
}
interface BabyKicker { interface SouthParkCharacter {
void kickTheBaby(Baby); void curse();
} }
class Chef implements IChef, SouthParkCharacter {
// overridden methods MUST be public
// can you tell why ?
public void curse() { … }
public void cook(Food f) { … }
}
* access rights (Java forbids reducing of access rights)
EXCEPTION - WHAT IS IT AND WHY
DO I CARE?
Exceptions take care of handling errors
instead of returning an error, some method calls will throw an exception.
Can be dealt with at any point in the method invocation stack.
Forces the programmer to be aware of what errors can occur and to deal with them.
Types of Java
Exceptions
There are mainly two types of exceptions: checked and unchecked.
Here, an error is considered as the unchecked exception. According
to Oracle, there are three types of exceptions:
Difference between Checked
and Unchecked Exceptions
1) Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error
are known as checked exceptions e.g. IOException, SQLException etc. Checked
exceptions are checked at compile-time.
2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Java Exception Handling Example
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
//rest code of the program
System.out.println("rest of the code...");
}
}
Output:
Exception in thread main
java.lang.ArithmeticException:/ by zero rest of the
code...
ANY
QUERIES