0% found this document useful (0 votes)
2 views

Java

The document explains various Java concepts including packages, multithreading, applets, exception handling, and I/O operations. It details the structure and life cycle of threads and applets, as well as the importance of exception handling in maintaining application flow. Additionally, it distinguishes between text and binary I/O operations in Java.

Uploaded by

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

Java

The document explains various Java concepts including packages, multithreading, applets, exception handling, and I/O operations. It details the structure and life cycle of threads and applets, as well as the importance of exception handling in maintaining application flow. Additionally, it distinguishes between text and binary I/O operations in Java.

Uploaded by

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

8. What is package?

Explain with example


Ans: A java package is a group of similar types of classes,
interfaces and sub-packages. Package in java can be
categorized in two form, built-in package and user-defined
package. There are many built-in packages such as java, lang,
awt, javax, swing, net, io, util, sql etc. Here, we will have the
detailed learning of creating and using user-defined packages.

Simple example of java package The package keyword is used


to create a package in java. //save as Simple.java package
mypack; public class Simple{ public static void main(String
args[]) { System.out.println("Welcome to package"); } }
6] what is multithread and explain its life cycle .
• Java is a multi-threaded programming language which means we can develop multi-threaded
program using Java.
• A multi-threaded program contains two or more parts that can run concurrently and each part can
handle a different task at the same time making optimal use of the available resources specially
when your computer has multiple CPUs
Each of the threads can run in parallel.

Life Cycle of a Thread


• Athread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then
dies.
New − Anew thread begins its life cycle in the new state. It remains in this state until the program starts the
thread. It is also referred to as a born thread.
 Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is
considered to be executing its task.
 Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread
to perform a task. A thread transitions back to the runnable state only when another thread signals the
waiting thread to continue executing.
 Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A
thread in this state transitions back to the runnable state when that time interval expires or when the event
it is waiting for occurs.
 Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or
otherwise terminates
13] what is thread ? Explain life cycle. [8marks]
Thread
AThread is a very light-weighted process, or we can say the smallest part of the process
that allows a program
to operate more efficiently by running multiple tasks simultaneously.
1) New (Ready to run)
Athread is in New when it gets CPU time.
2) Running new
Athread is in a Running state when it is under execution.
3) Suspended runna Non-runnable
A thread is in the Suspended state when it is temporarily ble (blocked
inactive or under execution.
5) Terminated runni
4) Blocked ng
A thread is in the Blocked state when it is waiting for
trmina
resources.
ted
A thread comes in this state when at any given time,
15] Define Applet and explain its life cycle.
Ans: Applet Life Cycle in Java
• TheJava plug-in software is responsible for managing the life cycle of an applet.
• An applet is a Java application executed in any web browser and works on the client-side. It
doesn't have
the main() method because it runs in the browser. It is thus created to be placed on an HTML
page.
• Theinit(), start(), stop() and destroy() methods belongs to the applet. Applet class.
• Thepaint() method belongs to the awt. Component class.
• InJava, if we want to make a class anApplet class, we need to extend the Applet
• Whenever we create an applet, we are creating the instance of the existing Applet class.
And thus, we can
use all the methods of that class.
 These methods are invoked by the browser
automatically. There is no need to call them explicitly.
init():
The init() method is the first method to run that initializes the applet. It can be invoked only once at the time of
initialization

start():
The start() method contains the actual code of the applet and starts the applet. It is invoked immediately after the init()
method is invoked. Every time the browser is loaded or refreshed, the start() method is invoked

stop():
The stop() method stops the execution of the applet. The stop () method is invoked whenever the applet is stopped, minimized,
or moving from one tab to another in the browser, the stop() method is invoked.
destroy():
The destroy() method destroys the applet after its work is done. It is invoked when the applet window is closed or
when the tab containing the webpage is closed.

paint():
The paint() method belongs to the Graphics class in Java. It is used to draw shapes like circle, square, trapezium, etc.,
in the applet. It is executed after the start() method and when the browser or applet windows are resized
1] Exception handling
Ans:Exception Handling in Java
• The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that
the normal flow of the application can be maintained.
What is Exception in Java?
What is Exception Handling?
In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown
at runtime.
Exception, SQL Exception, Remote Exception, etc.
Advantage of Exception Handling
Exception Handling is a mechanism to handle runtime errors such as Class Not Found Exception, IO
• The core advantage of exception handling is to maintain the normal flow of the application. An exception normally
disrupts the normal flow of the application; that is why we need to handle exceptions

Types of Java Exceptions


There are mainly two types of exceptions: checked and unchecked. An error is considered as the
unchecked exception. However, according to Oracle, there are three types of exceptions namely:
1. Checked Exception
2. Unchecked Exception
3. Error
1) Checked Exception
The classes that directly inherit the Throw able class except Runtime
Exception and Error are known as checked
exceptions. For example, IO Exception, SQL Exception, etc. Checked exceptions
are checked at compile-time.
2) Unchecked Exception
The classes that inherit the Runtime Exception are known as unchecked
exceptions. For example, Arithmetic
Exception, Null Pointer Exception, Array Index Out Of Bounds Exception, etc.
Unchecked exceptions are not checked
at compile-time, but they are checked at runtime.
3) Error
Error is irrecoverable. Some example of errors are Out Of Memory Error,
Virtual Machine Error, Assertion Error etc.
12. Explain java features.
Ans : Features of Java
The primary objective of Java programming language creation was to make it portable,
simple and secure
programming language. Apart from this, there are also some excellent features which
play an important
role in the
popularity of this language. The features of Java are also known as Java buzzwords.
a) Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun Microsystem, Java language is a simple programming language
because:
b) Object-oriented
java is an object-oriented programming language.
Everything in Java is an object. Object-oriented means we organize our software as a
combination of
different types of objects that incorporate both data and behavior.
c) Platform Independent
Java is platform independent because it is different from other languages like
C.
A platform is the hardware or software environment in which a program runs.
Java provides a software-based platform.
d) Secured
Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because
Java language provides these securities by default. Some security can also be
provided by an application
developer explicitly through SSL, JAAS, Cryptography, etc.
e) Robust
java is robust because:
It uses strong memory management.
what is random access files?
Ans:Random Access Files in Java:In Java, Random Access Files
allow reading from and writing to any part of a file at any time.
This is in contrast to sequential access, where you must read
or write the data in order. Key Points:File Handling: It is
implemented using the RandomAccessFile class in Java, which
allows you to move to any position in the file and read/write
data at that position.Methods:read(), write(): Used for reading
from and writing to the file.seek(long position): Moves the file
pointer to the specified position.File Modes:"r": Read-only
mode."rw": Read-write mode.
1.Try catch and finally block explain with example?

Ans:Try, Catch, and Finally Block in Java:In Java, exception handling is done
using the try, catch, and finally blocks. These blocks help manage
exceptions and ensure that the program continues to run smoothly even in
case of errors.Explanation:try block: The code that might throw an
exception is placed inside the try block. If an exception occurs, the flow of
control moves to the corresponding catch block.catch block: This block
catches and handles the exception thrown by the try block. You can have
multiple catch blocks to handle different types of exceptions.finally block:
The finally block is optional but, if present, it is executed whether an
exception is thrown or not. It is typically used to release resources like
closing files or network connections.
what is text and binary i/o?

Text and Binary I/O in Java:In Java, I/O (Input/Output) operations are classified into two types based on the
format in which data is read from or written to files:1. Text I/O:Text I/O refers to the reading and writing of data
in the form of human-readable characters (such as letters, digits, and symbols).Data is usually read or written
using character streams like FileReader and FileWriter in Java.It works with character encoding (e.g., UTF-8) to
convert between characters and bytes.
2.Binary i/o
*refers to the reading and writing of data in the form of raw binary format, including all types of data (e.g.,
images, videos, audio, and other files) that are not human-readable.
•Data is usually read or written using byte streams like FileInputStream and FileOutputStream.
•Binary I/O does not apply any character encoding or transformation, making it more efficient for handling non-text
data.

You might also like