Dca7102 - Programming in Java
Dca7102 - Programming in Java
Program :- MCA
Semester:- 3RD
SET- 1
Java Architecture is a collection of components, i.e., JVM, JRE, and JDK. It integrates the
process of interpretation and compilation. It defines all the processes involved in creating a
Java program. Java Architecture explains each and every step of how a program is compiled
and executed.
Java Virtual Machine :- The main feature of Java is WORA. WORA stands for Write
Once Run Anywhere. The feature states that we can write our code once and use it
anywhere or on any operating system. Our Java program can run any of the platforms only
because of the Java Virtual Machine. It is a Java platform component that gives us an
environment to execute java programs. JVM's main task is to convert byte code into machine
code. JVM, first of all, loads the code into memory and verifies it. After that, it executes the
code and provides a runtime environment.
Java Runtime Environment :- It provides an environment in which Java programs are
executed. JRE takes our Java code, integrates it with the required libraries, and then starts the
JVM to execute it.
(B) Unicode :- Unicode is a universal character encoding standard. This standard includes
roughly 100000 characters to represent characters of different languages. While ASCII uses only
1 byte the Unicode uses 4 bytes to represent characters. Hence, it provides a very wide variety of
encoding. It has three types namely UTF-8, UTF-16, UTF-32. Among them, UTF-8 is used
mostly it is also the default encoding for many programming languages.
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and
double. Java developers included these data types to maintain the portability of java as the
size of these primitive data types do not change from one operating system to another.
Int 32 Integer -2 31 to 2 31 -1
Based on primitive data types, the Abstract Datatypes have more functionality in comparison
primitive data types. An example of Abstract datatypes is String, it can store digits, alphabets
and special characters such as /, (); :$#. Normal numerical operations cannot be performed on
a variable of String datatype even if the data stored in it has digits.
ANS.2 (A) The super keyword in java is a reference variable that is used to refer to parent
class objects. An understanding of Inheritance and Polymorphism is needed in order to
understand the super keyword. The keyword “super” came into the picture with the concept
of Inheritance. It is majorly used in the following contexts.
1. Use of super with variables
This scenario occurs when a derived class and base class has the same data members. In
that case, there is a possibility of ambiguity for the JVM.
class Vehicle {
void display()
+ super.maxSpeed);
// Driver Program
class Test {
small.display();
Output
Maximum Speed: 120
2. Use of super with methods
This is used when we want to call the parent class method. So whenever a parent and child
class have the same-named methods then to resolve ambiguity we use the super keyword.
// superclass Person
class Person {
void message()
// Subclass Student
void message()
void display()
{
message();
super.message();
// Driver Program
class Test {
s.display();
Output
This is student class
This is person class
3. Use of super with constructors
The super keyword can also be used to access the parent class constructor. One more
important thing is that „super‟ can call both parametric as well as non-parametric
constructors depending upon the situation.
// superclass Person
class Person {
Person()
Student()
super();
// Driver Program
class Test {
Output
Person class Constructor
Student class Constructor
(B) Exception Handling :- 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. Java creates an object of appropriate exception class whenever there a
error occurs in your method. The term exception in Java means the occurrence of an
exceptional event. It is defined as an abnormality that occurs while the program is in
execution which hinders the successful execution of the program, which means the way the
program is meant to produce the result it doesn‟t do so.
Try :- The "try" keyword is used to specify a block where we should place an exception code.
It means we can't use try block alone. The try block must be followed by either catch or
finally.
try
{
// statement that may cause an exception
}
Catch :- The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.
try
{
//statements that may cause an exception
}
catch ()
{
// error handling code
}
ANS.3 Streams Basic :- Java handles all input and output in the form of streams. A stream
is a sequence of bytes traveling from a source to a destination over a communication path. If
a program writes into a stream, it is the stream‟s source. Similarly, if it is reading from a
stream, it is the stream‟s destination. Streams are powerful because they abstract the details of
input/output (I/O) operations.
The two basic streams used are the input and the output streams. Each stream has a particular
functionality. You can only read from an input stream and conversely, you can only write to
an output stream. Some streams are classified as mode streams because they read from or
write to a specific place like a disk file or memory. Some streams are classified as filters.
Filters are used to read data from one stream and write it to another stream. The stream
classes of java.io packages are categorized into two groups based on the data type on which
they operate:
• Byte Stream: Provide support for handling I/O on bytes.
• Character stream: Provide support for handling I/O on characters.
The two major classes for byte streams are InputStream and OutputStream. These abstract
classes are sub-classed to provide a variety of I/O capabilities. The InputStream class lays the
foundation for the input class hierarchy, whereas the OutputStream class lays the foundation
for the output class hierarchy
Methods of the InputStream Class
Method Explanation
int read () Reads a byte of data from the input stream.
int read (byte [] b) Reads bytes of data from the input stream
and stores them in the array.
int read (byte [] b, int off, int len) Reads the number of bytes specified by the
third argument from the input stream and
stores it in the array. The second argument
specifies the position from where the bytes
have to be read.
int available () Returns the number of bytes available for
reading from the input stream.
void close () Close an input stream and releases all the
resources associated with it.
Method Explanation
void write (int n) Writes the specified byte of data to the output
stream
void write (byte [] b ) Writes an array of bytes to the output stream.
void write (byte [] b, int off, int len ) Writes a segment of an array to the output
streams.
void flush () Force writes whenever the data accumulates
in the output stream
void close () Cause the output stream to close. Any data
present on it is stored before the stream is de-
allocated from the memory
Method Explanation
void writeChar (int v ) Writes a character to the output stream.
void writeLong (long v) Writes a long integer to the output stream.
void writeInt (int v ) Writes an integer to the output stream.
SET -2
ANS.1 (A) the collection hierarchy :- The hierarchy of the entire collection framework
consists of four core interfaces such as Collection, List, Set, Map, and two specialized
interfaces named SortedSet and SortedMap for sorting. All the interfaces and classes for the
collection framework are located in java.util package .
• Extends: The keyword extends used to create inheritance between two classes and two
interfaces
• Implements: The term implements are used to create inheritance across classes and
interfaces.
The Collection interface, which is the root interface of all archives in the API, is the most
fundamental interface of the collections system (Application programming interface). In Java,
this is at the apex of the collection hierarchy. It allows you to add and remove elements from
the Collection using basic methods. The Iterable interface is augmented by the Collection
interface. There is only one procedure in the iterable interface called Iterator (). The iterator
method's purpose is to retrieve the iterator object. We may loop over the components of the
array that use this iterator object.
1. List Interface
2. Set Interface
3. SortedSets Interface.
4. Queue Interface
5. Deque Interface
6. Map Interface
(B) Java Annotation :- Java Annotation is a tag that represents the metadata i.e. attached
with class, interface, methods or fields to indicate some additional information which can be
used by java compiler and JVM. Annotations in Java are used to provide additional
information, so it is an alternative option for XML and Java marker interfaces.
Java Custom annotations or Java User-defined annotations are easy to create and use. The
@interface element is used to declare an annotation.
Types of Annotation
1. Marker Annotation
2. Single-Value Annotation
3. Multi-Value Annotation
Marker Annotation :- An annotation that has no method, is called marker annotation. For
example: @interface MyAnnotation{}
@interface MyAnnotation{
int value();
@interface MyAnnotation{
@MyAnnotation(value=10)
Multi-Value Annotation :- An annotation that has more than one method, is called Multi-
Value annotation. For example:
@interface MyAnnotation {
int value1();
String value2();
String value3();
@interface MyAnnotation {
import javax.swing.*;
b.setBounds(130,100,100,40);
f.add(b);
f.setSize(400,500);
f.setLayout(null);
f.setVisible(true);
Life Cycle of a Thread :- A thread goes through various stages in its life cycle. For example,
a thread is born, started, runs, and then dies. The following diagram shows the complete life
cycle of a thread.
New − A new 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.
ANS.3 :- Java Server Pages (JSP) :- Java Server Pages (JSP) is a programming tool on the
application server side that supports platform-independent and dynamic methods to
construct Web-based applications. Much as Servlet technology does, the JSP method
provides a web application. It can be considered an expansion of Servlet because it offers
more features than servlet. Since we can differentiate design and development, the JSP
pages are simpler to manage than Servlet. HTML tags and JSP tags are present in Java
Server Pages. To access enterprise servers, Java Server Pages has an approach to the
entire community of Java APIs, including the JDBC API. This tutorial will walk you to
the path of building your own web application in convenient and simple steps using Java
Server Pages.
How does JSP look? Before considering more technical details regarding JSP, let us see
how a JSP file looks and what its static parts are and what its dynamics parts are:
1. <HTML>
2. <HEAD>
6.0”>
4. <TITLE></TITLE>
5. </HEAD>
6. <BODY>
7. <center><h1>
8. <% out.println(“Hello World!”); %>
9. </h1><center>
10. </BODY>
11. </HTML>
As you can see a JSP page looks almost like a simple HTML file. In the above
HelloWorld.jsp line number eight is the one accounting for the dynamic content. All the
other lines are static HTML content. If you observe line eight carefully, you can notice
that the written code resembles servlet code in some way. Once a JSP is written next
question is how do you test it? To test JSP we are going to use Java Web Server. The steps
are as following: