Java Features:-
simple
robust
multithreaded
portable
secure
distributed
high performance
architecture neutral
//4 Pillars of java oops:=
AEI OU //
A abstraction - hiring engine
Abstract class to partial abstraction ex. keyboard
interface when we want 100% abstraction ex. banking system
E encapsulation - wrapping the code and data together
Two methods getter and setter
getter- to get values
setter- to set values
I inheritance - acquiring all properties and behaviours of parent class
Single Inheritance
Multilevel Inheritance
O
P polymorphism - doing one thing in different way
U Method Overloading - defining method again in same class
Method Overriding - defining method again in child class
//ACCESS MODIFIERS:
private: data accessible in only same class ex. Roll no
protected: data accessible in child class ex.
default: data within same package
public: data accessible everywhere(all classes/packages)
//CONSTRUCTOR
it is special method which has same name as class name which is called when object
is created.
//int float char class are Keywords in Java
//STRINGS
it is array of characters
types- string immutable
string buffer- used when thread safety is imp
string builder-used when we want high performance
//THREADS:
light weight process
used to run multiple class simultaneously
it will call thread by extends keywords
for thread communication we are using 3 methods wait(), notify(), notifyAll()
//hw
JDBC Database connection
//VARIABLES
Local Variables :
variables declared in method
global:
variables declared outside method in class
static variables:
var declared: at class level
//JVM machine memory ke 3 level
stack
memory
heap
//Data types in java
Primitive ->Numeric->integer
->float
->double
->nonnumeric
non-Primitive->classes
->Interface
->Arrays
//Type Casting:
assigning value of one type toa variable of another type is known as type casting
widening
byte->short->int->long->float->double
Narrowing
double->float->long->int->short->byte
//OPERATORS IN JAVA
Arithmetic: + - * / %
Relational: < > >= <= == !=
Logical: && || !
Unary: + - ++ -- !
Boolean logical: True False
//Java Statements
Sequential Statements:
if-else
nested if-else
Switch case
Control Statements:
do-while
while
for loop
//COLLECTION
group of object is called as collection
List
Set
Map
collection
list
array list, queue, vector, stack
queue
priority queue, deque, arraydeque,
set
hashset, linked hashset sorted set
array list
internal data structure is array
duplicates are allowed
order is maintained
searching operation
linked List
used at the time of deletion addition
hash set:
internally used data structure like hashmap or set is tree
Tree set
internal data structure used tree , duplicates are allowed
/imp/collection-multiple grp of object;
list ,set,queue are the types of collections;
iterable
^
|
collection
^
|
list Queue Set
Array list Priority Queue Hash Set and Linked Hash set
linked list Array queue Tree set
vector stack
*list:-
array list ---Internal DS is Array;
for searching eg.roll no searching
linked list ---Internal DS is linked List
for addtion,deletion operation .
Generics concept//order mainting
eg- ArrayList<string//this is generics> Car = new ArrayList<string>();
cars.add("volvo");
cars.add("BMD");
cars.add("ODII");
cars.add("mazda");//add hote jayenge
SOP(cars);
*SET:-
Duplicate not Allowed
Hash Set:-
internally uses Hash Map or Hash table Data Types
Duplition not Allowed and Order is anything
Linked Hash set:-
eg- LinkedHashSet<string> cars = new LinkedHashSet< string>
Tree Set:-
Internally use Tree Map Data structure And Duplicationn is not
allowed;
Insertion order is Ascending;
//Map
Hash Map
used when we want to use key-value pair then that time ew use map
HashMap<Integer,String> cars = new HashMap<Integer>
cars.put(1,"ALTROZ");
cars.put(2,"EFEFEF");
System.out.println(cars)
Linked hash Map
Tree Hash Map
//wrapper class
Wrapper classes
Wrapper classes are used to convert any primitive type into an object. The
primitive data types are not objects, they do not belong to any class, they
are defined in the language itself. While storing in data structures which
support only objects, it is required to convert the primitive type to object
first, so we go for wrapper class.
Example :
int x = 25;
Integer y = new Integer(x);
//Garbage Collection
programmer doesn't need to worry about dereferencing an object .
it is done auto by JVM
//This Keyword
//final keyword
is the variable
if we apply final on method it cannot be override
if we apply it on class then class cannot be extended
if we apply final on variable then value of variable cannot be changed
//Stream API:-
Stream api does not store elements. It simply coveys elements from source
such as
a data structure, an array, or an I/O channel, through pipeline of computational
operations
two methods in string
1. filer
2. toset
//code:->
import java.util.ArrayList;
public class collectionExample{
public static void main(string args[]){
ArrayList<Integer>num = new ArrayList<Integer>();
num.add(30);
num.add(40);
num.add(50);
num.add(60);
num.add(60);
List<Integer> ans = num.stream().filter(i->i%20 ==
0).collect(collector.toset());
S.O.P(ans);
List<Integer> ans2 = num.stream().filter(i->i+10).collect(collector.toList());
S.O.P(ans2);
//Exception Handling:-
Exception
Definition:
"An exception is an event, which occurs during the execution of a program, that
disrupts
the normal flow of the program's instructions"
An exception occurs when our code asks the JVM to perform the technically
impossible
and unanticipated (by the compiler)!
1. Anticipate the error by the user/system.
2. Return the program to a safe state that enables the user to execute other
commands.
3. Inform the user of the error's cause.
4. Allow the user to save work and terminate the program gracefully.
//Exception Hierarchy
All exception types are subclasses of the built-in class Throwable
Throwable has two subclasses :
o Exception
o Error
//Types of Exceptions in Java
Checked exceptions
-All exception inherited from Exception Class.
-It is compile time error.
Unchecked exceptions
-All exception inherited from Runtime Exception Class.
-It is run time error.
//handling the exception
public class ExceptionHandling{
public static void main (String[] args) {
try
{
int data=50/0; //may throw exception
}
catch (Exception e)
{
Syatem.out.prinln(e);
}
System.out.println("rest of the code") ;
Output:-
rest of the code
//finally
It is such type block of statements which will execute irrespective of exception.
It will execute always.
public class ExceptionHandling{
public static void main (String[] args) {
try
{
int data=50/0; //may throw exception
}
finally
{
Syatem.out.prinln("Finally Block");
}
System.out.println("rest of the code") ;
Output:-
Finally Block
//Applet in Java
-used for web applications
. Applets are small Java applications that can be accessed on an
internet server, transported over internet, and can be automatically
installed and run as apart of a web document.
. Any applet in Java is a class that extends the java.applet.Applet class.
. An Applet class does not have any main() method.
. It is viewed using JVM. The JVM can use either a plug-in of the
Web browser or a separate runtime environment to run an applet
application.
. JVM creates an instance of the applet class and invokes init()
method to initialize an Applet.
//Advantages of Applets:
. Very less response time as it works on the client side.
. Can be run using any browser, which has JVM running in it.
//An applet can:
· Draw pictures on a web page
· Create a new window and draw in it.
. Play sounds.
. Receive input from the user through the keyboard or the
mouse.
. Make a network connection to the server from which it
came and can send to and receive arbitrary data from
that server.
//Life Cycles of Methods:-
init()->Start()->Paint(Graphicd G)->Stop()->Destroy()
//Events
An event is an object that describes some change in the state of a source.
An event is generated when the user interacts with an element in a graphical
user interface.
Examples of such events are pressing a button, moving the slider in a scroll
bar, 'typing characters into a text field, closing a window or dragging the
mouse.
In addition to these events, events that are not directly caused by user input
can also be generated. For example, arrival of incoming data, buffer overflow,
end of a process or expiry of a software timer.
//Applet's Size
import java.applet.*;
import java.awt .*;
public class SizeApplet extends Applet
{
public void paint(Graphics g)
{
Dimension appletSize = this.getSize();
int appletHeight = appletSize.height;
int appletWidth = appletSize.width;
g.drawString("This applet is " + appletHeight +
"pixels high by "+ appletWidth+
" pixels wide.", 15, appletHeight/2);
}
}
//AWT
Java AWT calls the native platform calls the native platform (operating systems)
subroutine for creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI wit Ncomponents lke TextField, label and button will
have different look and feel for the different platforms like Windows, MAC OS,
and Unix. The reason for this is the platforms have different view for their native
components and AWT directly calls the native subroutine that creates those
components.
In simple words, an AWT application will look like a windows application in
Windows OS whereas it will look like a Mac application in the MAC OS.
//Components
All the elements like the button, text fields, scroll bars, etc. are called
components. In Java AWT, there are classes for each component as shown in
above diagram. In order to place every component in a particular position on a
screen, we need to add them to a container.
//Container
The Container is a component in AWT that can contain another components
like buttons, textfields, labels etc. The classes that extends Container class are
known as container such as Frame, Dialog and Panel.
Notes
Types of containers:
There are four types of containers in Java AWT:
1.Window
2.Panel
3.Frame
4.Dialog
//SWING
Swing
Swing Framework contains a set of classes that provides more powerful and
flexible GUI components than those of AWT. Swing provides the look and feel of
modern Java GUI.
· swing component provide much flexible user interface because it follow model
view controller(mvc).
· swing works faster.
· swing components are light weight.
· swing occupies less memory space.
· swing component is platform independent.
· swing require javax.swing package.
//JComponent:-
JFrame : JFrame is Swing's version of Frame and is descended directly
from Frame class. The component which is added to the Frame, is refered
as its Content.
. JWindow : This is Swing's version of Window and hass descended directly
from Window class. Like Window it uses BorderLayout by default.
. JLabel : JLabel descended from Jcomponent, and is used to create text
labels.
JButton : JButton class provides the functioning of push button. JButton
allows an icon, string or both associated with a button.
JTextField : JTextFields allow editing of a single line of text.
Introduction to Swing Classes
. JPanel : JPanel is Swing's version of AWT class Panel and uses the same
default layout,
FlowLayout. JPanel is descended directly from