Answer the folllowing questions: 1. Explain the compilation process for Java sou
ID: 3707540 • Letter: A
Question
Answer the folllowing questions:
1. Explain the compilation process for Java source code. Make sure to discuss the following in your answer: bytecode, Java Virtual Machine, *.java and *.class file extensions.
2. Discuss how java programs are in terms of performance and security compared to other native programming languages such as C or C++.
3. Explain what primitive type and reference type mean in Java. Write sample code to support your explanations. Provide the output of your sample code as well.
4. Explain what a package is in Java. What are public, private and protected access specifiers in Java? What do we mean by “package private”? How are the names of public classes assoc?ated with the files they are included?
5. Write a demo code to show how the continue and break statements with labels work in java. Make sure to include the output of your program and explain the result.
6. Write a sample class with at least three constructors and show when these constructors will be invoked.
7. Write a class that includes a function that is overloaded at least three times. Show how to invoke each one of these overloads.
8. Show how exceptions are thrown and caught in Java. When does the finally block get executed? Show with a sample code if the finally block is executed if we have a return statement in the try block.
9. Write a sample code to explain the difference between a String, StringBuilder and StringBuffer in Java.
Explanation / Answer
Hey there.... I try to solve all your questions but limited time and policy restrict to do so.
Ans 1. In Java, programs are not compiled into executable files; they are compiled into bytecode then
the bytecode gets saved on the disk with the file extension .class.
When the program is to be run, the bytecode is converted into machine code using JVM.
The result is machine code which is then fed to the memory and is executed.
Java code needs to be compiled twice in order to be executed:
a)write your code in *.java file
b) then it compiled to bytecode.
b) When the bytecode is run, it needs to be converted to machine code that is *.class.
The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when needed the first time.
This is different from other languages like C/C++ where programs are to be compiled to machine code and
linked to create an executable file before it can be executed.
-------------------------------------------------------------------------------------------------------------------------
Ans 2. c ++ 1.Write once, compile anywhere (WOCA).
2. C++ is more nearer to hardware then Java
3. there is no guarantee that C++ program that's written for one platform will perform on another.
That's why C++ has got so many cross-platform libraries to make the transition easier.
4. C++ support destructors, which is automatically invoked when the object is destroyed.
Java -- 1. Write once, run anywhere/everywhere (WORA/WORE).
2. Java has method overloading, but no operator overloading just like c++.
3. Java on the other hand compiles to the JVM (Java Virtual Machine) bytecode.
Java Virtual Machine is an abstraction layer software placed between the physical machine, and the actual program.
4. Java takes more memory space than C++.
5. Java support automatic garbage collection. It does not support destructors as C++ does.
------------------------------------------------------------------------------------------------------------------------
3. Ans. Primitive Data Types :
Predefined by the language and named by a keyword
Total no = 8
*boolean
*char
*byte
*short
*integer
*long
*float
*double
Reference/Object Data Types :
*Created using defined constructors of the classes
*Used to access objects
*Default value of any reference variable is null
*Reference variable can be used to refer to any object of the declared type or any compatible type.
for example - Primitive data type
int it = 7;
int it2 = it;
Making it2 hold a value 7 still.
But for reference data type
Point p = new Point(1.9, 2.9);
Point q = p;
q will be holding address of object p.
Similarly the address is passed to methods for reference type whereas value will be passed for primitive type.
---------------------------------------------------------------------------------------------------
4. ans. Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for:
a)Preventing naming conflicts.
b)Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
c)Providing controlled access
d)Packages can be considered as data encapsulation.
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.
and user-defined are those who are defined by users or programmers.
Private Access Modifier - Private
Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Example
public class Blogger {
private String format;
public String getFormat() {
return this.format;
}
public void setFormat(String format) {
this.format = format;
}
}
Public Access Modifier - Public
A class, method, constructor, interface, etc. declared public can be accessed from any other class.
Example
public static void main(String[] arguments) {
// common used public class...
}
Protected Access Modifier - Protected
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses
in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces.
Example
class Player {
protected boolean openSpeaker(Speaker sp) {
// implementation details
}
}
class StreamingPlayer {
boolean openSpeaker(Speaker sp) {
// implementation details
}
}
Summary of all of above access modifiers
| Class | Package | Subclass | Subclass | World
| | |(same pkg)|(diff pkg)|
————————————+———————+—————————+——————————+——————————+————————
public | + | + | + | + | +
————————————+———————+—————————+——————————+——————————+————————
protected | + | + | + | + |
————————————+———————+—————————+——————————+——————————+————————
no modifier | + | + | + | |
————————————+———————+—————————+——————————+——————————+————————
private | + | | | |
+ : accessible
blank : not accessible
------------------------------------------------------------------------------------
5 Ans: Break: In Java, break is majorly used for:
a. Terminate a sequence in a switch statement
b. To exit a loop.
c. Used as a “civilized” form of goto.
Example:
// Java program to illustrate using
// break to exit a loop
class BreakLoopDemo
{
public static void main(String args[])
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++)
{
// terminate loop when i is 5.
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.
Continue: You might want to continue running the loop but stop processing the remainder of the code in its body for this
particular iteration.
Example:
// Java program to illustrate using
// continue in an if statement
class ContinueDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
// If the number is even
// skip and continue
if (i%2 == 0)
continue;
// If number is odd, print it
System.out.print(i + " ");
}
}
}
Output:
1 3 5 7 9
------------------------------------------------------------------------------------------------------
6 Ans. A constructor in Java is a method which is used used to initialize objects. Constructor method of a class has the same name as that of the class, they are called or
invoked when an object of a class is created and can't be called explicitly.
class Sample {
String name;
int a;
//1st constructor method
Sample() {
System.out.println("1st Constructor method called.");
}
//2nd constructor method
Sample(String s) {
name=s;
System.out.println("2nd Constructor method called."+name);
}
//3rd constructor method
Sample(int i) {
a=i;
System.out.println("3rd Constructor method called."+a);
}
public static void main(String[] args) {
Sample object1 = new Sample(); // Creating an object
Sample object2 = new Sample("Java");
Sample object3 = new Sample(9);
}
}
Output:
1st Constructor method called.
2nd Constructor method called.Java
3rd Constructor method called.9
-------------------------------------------------------------------------------