Java Bootcamp Day1
Java Bootcamp Day1
simple
robust
multithreaded
portable
secure
distributed
high performance
architecture neutral
//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.
//STRINGS
it is array of characters
//THREADS:
light weight process
used to run multiple class simultaneously
it will call thread by extends keywords
//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
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
iterable
^
|
collection
^
|
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 .
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
//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
//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
//code:->
import java.util.ArrayList;
public class collectionExample{
//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)!
2. Return the program to a safe state that enables the user to execute other
commands.
4. Allow the user to save work and terminate the program gracefully.
//Exception Hierarchy
o Exception
o Error
Checked exceptions
Unchecked exceptions
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{
finally
{
Syatem.out.prinln("Finally Block");
}
System.out.println("rest of the code") ;
Output:-
Finally Block
//Applet in Java
. 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.
//Advantages of Applets:
. Can be run using any browser, which has JVM running in it.
init()->Start()->Paint(Graphicd G)->Stop()->Destroy()
//Events
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
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:-
. JPanel : JPanel is Swing's version of AWT class Panel and uses the same
default layout,
FlowLayout. JPanel is descended directly from