Core Java
Core Java
Where it is used?
According to Sun Microsystems, 3 billion devices run on java. There are many areas
where java is currently used. Some of them are as follows:
4. Embedded System
5. Smart Card
6. Robotics
7. Games etc.
An application that runs on the server side and creates dynamic page, is called web
application. Currently servlet, jsp, struts, jsf etc. technologies are used for creating web
applications in java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc., It has the
advantage of high level security, load balancing and clustering. In java, EJB is used for
creating enterprise applications.
History of Java
Java history is interesting to know. The history of java starts from Green Team. Java
team members (also known as Green Team), initiated a revolutionary task to develop a
language for digital devices such as set-top boxes, television remote controls etc.
For the green team members, it was an advance concept at that time. But, it was suited for
internet programming. Later, Java technology as incorporated by
Netscape. (Creator of Netscape Navigator Browser)
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of Sun microsystems engineers called Green
Project Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top
boxes, remote controls.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was “.gt”.
4) After that, it was called Oak and was developed as a part of the Green project.
1. Simple (Familiar)
2. Object-Oriented
3. Platform independent
4. Secured
5. Robust
6. Architecture neutral
7. Portable
8. High Performance
9. Multithreaded
10. Distributed
Simple (Familiar)
According to Sun, Java language is simple because:
syntax is based on C++ (so easier for programmers to learn it after C++).
Object-oriented
Object-oriented means we organize our software as a combination of different types of
objects that incorporates both data and behavior.
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent
Java code can be run on multiple platforms (in this context, platform means operating
system) e.g., Windows, Linux, Sun Solaris, Mac/OS etc. Java code (.java) is compiled by the
compiler and converted into byte code (.class).This byte code is a platform independent
code because it can run on multiple platforms i.e. Write Once and Run Anywhere (WORA).
Secured
Java is secured because:
No explicit pointer
Robust
Robust simply means strong. Java uses strong memory management. There is
automatic garbage collection in java. There is exception handling and strong type
checking mechanism in java. All these points make java robust.
Architecture-neutral
Java program can run on any machine (i.e., micro computers, main frame computers.)
Portable
We may carry the java byte code to any machine.
High-performance
Java is faster than traditional interpretation since byte code is "close" to native code still
somewhat slower than a compiled language (e.g., C++)
Distributed
We can create distributed applications in java. RMI and EJB are used for creating
distributed applications. We may access files by calling the methods from any machine on
the internet.
Multi-threaded
A thread is like a separate process, executing concurrently. We can write Java programs
that deal with many tasks at once by defining multiple threads. The main advantage of
multi-threading is that it shares the same memory. Threads are important for multi-media
etc.
install the JDK if you don't have installed it, download the JDK and install it.
Simple.java
class Simple{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
To compile: javac Simple.java
To execute: java Simple
Output: Hello World!
void is the return type of the method, it means it doesn't return any value.
o Java.lang package is the default package that means we need not import it.
Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.
JVMs are available for many hardware and software platforms. JVM, JRE and JDK are
platform dependent because configuration of each OS differs. But, Java is platform
independent. (i.e., the byte code generated by java is platform independent).
Loads code
Verifies code
Executes code
Note: JVM is platform dependent but it makes the Java platform independent .
JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE +
development tools.
2) Class(Method) Area:
Class (Method) Area stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.
3) Heap:
4) Stack:
Java Stack stores frames. It holds local variables and partial results, and plays a part in
method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its
method invocation completes.
PC (program counter) register. It contains the address of the Java virtual machine
instruction currently being executed.
It contains all the native methods (‘C’/C++ functions) used in the application.
7) Execution Engine:
It contains:
1) A virtual processor
Operators in java
Operator in java is a symbol that is used to perform operations. There are many types of
operators in java such as unary operator, arithmetic operator, relational operator, shift
operator, bitwise operator, ternary operator and assignment operator.
Operators Precedence
Postfix expr++ expr--
Unary ++expr --expr +expr -expr ~ !
multiplicative * / %
Additive + -
Shift << >> >>>
Relational < > <= >= instanceof
Equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
Ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Simula is considered as the first object-oriented programming language. The programming paradigm where
everything is represented as an object, is known as truly object-oriented programming language.
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It
can be physical and logical (such as bankaccount).
Class
Inheritance
When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It
provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to convense the
customer differently, to draw something e.g. shape or rectangle etc.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't
know the internal processing.
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines.
2)OOPs provides data hiding whereas in Procedure-oriented prgramming language a global data can be
accessed from anywhere.
3)OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of
real word problem if we are using the Object-Oriented Programming language.
Object and Class in Java
In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.
Object in Java
An entity that has state and behavior is known as
an object e.g. chair, bike, marker, pen, table, car
etc. It can be physical or logical (tangible and
intangible). The example of intagible object is
banking system.
behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
identity: Object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. But,it is used internally by the JVM to identify each object uniquely.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to
write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So
object is the instance(result) of a class.
Class in Java
A class is a group of objects that has common properties. It is a template or blueprint from which
objects are created.
data member
method
constructor
Method in Java
In java, a method is like function i.e. used to expose behaviour of an object.
Advantage of Method
Code Reusability
Code Optimization
new keyword
The new keyword is used to allocate memory at runtime.
class Student2{
int rollno;
String name;
Output:111 Karan
222 Aryan
As you see in the above figure, object gets the memory in Heap area and reference variable refers to
the object allocated in the Heap memory area. Here, s1 and s2 both are reference variables that refer
to the objects allocated in memory.
class Rectangle{
int length;
int width;
By newInstance() method
By clone() method
Annonymous object
Annonymous simply means nameless.An object that have no reference is known as annonymous object.
If you have to use an object only once, annonymous object is a good approach.
class Calculation{
void calculateArea(){System.out.println(length*width);}
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
Output:55
45
Method Overloading in Java
If a class have multiple methods by same name but different parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the readability of the
program.
In java, Methood Overloading is not possible by changing the return type of the method.
In this example, we have created two overloaded methods, first sum method performs addition of two numbers
and second sum method performs addition of three numbers.
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
}
}
Output:30
40
In this example, we have created two overloaded methods that differs in data type. The first sum method receives
two integer arguments and second sum method receives two double arguments.
class Calculation2{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
}
}
Output:21.0
40
In java, method overloading is not possible by changing the return type of the method because there may occur
ambiguity. Let's see how ambiguity may occur:
class Calculation3{
int sum(int a,int b){System.out.println(a+b);}
double sum(int a,int b){System.out.println(a+b);}
}
}
int result=obj.sum(20,20); //Here how can java determine which sum() method should be called
Yes, by method overloading. You can have any number of main methods in a class by
method overloading. Let's see the simple example:
class Overloading1{
public static void main(int a){
System.out.println(a);
}
One type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the
figure given below:
As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype
can be promoted to int,long,float or double. The char datatype can be promoted to int,long,float or double and so
on.
As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype
can be promoted to int,long,float or double. The char datatype can be promoted to int,long,float or double and so
on.
If there are matching type arguments in the method, type promotion is not performed.
class OverloadingCalculation2{
void sum(int a,int b){System.out.println("int arg method invoked");}
void sum(long a,long b){System.out.println("long arg method invoked");}
If there are no matching type arguments in the method, and each method promotes similar number of arguments,
there will be ambiguity.
class OverloadingCalculation3{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}