Sie sind auf Seite 1von 9

Question 1: Describe the following:

a) Multi-threading.

b) Significance of Java Bytecode

Answer:

a) Multithreading is the ability of an application to perform multiple tasks at the same time.
You can create multithreading programs using Java. Multithreading is the ability of a
program or an operating system process to manage its use by more than one user at a time
and to even manage multiple requests by the same user without having to have multiple
copies of the programming running in the computer. Each user request for a program or
system service (and here a user can also be another program) is kept track of as a thread with
a separate identity. As programs work on behalf of the initial request for that thread and are
interrupted by other requests, the status of work on behalf of that thread is kept track of until
the work is completed. Multithreading in java is a process of executing multiple threads
simultaneously. Thread is basically a lightweight sub-process, a smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking. But we use
multithreading than multiprocessing because threads share a common 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 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 exception occur in a
single thread.

b) A programming language has to be portable and also provide enough ground for security.
The key factor that allows Java to solve both the security and the portability problems is that
the output of a Java compiler is not executable code. Rather, it is bytecode. 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). That is, in its standard form, the JVM is an
interpreter for bytecode. Bytecodes are the machine language of the Java virtual machine.
When a JVM loads a class file, it gets one stream of bytecodes for each method in the class.
The bytecodes streams are stored in the method area of the JVM. The bytecodes for a method
are executed when that method is invoked during the course of running the program. They
can be executed by intepretation, just-in-time compiling, or any other technique that was
chosen by the designer of a particular JVM.
Question 2: Differentiate Break and Continue statements in Java with example program.

Answer:

Break Statement- By using break you can force immediate termination of a loop bypassing
the conditional expression and any remaining code in the body of the loop. When a break
statement is encountered inside a loop, the loop is terminated and program control resumes at
the next statement following the loop. Here is a simple example:

// Using break to exit a loop


Class BreakLoop{
Public static void main(String args[]) {
For(int i=0;i<=100; i++)
{
If(i==10) break;
System.out.println(“i:”+i);
}
System.out.println(“Loop complete”);
}
}
Output is
I:0
I:1
I:2
I:3
I:4
I:5
I:6
I:7
I:8
I:9

Continue Statement - Sometimes it is useful to force an early iteration of a loop. That is you
might want to continue running the loop, but stop processing the remainder of the code in its
body for this particular iteration. The continue statement performs such an action. Int while
and do-while loops, a continue statement causes control to be transferred directly to the
conditional expression that controls the loop in a for loop, control goes first to the iteration
portion of the for statement and then to the conditional expression. For all three loops any
intermediate code is bypassed.
Here is an example program that uses continue two numbers to be printed on each line:

// Demostrate continue
Class Continue{
public static void main(String args[]) {
for(int i=0;i<10;i++)
{
System.out.println(i=””);
If(i%2==0) continue;
System.out.println(“”);
}
}
}
Output is
01
23
45
67
89
Question 3: Differentiate between packages and Interfaces

Answer:

There is a huge difference between packages and interfaces. A package is a scoping


mechanism for classes within a group. So that the names used for classes in one package do
not conflict with the names (even if they are the same) of classes and public methods in
another package.

Packages

Definition: A package is a grouping of related types providing access protection and name
space management. Note that a type refers to classes, interfaces, enumerations, and
annotation types. Enumerations and annotation types are special kinds of classes and
interfaces, respectively, so types are often referred to in this lesson simply as classes and
interfaces.

A package is just a mechanism for grouping objects; it is very similar to grouping items
within a folder or directory on a file system. A class is found within a package, but this does
not have an impact on the class' behaviour (review the "package" access level for a slight
exception).

Creating a Package:

To create a package, you choose a name for the package and put a package statement with
that name at the top of every source file that contains the types (classes, interfaces,
enumerations, and annotation types) that you want to include in the package.

You can include non-public types in the same file as a public type (this is strongly
discouraged, unless the non-public types are small and closely related to the public type), but
only the public type will be accessible from outside of the package. All the top-level, non-
public types will be package private.

Interface

An interface, however, is a .java file that is used (implemented) by another class to tell the
outside world that it conforms to a certain specification. For example, you might have a
"Runnable" interface that has a "run()" method in it, by having a class that is "Runnable"
(implements Runnable) anyone using that class knows that it must have a "run()" method
defined. This is used when you have several different classes that have the same interface.

Interfaces have more in common with abstract classes than they do with packages. An
interface, by definition, cannot have any implemented methods; an abstract class, in contrast,
can define some methods and leave some methods to be implemented by a subclass. Also, a
class can implement many interfaces, but can only extend one (abstract) class.

Interfaces have nothing to do with scoping... they have to do with having behaviours cross a
set of classes outside of an inheritance chain. Of course, behaviours are shared via
inheritance from parent class to child class - but what do you do if you want to share
behaviours across a group of classes that are not related via inheritance? The answer is
interfaces. A good common example of this is logging or capturing metrics or creating plug-
in - whenever behaviour (and not attributes) are common across a set of classes.
Question 4: What are Applets? What are the restrictions of Applets? Describe about applet
class.

Answer:

Applets

An applet is a little application. Prior to the World Wide Web, the built-in writing and
drawing programs that came with Windows were sometimes called "applets." On the Web,
using Java, the object-oriented programming language, an applet is a small program that can
be sent along with a Web page to a user. Java applets can perform interactive animations,
immediate calculations, or other simple tasks without having to send a user request back to
the server

An applet is a small Internet-based program written in Java, a programming language for the
Web, which can be downloaded by any computer. The applet is also able to run in HTML.
The applet is usually embedded in an HTML page on a Web site and can be executed from
within a browser

Applets Restrictions

Applets have many restrictions over the areas of security because they are obtained from
remote machines

1. If we are running an applet from a provider who is not trustworthy than security is
important.
2. Applet itself cannot run or modify any application on the local system.
3. Applet has no access to client-side resources such as files, OS etc.
4. Applets can have special privileges. They have to be tagged as trusted applets and they
must be registered to APS applet security manager.
5. Applet has little restriction when it comes to communication. It can communicate only
with the machine from which it was loaded.
6. Applet cannot work with native methods
7. Applet can only extract information about client machine is its name, version OS, version
etc.

Class Applet

An applet is a small program that is intended not to be run on its own, but rather to be
embedded inside another application.

The Applet class must be the superclass of any applet that is to be embedded in a Web page
or viewed by the Java Applet Viewer. The Applet class provides a standard interface between
applets and their environment.

The Applet class doesn't provide any genuine applicability on its own. Its purpose is to
provide a super class from which custom applet classes are extended.
Question 5: Compare JDBC and ODBC.

Answer:

Compare JDBC and ODBC

ODBC is an open interface which can be used by any application to communicate with any
database system, while JDBC is an interface that can be used by Java applications to access
databases. Therefore, unlike JDBC, ODBC is language independent. But by using JDBC-to-
ODBC bridge Java applications can also talk to any ODBC compliant database

Typically, software applications are written in a specific programming language (such as


Java, C#, etc.), while databases accept queries in some other database specific language (such
as SQL). Therefore, when a software application needs to access data in a database, an
interface that can translate languages to each other (application and database) is required.
Otherwise, application programmers need to learn and incorporate database specific
languages within their applications. ODBC (Open Database Connectivity) and JDBC (Java
DatabBase Connectivity) are two interfaces that solve this specific problem.

ODBC : ODBC is an interface to access database management systems (DBMS). ODBC was
developed by SQL Access Group in 1992 at a time there were no standard medium to
communicate between a database and an application. It does not depend on a specific
programming language or a database system or an operating system. Programmers can use
ODBC interface to write applications that can query data from any database, regardless of the
environment it is running on or the type of DBMS it uses

ODBC is a multidatabase API for programs that use SQL statements to access data. An
ODBC-based program can access heterogeneous databases without needing source code
changes-one program can retrieve and store content in different vendors' databases via the
ODBC interface. ODBC thus provides database-neutral delivery of both SQL and database
content. Be aware, however, that you must load ODBC driver software for each vendor's
database you want to access.

JDBC is a Data API developed for Java programming language. It was released with JDK
1.1 by Sun Microsystems (Java’s initial owners). And its current version is JDBC 4.0
(currently distributed with JAVA SE6). Java.sql and javax.sql packages contain the JDBC
classes. It is an interface that helps a client to access a database system, by providing methods
to query and update data in the databases. JDBC is more suitable for object oriented
databases. You can access any ODBC-compliant database by using the JDBC-to-ODBC
bridge.

JDBC is a collection of database access middleware drivers that provide Java programs with
a call-level SQL API. Java applets and applications can use the drivers' API to connect to
databases, store and retrieve database content and execute stored procedures, thus making
JDBC a Java-enabled delivery mechanism for SQL. JDBC is to Java programs what ODBC is
to programs written in languages other than Java. In fact, JDBC's design is based on ODBC's.
Question 6: Describe about Java Beans and BeanBox.

Answer:

Java Beans

JavaBeans is the software component architecture for Java. It allows constructing


applications efficiently by configuring and connecting components called Beans. The Beans
need to be written and tested with a rich set of mechanisms for interaction between objects,
along with common actions that most objects will need to support such as persistence and
event handling. It allows you to try out both the BDK example beans and your own newly
created beans.

A Java Bean is a java class that should follow following conventions:

 It should have a no-arg constructor.


 It should be Serializable.
 It should provide methods to set and get the values of the properties, known as getter
and setter methods.

According to Java white paper, it is a reusable software component. A bean encapsulates


many objects into one object, so we can access this object from multiple places. Moreover, it
provides the easy maintenance.

In computing based on the Java Platform, JavaBeans are classes that encapsulate many
objects into a single object (the bean). They are serializable, have a zero-argument
constructor, and allow access to properties using getter and setter methods. The name "Bean"
was given to encompass this standard, which aims to create reusable software components for
Java.

BeanBox

BeanBox is a test container in BDK. We can use Bean Box to create Application, Applet, and
New Beans. We can lay out, edit and interconnect beans visually. That is one of the benefits
of Java beans is the capability for the beans to be used in visual application builder tools.

Before installing BDK, we have to install JDK version 1.1 or later first and then install BDK.
There is a "beanbox" directory inside of BDK (assume "BDK" is a directory for JavaBeans").
BeanBox comes with the batch file, called run.bat in the "beanbox" directory. If we type in
"run", it would displays 3 windows:

The first window is Toolbox, it lists all the beans registered for use with BeaBox. We also
can write our own beans and add them to this window.

The second window is BeanBox, it is the main container window.

Last window is Propertysheet, it lists the properties associated with the current selected bean.
It is responsible for providing the visual editing capabilities of the BeanBox.
The following is the picture of the windows from Starting and Using the BeanBox

What can BeanBox do?

The BeanBox allows you to:


Drop beans onto a composition window
Resize and move beans around
Edit the exported properties of a bean
Run a customizer to configure a bean
Connect a bean event source to an event handler method
Connect together bound properties on different beans
Save and restore sets of beans
Making applets from beans
Get an introspection report on a bean
Add new beans from JAR files

Using the BeanBox

To create a simple application using BeanBox we are going to select "OurButton" from the
ToolBox window and place it somewhere in the BeanBox window. We are going to edit this
bean in the Propertysheet window, change the label to "Start". We are going to select another
button, and call it "Stop". Now let's get something exciting, select the "Juggler" bean. A
useful function of BeanBox is that we can wire beans together using events, we can do that
visully, this makes it very easy. Click the "Start" button, go to Edit and Event, select Action
Event Menu item, and then select Action performed. Now we can see a line originating from
the "Start" button that moves as the mouse moved around. We are going to move the mouse
over the "Juggle" bean. Click to connect the button bean's action event to the "Juggle" bean.
Select "start Juggle" method from Dialog box. We are going to do the same thing for "Stop"
button. Now, we can click "stop" and "start" to control the animation.

Das könnte Ihnen auch gefallen