1.
Indicate the
difference between PATH and CLASSPATH.(WBUT 2013)
Sol:
PATH and CLASSPATH
are two most important environment variable of Java
environment which is used to find JDK binaries used to compile and run Java in
windows and Linux and class files which are compile
Java byte codes.
Difference between PATH and CLASSPATH in Java
Here are some of
the common difference between PATH vs CLASSPATH in Java :
1)Main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand CLASSPATH environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in .class file.
2) In order to set PATH in Java you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directory where you have put either your .class file or JAR file which is required by your Java application.
3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.
4) PATH environment variable is used by operating system to find any binary or command typed in shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.
1)Main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand CLASSPATH environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in .class file.
2) In order to set PATH in Java you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directory where you have put either your .class file or JAR file which is required by your Java application.
3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.
4) PATH environment variable is used by operating system to find any binary or command typed in shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.
2.
Illustrate the uses of
'integer' and 'Integer' keyword. (WBUT 2013)
Sol:
int
is a keyword which designates the 32 bit signed integer primitive type.
The
java.lang.Integer
class
is the nominal wrapper class when you need to store an int value but an
object reference is required.
Syntax:
int <variable-name> = <integer-value>;
Ex: int i = 65;
OR
(By default, the int
data type is a 32-bit signed two's complement integer, which has a minimum value of -231
and a maximum value of 231-1. In Java SE 8 and later, we can use the int
data type to represent an unsigned 32-bit integer,
which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int
data type as an unsigned integer.
See the section The Number Classes for more information. Static methods like compareUnsigned
, divideUnsigned
etc have been
added to the Integer
class to support the arithmetic operations for unsigned integers)
3.
What is the difference among final, finally
and finalize? (WBUT 2013)
Sol:
final:
final is a keyword. The variable decleared as final should be initialized only once and
cannot be changed. Java classes declared as final cannot be extended. Methods declared
as finalcannot be overridden.
finally:
finally is a block. The finally block always executes when the try block exits.
This ensures that the finally block is executed even if an unexpected exception occurs.
But finally is useful for more than just exception handling - it allows the programmer to
avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting
cleanup code in a finally block is always a good practice, even when no exceptions are
anticipated.
finalize:
finalize is a method. Before an object is garbage collected, the runtime system
calls its finalize() method. You can write system resources release code in finalize() method
before getting garbage collected.
4.
Discuss each part of the following statement:
System.out.println( );
(WBUT
2013)
Sol:
System.out.println prints the
argument passed, into the System.out, which is generally stdout.
·
System – is a final class and cannot be inherited. As
per javadoc, “…Among the facilities provided by the
System
class are standard input, standard output, and error output streams; access to
externally defined properties and environment variables; a means of loading
files and libraries; and a utility method for quickly copying a portion of an array…”
·
out – is a static
member field of System
class and is of type PrintStream.
Its access specifiers are
public final. This
gets instantiated during startup and gets mapped with standard output console
of the host. This stream is open by itself immediately after its instantiation
and ready to accept data.
·
println – println prints the argument passed to the
standard console and a newline. There are multiple println methods with
different arguments (overloading).
Every println makes a call to
print
method and adds a
newline. print
calls write()
and the story
goes on like that.
5.
What is the difference
between an instance member and a class member?
(WBUT 2013)
Sol:
Instance variables
and class variables are both member variables. They are both member variables
because they are both associated with a specific class. But, there are
differences between instance variables and class variables.
Instance variables
Instance variables belong to an
instance of a class. Another way of saying that is instance variables belong to
an object, since an object is an instance of a class. Every object has it’s own
copy of the instance variables. Here is what a declaration of an instance
variable would look like:
Ex:
class Taxes
{
int count;
/*...*/
}
Class variables – also known as static member variables
Class variables, however, only have one
copy of the variable(s) shared with all instances of the class. It’s important
to remember that class variables are also known as static member variables
in C++, Java, and C#. Each object of the class does not have its own copy of a
class variable. Instead, every object shares the one and only copy of
that class variable – and any changes made to that copy are seen by all of the
objects of that class. Here is what a class variable – or a static member
variable – would look like in C++:
Ex:
class Taxes
{
static int count;
/*...*/
}
Difference between class and instance variables
Now, it should be clear what the
difference between instance and class variables is. Class variables only have
one copy that is shared by all the different objects of a class, whereas every
object has it’s own personal copy of an instance variable. So, instance
variables across different objects can have different values whereas class
variables across different objects can have only one value.
Class and Instance variables are both Member variables
Here’s a little diagram to help you
remember the differences between instance and class variables:
1.
What is JAR? Write few
options used with JAR (WBUT 2013)
Sol:
1st Part:
A JAR (Java ARchive) file is a file
that contains the class, image, and sound files for a Java application or
applet gathered into a single file and possibly compressed. When a programmer
gets a Java program development kit, a small program or utility called
"jar" is included. The jar utility lets the programmer create, list,
or extract the individual files from a JAR file. In an enterprise, a Java
application can be started with a set of JAR files for use during execution. An
off-the-shelf open source package can be delivered as a JAR file and run with
XML data.
On the Web, a JAR file containing an
applet may accompany a Web page. By putting the applet components in a single
file and compressing that file, download time is saved. Ordinarily, a browser
user will not need to "open" or view a JAR file directly. It is
opened when the Web page is received and the applet is in some manner
initiated.
The JAR format is based on the
popular zip file format and is somewhat comparable to the Unix tar file.
2nd Part:
The basic format of the command for
creating a JAR file is:
jar cf jar-file input-file(s)
The options and arguments used in
this command are:
·
The c option indicates that you
want to create a JAR file.
·
The f option indicates that you
want the output to go to a file rather than to stdout.
·
jar-file is the name that you want the resulting
JAR file to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required.
·
The input-file(s) argument is a
space-separated list of one or more files that you want to include in your JAR
file. The input-file(s) argument can contain the wildcard *
symbol. If any of the "input-files" are directories, the contents of
those directories are added to the JAR archive recursively.
The c and f
options can appear in either order, but there must not be any space between
them.
This command will generate a
compressed JAR file and place it in the current directory. The command will
also generate a default manifest file for the JAR archive.
2.
What is polymorphism? Differentiate between
compile time and runtime polymorphism with the help of complete java program.
(WBUT 2013,2012)
Sol:
1st Part:
Polymorphism is an Oops concept which advice
use of common interface instead of concrete implementation while writing code.
When we program for interface our code is capable of handling any new
requirement or enhancement arise in near future due to new implementation of our
common interface. If we don't use common interface and rely on concrete
implementation, we always need to change and duplicate most of our code to
support new implementation. Its not only Java but other object oriented
language like C++ also supports polymorphism and it comes as fundamental along
with other OOPS concepts like Encapsulation , Abstraction and Inheritance.
Or
Polymorphism (from the Greek, meaning “many
forms”) is a special feature that allows one interface to be used for a general
class of actions. The specific action is determined by the exact nature of the
situation. More generally, the concept of polymorphism is often expressed by
the phrase “one interface, multiple methods”. This means that it is possible to
design a generic interface to a group of relative activities. This helps reduce
complexity by allowing the same interface to be used to specify a general class
of action. It is the compiler’s job to select the specific action (that is,
method) as it applies to each situation. User need only remember and utilize
the general interface.
How Polymorphism supported in Java
Java has excellent support of polymorphism in
terms of Inheritance, method overloading and method overriding. Method
overriding allows Java to invoke method based on a particular object at
run-time instead of declared type while coding. To get hold of concept let's
see an example of polymorphism in Java:
public class TradingSystem{
public String getDescription(){
return
"electronic trading system";
}
}
public class DirectMarketAccessSystem extends
TradingSystem{
public String getDescription(){
return "direct
market access system";
}
}
public class CommodityTradingSystem extends
TradingSystem{
public String getDescription(){
return "Futures
trading system";
}
}
2nd
Part:
In java language, polymorphism is essentially considered
into two versions.
A.
Compile time polymorphism (static binding or method
overloading)
B.
Runtime polymorphism (dynamic binding or method overriding)
A. Compile time polymorphism (static binding or method overloading)
As the meaning is implicit, this is used to write the
program in such a way, that flow of control is decided in compile time itself.
It is achieved using method overloading.
In method overloading, an object can have two or more
methods with same name, BUT, with their method parameters different. These
parameters may be different on two bases:
1) Parameter type: Type of method
parameters can be different. e.g. java.util.Math.max() function comes with
following versions:
public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
public static int Math.max(int a, int b){..}
public static long Math.max(long a, long b){..}
The actual method to be called is decided on compile
time based on parameters passed to function in program.
2) Parameter count: Functions accepting
different number of parameters. e.g. in employee management application, a
factory can have these methods:
EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String lastName){...}
Both methods have same name “create” but actual method
invoked will be based on parameters passed in program.
B. Runtime polymorphism (dynamic binding or method overriding)
Runtime polymorphism is essentially referred as
method overriding. Method overriding is a feature which you get when
you implement inheritance in your program.
A simple example can be from real world e.g. Animal. An
application can have Animal class, and its specialized sub classes like Cat and
Dog. These subclasses will override the default behavior provided by Animal
class + some of its own specific behavior.
public class Animal
{ public void
makeNoise() { System.out.println("Some
sound"); } } class Dog extends Animal{ public void
makeNoise() { System.out.println("Bark"); } } class Cat extends Animal{ public void
makeNoise() { System.out.println("Meawoo"); } } |
Now which makeNoise() method will be called, depends on
type of actual instance created on runtime e.g.
public class Demo { public static
void
main(String[]
args) { Animal a1
= new Cat(); a1.makeNoise();
//Prints Meowoo Animal a2
= new Dog(); a2.makeNoise();
//Prints Bark } } |
Important points:
1.
Polymorphism is the ability to create a variable, a function,
or an object that has more than one form.
2.
In java, polymorphism is divided into two parts : method
overloading and method overriding.
3.
Some may argue that method overloading is not polymorphism.
Then what does the term compile time “polymorphism” means??
4.
Another term operator overloading is also there, e.g. “+”
operator can be used to add two integers as well as concat two sub-strings.
Well, this is the only available support for operator overloading in java, and
you can not have your own custom defined operator overloading in java.
No comments:
Post a Comment