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

Java MIDS

The document discusses key concepts related to Java including why Java is important for the internet, Java applications and applets, Java bytecode, Java buzzwords, object-oriented programming principles like encapsulation and inheritance, control statements like if and for loops, and Java class libraries. It provides definitions and examples for each topic. Some key points are that Java expanded the types of objects that could move freely online, Java applets allowed interactive content on web pages, bytecode makes Java programs portable, OOP principles organize code around objects, and built-in class libraries provide functionality for common tasks.

Uploaded by

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

Java MIDS

The document discusses key concepts related to Java including why Java is important for the internet, Java applications and applets, Java bytecode, Java buzzwords, object-oriented programming principles like encapsulation and inheritance, control statements like if and for loops, and Java class libraries. It provides definitions and examples for each topic. Some key points are that Java expanded the types of objects that could move freely online, Java applets allowed interactive content on web pages, bytecode makes Java programs portable, OOP principles organize code around objects, and built-in class libraries provide functionality for common tasks.

Uploaded by

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

CHAPTER 1

Q1. WHY JAVA IS IMPORTANT TO THE INTERNET?

• The Internet helped catapult Java to the forefront of programming and in turn java has a
profound effect on the Internet. The reason for this is quite simple: Java expands the
universe of objects that can move about freely in cyberspace. In a network, two very
broad categories of objects are transmitted between the server and your personal
computer: passive information and dynamic, active programs.

• For example, when you read your e-mail, you are viewing passive data. Even when you
download a program, the program’s code is still only passive data until you execute it.
However, a second type of object can be transmitted to your computer: a dynamic, self-
executing program.

• As desirable as dynamic programs are, they also present serious problems in the areas
of security and portability. Before Java, cyberspace was effectively closed to half the
entities that now live there. As you will see, Java addresses those concerns and by doing
so the door to an exciting new form of program has been opened: the applet.

Q2. WHAT ARE JAVA APPLICATIONS AND APPLETS?

JAVA APPLICATIONS:

Java applications are software programs or applications developed using the Java programming
language. Java is a versatile, object-oriented programming language that is designed to be
platform-independent, meaning that Java programs can run on any device that has a Java
Virtual Machine (JVM) installed.

Here are some key characteristics and aspects of Java applications:

 Platform Independence
 Object-Oriented Programming (OOP)
 Robustness & Security

Examples of Java applications include popular software like the Eclipse IDE (Integrated
Development Environment), Apache Tomcat, and many Android mobile applications.
JAVA APPLETS:

Java applets are small Java programs that are designed to be embedded and executed within
web browsers. They were a popular technology in the early days of the internet for adding
interactive and dynamic content to web pages. Java applets are distinct from standalone Java
applications in that they run within the context of a web browser, providing a means to create
more engaging and interactive web experiences.

Here are key characteristics and aspects of Java applets:

 Browser Compatibility
 Interactivity
 Web Browser Integration

Given these factors, Java applets are considered outdated, and developers are encouraged to
use alternative web technologies for creating interactive content on the web.

Q3. JAVA MAGIC THE BYTE CODE

Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time
system, which is called the Java Virtual Machine (JVM). The original JVM was designed as an
interpreter. Translating a Java program into bytecode makes it much easier to run a program in
a wide variety of environments because only the JVM needs to be implemented for each
platform. Once the run-time package exists for a given system, any Java program can run on it.
If a Java program were compiled to native code, then different versions of the same program
would have to exist for each type of CPU connected to the Internet. This is, of course, not a
feasible solution. Thus, the execution of bytecode by the JVM is the easiest way to create truly
portable programs. The fact that a Java program is executed by the JVM also helps to make it
secure. Because the JVM is in control, it can contain the program and prevent it from
generating side effects outside of the system. As you will see, safety is also enhanced by certain
restrictions that exist in the Java language.

Q4. WHAT ARE JAVA BUZZWORDS?

“Java buzzwords" typically refer to key concepts and features associated with the Java
programming language. These are terms that are commonly used to describe the characteristics
and strengths of Java. Here are some of the prominent Java buzzwords:

1. Simple:
Java is designed to be easy to learn and use. Its syntax is similar to other C-based languages,
making it accessible for developers.
2. Secure:
Java incorporates various security features, such as the Java Security Manager and bytecode
verification, to create a secure execution environment.

3. Portable:
Java programs can be easily moved from one system to another without modification, thanks to
its platform independence.

4. Object-Oriented:
Java follows an object-oriented programming (OOP) paradigm, emphasizing the use of objects
and classes to organize and structure code.

5. Robust:
Java's strict compile-time and runtime checks contribute to its robustness, reducing the
likelihood of errors and improving the stability of applications.

6. Multithreaded:
Java has built-in support for multithreading, allowing the execution of multiple threads within a
program.

7. Architecture Neutral:
Java's architecture-neutral design allows for flexibility and adaptability across different types of
systems and architectures.

8. Interpreted:
Java is often described as both compiled and interpreted. Code is initially compiled into
bytecode, which is then interpreted by the JVM at runtime.

9. High Performance:
Java is designed to offer high performance through the use of Just-In-Time (JIT) compilation and
other optimization techniques.

10. Distributed:
Java supports the development of distributed applications, enabling the creation of networked
and client-server architectures.

11. Dynamic:
Java supports dynamic memory allocation and garbage collection, simplifying memory
management for developers.
CHAPTER 2
Q6. EXPLAIN THREE OOP PRINCIPLES.

Object-oriented programming (OOP) is a computer programming model that organizes software


design around data, or objects, rather than functions and logic. An object can be defined as a
data field that has unique attributes and behavior. It is the most popular programming model
among developers. It is well suited for programs that are large, complex, and actively updated
or maintained.

The three OOP principles are as follows:

 Encapsulation
 Inheritance
 Polymorphism

1. ENCAPSULATION:

Encapsulation is a process of combining data and functions in a single unit called class. This is to
prevent direct access to data, access is only provided through functions and classes. This is one
popular principle of OOP that helps in hiding. One way to think about encapsulation is as a
protective wrapper that prevents the code and data from being accessed from outside the
wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-
defined interface.

2. INHERITANCE:

Capability of class to derive characteristics and properties of other class is called inheritance.
Inheritance is one of the most important principles of OOP. Inheritance is a feature in which
new classes are created from existing ones. New class created is called derived-class or child-
class and existing class is called base-class or parent-class. Derived class is said to be inherited
class.
3. POLYMORPHISM:

Using operators or functions in different ways depending on what they are operating on is
called polymorphism. It is an important principle in OOP. It simply means more than one form.
That is, same function or operator behaves differently in different scenarios.

Q7. WHAT ARE TWO CONTROL STATEMENTS?

THE IF STATEMENT

The Java if statement works much like the IF statement in any other language. Further,it is
syntactically identical to the if statements in C, C++, and C#. Its simplest form is shown here

if(condition) statement;

Here, condition is a Boolean expression. If condition is true, then the statement is executed. If
condition is false, then the statement is bypassed. Here is an example:

if(num < 100) println("num is less than 100");

In this case, if num contains a value that is less than 100, the conditional expression is true, and
println( ) will execute. If num contains a value greater than or equal to 100, then the println( )
method is bypassed. Java defines a full complement of relational operators which may be used
in a conditional expression.
FOR LOOP

Loop statements are an important part of any programming language. Java provides a variety of
loop constructs. The most versatile is for loop. If you are familiar with C, C++, or C#, then you
will be pleased to know that for loop in Java works the same way it does in those languages.

SYNTAX

For (initialization; condition; iteration) {

//for loop body

The initializations portion sets variable to an initial value. The condition is a Boolean expression.
If the outcome of that test is true, for loop continues to iterate. If it is false, the loop
terminates. The iteration expression determines how the loop control variable is changed each
time the loop iterates.

EXAMPLE:

class Example {

public static void main ( string args [ ] ) {

int x;

for(x=0; x<10; x=x+1) {

system.out.println (“This is x: “ + x);

Q8. WHAT ARE JAVA CLASS LIBRARIES?


Java Class Libraries are a comprehensive set of reusable classes, interfaces, and APIs that come bundled
with the Java Development Kit (JDK). These libraries provide a wealth of functionality that covers various
domains, including networking, file I/O, database connectivity, graphical user interfaces (GUI), data
structures, cryptography, and more. They serve as a foundation for building Java applications, offering
ready-to-use components and tools to developers.

println( ) and print( ) methods are members of the System class, which is a class predefined by Java that
is automatically included in your programs. In the larger view, the Java environment relies on several
built-in class libraries that contain many built-in methods that provide support for such things as I/O,
string handling, networking, and graphics. The standard classes also provide support for windowed
output. Thus, Java as a totality is a combination of the Java language itself, plus its standard classes. As
you will see, the class libraries provide much of the functionality that comes with Java. Indeed, part of
becoming a Java programmer is learning to use the standard Java classes.
CHAPTER 3
Q9. WHAT ARE CHARACTERS?

CHARACTERS:

Characters are represented using the char data type. The char data type is a 16-bit unsigned
integer that represents Unicode characters. Unicode is a character encoding standard that
supports a vast range of characters, including letters, digits, symbols, and special characters
from various writing systems around the world.

Here are some key points about characters in Java:

Declaration and Initialization:

To declare a char variable, you can use the following syntax:

char myChar;

To initialize a char variable with a specific character, use single quotes ('):

char myChar = 'A';

Q10. WHAT ARE VARIBALES?

VARIABLE:

The variable is the basic unit of storage in a Java program. A variable is defined by the
combination of an identifier, a type, and an optional initializer. In addition, all variables have a
scope, which defines their visibility, and a lifetime.

Declaring a Variable:

type identifier [ = value][, identifier [= value] ...] ;

The type is the name of a class or interface. The identifier is the name of the variable. You can
initialize the variable by specifying an equal sign and a value. Keep in mind that the initialization
expression must result in a value of the same type as that specified for the variable. To declare
more than one variable of the specified type, use a comma-separated list.
Q11. EXPLAIN ARRAYS WITH EXAMPLES.

In Java, Array is a group of like-typed variables referred to by a common name. Arrays in Java
work differently than they do in C/C++. Arrays of any type can be created and may have one or
more dimensions. A specific element in an array is accessed by its index.

 In Java, all arrays are dynamically allocated.


 Arrays may be stored in contiguous memory
 Since arrays are objects in Java, we can find their length using the object property
length.
 A Java array variable can also be declared like other variables with [] after the data
type.
 The variables in the array are ordered, and each has an index beginning with 0.
 Java array can also be used as a static field, a local variable, or a method parameter.

ONE-DIMENSIONAL ARRAYS
type var-name[ ];

An array declaration has two components: the type and the name, type declares the element
type of the array. The element type determines the data type of each element that comprises
the array.

Example:

class Main {
public static void main(String args[]) {
int[] a=new int[3];//declaration
a[0]=10;//initialization
a[1]=20;
a[2]=30;
System.out.println("One dimensional array elements are");
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
TWO-DIMENSIONAL ARRAY
Two-dimensional arrays store the data in rows and columns:

MULTIDIMENSIONAL ARRAYS:

Multidimensional arrays are arrays of arrays with each element of the array holding the
reference of other arrays. These are also known as Jagged Arrays. A multidimensional array is
created by appending one set of square brackets ([]) per dimension.

Syntax of Multidimensional Array

Syntax to declare Java Multidimensional Arrays as mentioned below:

datatype arrayrefvariable[ ][ ];

Example:

class GFG {
public static void main(String[] args)
{
int[][] arr = new int[3][3];

System.out.println("Number of Rows:"+arr.length);

System.out.println("Number of Columns:” arr[0].length);


}
}

CHAPTER 6
Q13. DEFINE AND EXPLAIN CONSTUCTORS WITH THE HELP OF PROGRAM.
CONSTRUCTORS:

A constructor is a special method that is called when an object is created. It is used to initialize
the object's state and perform any necessary setup tasks. Constructors have the same name as
the class they belong to and don’t have a return type.

In Java, constructors are special methods within a class that are invoked when an object of that
class is created using the 'new' keyword. Constructors play a crucial role in initializing the state
of objects and ensuring that they are in a valid and usable state upon creation. The constructor
method shares the same name as the class and may take parameters to initialize the object's
attributes. They do not have a return type not even 'void'. Constructors are often employed to
set the initial values of instance variables and perform any necessary setup operations.

EXAMPLE:

public class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public String getName() {

return name;

public int getAge() {

return age;

public static void main(String[] args) {

Person person1 = new Person("Ramsha", 25);


String person1Name = person1.getName();

int person1Age = person1.getAge();

System.out.println("Person 1 Name: " + person1Name);

System.out.println("Person 1 Age: " + person1Age);

Person person2 = new Person("Kinza",23 );

System.out.println("Person 2 Name: " + person2.getName());

System.out.println("Person 2 Age: " + person2.getAge());

In this program:

 The “Person” class has two private instance variables (‘name’ and ‘age’) that represent
the attributes of a person.
 The class has a parameterized constructor that takes two parameters (‘name’ and ‘age’)
and initializes the instance variables accordingly using the ‘this’ keyword.
 Getter methods (‘getName’ and ‘getAge’) are provided to access the values of the
private variables from outside the class.
 In the ‘main’ method, we demonstrate how to create two ‘Person’ objects (‘person1’
and ‘person2’) using the constructor and access their information.

Q14. WHAT IS GARBAGE COLLECTOR IN JAVA?

In C/C++, a programmer is responsible for both the creation and destruction of objects. Usually,
programmer neglects the destruction of useless objects. Due to this negligence, at a certain
point, sufficient memory may not be available to create new objects, and the entire program
will terminate abnormally, causing OutOfMemoryErrors.

But in Java, the programmer need not care for all those objects which are no longer in use.
Garbage collector destroys these objects. The main objective of Garbage Collector is to free
heap memory by destroying unreachable objects. The garbage collector is the best example of
the Daemon thread as it is always running in the background.

Q14. WHAT IS FINALIZE METHOD?

Sometimes an object will need to perform some action when it is destroyed. For example, if an
object is holding some non-Java resource such as a file handle or window character font, then
you might want to make sure these resources are freed before an object is destroyed. To
handle such situations, Java provides a mechanism called finalization. By using finalization, you
can define specific actions that will occur when an object is just about to be reclaimed by the
garbage collector.

To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that
method whenever it is about to recycle an object of that class. Inside the finalize( ) method you
will specify those actions that must be performed before an object is destroyed. The garbage
collector runs periodically, checking for objects that are no longer referenced by any running
state or indirectly through other referenced objects. Right before an asset is freed, the Java run
time calls the finalize( ) method on the object.

The finalize( ) method has this general form:

Protected void finalize () {

// finalization code

CHAPTER 9
Q15. EXPLAIN PACKAGES IN DETAIL.

PACKAGES:

A set of classes and interfaces grouped together are known as Packages in JAVA. The name
itself defines that pack (group) of related types such as classes, sub-packages, enumeration,
annotations, and interfaces that provide name-space management. Every class is a part of a
certain package. When you need to use an existing class, you need to add the package within
the Java program.

Packages are used for:

 Preventing naming conflicts. For example there can be two classes with name Employee
in two packages, college.staff.cse.Employee and college.staff.ee.Employee
 Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
 Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses. A
default member (without any access specifier) is accessible by classes in the same
package only.
 Packages can be considered as data encapsulation (or data-hiding).

You can categorize packages into:


 Built-in Packages
 User-defined Packages

DEFINING A PACKAGE

To create a package is quite easy: simply include a package command as the first statement in a
Java source file. Any classes declared within that file will belong to the specified package. The
package statement defines a name space in which classes are stored. While the default package
is fine for short, sample programs, it is not compatible for real applications. Most of the time,
you will define a package for your code.

This is the general form of the package statement:

package pkg;

Java uses file system directories to store packages. For example, the .class files for any classes
you declare to be part of MyPackage must be stored in a directory called MyPackage.

IMPORTING PACKAGES

To import the java package into a class, we need to use the java import keyword which is used
to access the package and its classes into the java program.

syntax:

import package.name.ClassName; // To import a certain class only

import package.name.* // To import the whole package

EXAMPLE

package greeting; // creating package

public class Greet{

public void msg() {

System.out.println("Hello");

package Java;

import greeting.*; //importing package

class Main {

public static void main(String args[]) {


Greet g = new Greet();

g.msg();

Q16. EXPLAIN VARIABLE IN INTERFACES.

In java, an interface is a completely abstract class. An interface is a container of abstract


methods and static final variables. The interface contains the static final variables. The variables
defined in an interface can not be modified by the class that implements the interface, but it
may use as it defined in the interface.

 The variable in an interface is public, static, and final by default.


 If any variable in an interface is defined without public, static, and final keywords then,
the compiler automatically adds the same.
 No access modifier is allowed except the public for interface variables.
 Every variable of an interface must be initialized in the interface itself.
 The class that implements an interface can not modify the interface variable, but it may
use as it defined in the interface.

You might also like