0% found this document useful (0 votes)
14 views14 pages

AJP 2 Marks

Gujarat power Bank of India is my name is farmza in the postfix notation for my business in the group

Uploaded by

Gobinath M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views14 pages

AJP 2 Marks

Gujarat power Bank of India is my name is farmza in the postfix notation for my business in the group

Uploaded by

Gobinath M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INTRODUCTION

"A thread is an independent path of execution within a program.


" Many threads can run concurrently within a program.
"Every thread in java is created and controlled by the java.lang.Thread
class.

"A Java program can have many threads and these threads can run
concurrently, either asynchronously or synchronously.

INTRODUCTION
"Typically, we can define threads as a
lightweight subprocess and also has
separate paths of execution.
"Threads are independent, if there occurs 13

exception in one thread, it doesn't affect


Process3
other threads.
" It shares a common memory area.
"There can be multiple processes inside Process1
Process2

the OS and one process can have


multiple threads.

What are threads Good for?


"ToMaintain responsiveness of an application during a long running task.

"To enable cancellation of separable tasks.

"To monitor status of some resources(e.g., DB)

" Some APIs and System demand it (e.g., Swing)


JLabel

JLabel is a class in javax.swing package. JLabel is used to display a short message ie. String
or an image icon or both.

Constructor methods of JLabel :

" JLabel() : creates a new empty label.


JLabel(String text) : creates a new label with the text string on it.
JLabel(Icon i) : creates a new label with an image.
JLabel(String s, Icon I, int align) : creates a new label with a string and an image
with a specified horizontal alignment.
Other methods of JLabel :

getIcon(): returns the icon displayed by the label.


" setlcon(Icon i):sets the icon to the label.
getText() :returns the text of the label.
" setText(String s): sets the text to the label.

Example:
Lport javax. Swing. *;
public class Main extendsjavax . Swing.JErame (
publiC static void main (String args [] ) {
JFrame frame= new JFrame ("My Swing JLabel") ;
frame. setSize (400, 400) ;
JPanel panel = new JPanel () ;
JLabel heyImageLabel = neW JLabel () ;

ImageIcon heyimageI con = new


ImageIcon ("C: \\Users\\ Data\ \ Downloads\\hey design. png"):
heyImageLabel .setIcon (heyimageIcon) ;
panel. add (heyImageLabel) ;
frame. add (panel);
frame. setVisible (true) ;

We create a label named heylmageLabel with no text in it. To add an image to the label we
create an object of Imagelcon named heyimagelcon. It takes the address of the image as the
parameter. Using the setlcon method we add an icon to the label heylmageLabel as seen in
the output below.

Note: Here we use two backslashes instead of one and the png address should be in double
inverted commas.

OUTPUT:

EY!
Implementing Runnable interface
"The Runnable interface should be implemented by any class whose
instances are intended to be executed by a thread. Runnable
interface have only one method named run().
1. public void run(): is used to perform action for a thread.
"Starting a thread:
The start() method of Thread class is used to start a newly
created thread. t performs the following tasks:
"A new thread starts(with new callstack).
" The thread moves from New state to the Runnable state.
"When the thread gets a chance to execute, its target run() method
will run.
Demo for using ServletConfig (Contd.).
Web.xml
<web-app>
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>Second</servlet-class>
<init-param>
<param-name>homeName</param-narme>
<param-value>Welcome to www.simple.coms/param-value>
<linit-param>
<0servlet> The init parameter valuesre
<servlet-mapping> a configured in the
<servlet-name>Second</servlet-name> web.xml deployment
descriptor file
<url-pattern>/Second</ur-pattern>
<0servlet-mapping>
</web-app>

9
HashSet

"HashSets store elements according to a hash code.


"A hash code of an element is an integer computed
from the value of the element that can be used
to help identify the element.
"The procedure used to compute the hash code of
an element is called the hashing function or
the hashing algorithm.

Examples of Hashing Functions

"For Integer objects, youcan use the integer value of


the object (or its absolute value).

"For Character objects,you can use the UNICODE value


for the character.

"For String objects, you can use a function that takes


into account the UNICODE values of the characters
that make up the string, as well as the position
Occupied by each characte.
TreeSe
t
A TreeSet stores elements based on a natural order
defined on those elements.

The natural order is based on the values of the


objects being stored

By internally organizing the storage of its elements


according to this order, a TreeSet allows fast search for
any element in the collection.

Order

An order on aset of objects specifies for any two


objects x and y, exactly one of the following:
x is less than y
x isequal to y
x isgreater than y

55-56/82

Examples of Natural Orders

Some classes have a "natural" order for their objects:


Integer,Float, Double etc has the obvious concept of natural order which
tells when one number is less than another.
" The String class has a natural alphabetic order for its objects.
"finally'
Afinally block contains all the crucial statements that must be executed whether an exception
occurs or not. i.e important statements are placed here.
The statements present in this block will always execute, regardless an exception occurs in the
try block or not such as closing a connection, stream etc.
catch(NullPointerException e) {
class SampleFinallyBlock{
System.out.printin(e):
public static void main(String argsD){
try{
finally {
int data=55/5;
System.out.println("finally block is executed")]}
System.out.printin(data):
System.out. println("remaining code");
Life Cycle of Thread
"There are different states Thread transfers into during its lifetime, let us
know about those states in the following lines: in its lifetime, a thread
undergoes the following states, namely:
1. New State

2. Active State

3. Waiting/Blocked State
4. Timed Waiting State
5. Terminated State
JDBC Drivers

? Java application can't directly communicate with database,only


DBMS can understand SQL statements
? JDBC drivers implement the defined interfaces in the JDBC API, for
interacting with your database server.
? For example, using JDBC drivers enable you to open database
connections and to interact with it by sending SQL or database
commands then receiving results with Java.

10/202023
JDBC Application creation
steps to connect to database in Java

" Register the driver class


" Create the connection object
" Create the Statement object
" Execute the query
o Close the connection obiect
Multithreading
" Multithreading in Java is a process of executing multiple threads simultaneously.
"A thread is a lightweight sub-process, the smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.

However, we use multithreading than multiprocessing because threads use a


shared memory area. They don't allocate separate memory area so saves
memory, and context-switching between the threads takes less time than
process.

" Java Multithreading is mostly used in games, animation, etc.

Advantages of Java Multithreading


1) It doesn't block the user because threads are independent and you

can perform multiple operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn't affect other threads if an

exception occurs in a single thread.


"throw' vs "throws'

throw throws

1. Used to explicitly throw an exception 1. Used to declare an exception


2. Checked exceptions cannot be propagated using
throw only 2. Checked exceptions can be propagated
3. Followed by an instance 3. Followed by a class
4. Used within a method 4. Used with a method signature
5. Cannot throw multiple exceptions 5. Can declare multiple exceptions

Exception Instance Responsibility of handling


exception

throw JVM throws Caller method


ArrayList and Vector

ArrayList and Vector are array-based lists.


Internally, they use arrays to store their elements:
whenever the array gets full, a new, bigger array is
created, and the elements are copied to the new
array.

Vector has higher overhead than ArrayList because


Vector is synchronized to make it safe for use in
programs with multiple threads.

AbstractSequential List and LinkedList

Array-based lists have high overhead when elements are


being inserted into the list, or removed from the list, at
positions that are not at the end of the list.

LinkedList is a concrete class that stores elements in a way


that eliminates the high overhead of adding to, and
removing from positions in the middle of the list.
LinkedList extends AbstractSequentialList, which in turn,
extends AbstractList.

Using the Concrete List Classes

" The concrete classes ArrayList, Vector, and LinkedList work in


similar ways, but have different performance characteristics.
" Because they all implement the List interface, you can use
List interface references to instantiate and refer to the
different concrete classes.
" Using a List interface instead of the concrete class reference
allows you to later switch to a different concrete class to get
better performance.
Example: ArrayList
import java.util.*;
public class Test

public static void main(Stringl] args)


List<String> nameList = new ArrayList<String> ();
String []names =("Ann", "Bob", "Carol"};
||Add to arrayList
for (int k=0; k< names. length; k++)
namelist.add(names [k);
|/Display name list
for (int k =0; k< nameList.size(); k++)
System.out.printin(nameList.get(k));

24-25/93
An Example: LinkedList

Because we used a List reference to refer to the


concrete class objects, we can easily switch from an
ArrayList to a LinkedList :the only change is in the
class used to instantiate the collection.

Example: LinkedList
import java.util.*;
public class Test

public static void main(String [] args)


List<String> nameList = new LinkedList<String> ();
String [] names = ("Ann", "Bob", "Carol");

|| Add to arrayList
for (int k=0; k< names. length; k++)
namelist.add(names[kl);

|/Display name list


for (int k= 0; k< nameList.size(); k++)
System.out.println(namelist.get(k));

You might also like