Java
Java
The Java Development Kit (JDK) is a software development environment used for developing
and running Java applications and applets. It includes JRE and development tools.
JDK = JRE + Development Tools
JDK has JRE, an interpreter/loader (Java), a compiler (javac), an archiver (jar) and other tools
needed for Java Development.
The Java Runtime Environment (JRE) provides the minimum requirements for
executing/running (not develop) a Java application. It includes JVM, core library classes and
supporting files. JRE is a container and JVM is the content. JRE physically exists. JVM virtually
exists.
JRE = JVM + core library classes
The Java Virtual Machine is responsible for executing a java program line by line, hence it’s also
called as interpreter. It loads, verifies and executes the code.
2.
A variable which is created inside the class but outside the method, is known as instance
variable. Instance variable doesn't get memory at compile time. It gets memory at run time
when object(instance) is created. That is why, it is known as instance variable.
Since main method is like an entry point, JVM doesn’t want to create an instance of main
method every time while running the program. To avoid that we are declaring that as static.
Yes, but JVM will look for the method declared with public static void main (String args[])
7. Can main method be declared ‘final’?
Yes, main method can be declared final, synchronized, strictfp modifier in the signature.
8. Can main method be declared private, protected?
No, we can’t. It reduces the visibility of main method and it’s not allowed.
9. Why Java doesn’t support Multiple Inheritance?
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java. Consider a scenario where A, B and C are three classes. The C class inherits A and B
classes. If A and B classes have same method and you call it from child class object, there will be
ambiguity to call method of A or B class. This will result in compile time error.
10. Why Java doesn’t support call by reference?
Java is strictly call by value. Say for example, a variable is created, we assign a value to it. Now if
we pass that value to a method and try to change the value in the method. Now java will
modify the value inside the method, java will try to create a copy of that variable while passing
it as a variable. The original value of the variable is left unmodified. This is not the case with call
by reference, if we pass an object as reference and try to access and modify the same variable,
then the original value of the variable is changed.
11. Difference between Compiler and Interpreter
Compiler Interpreter
It scans the whole code and translates the It scans the code line by line and translates to
whole program into machine readable form machine readable form.
The compilation/scanning time is high and The scanning time faster for single line. But
execution time is less. the execution time is high
The compiler saves an executable file after Interpreter doesn’t save any file.
successful compilation. But the interpreter needs to translate the
The executable file can be reused in the source code every time.
future if there are no changes in the source
code. No need for compilation in the future.
Needs extra memory to save the executable Interpreter doesn’t require such extra
file memory
14. Method Overloading. Ways in which you can achieve Method Overloading. Advantages
of Method Overloading.
If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.
String Literals
String s1 = “Hello”; // cached in string pool
String s2 = “Hello”; // now s2 will also point to the same string obj in string pool
s1 == s2 is true
Strings are stored in a special memory called String Pool. Say we have another String s2
= “Hello”. Previously, String s1 value is cached in the String pool. Now JVM will go and
check the String pool whether “Hello” is already present. Since its already there s2 will
also point to the same object in the string pool.
Creating String by calling the constructor
String s3 = new String(“Hello”); // this string is not interned
s1 == s3 is false
JVM does not search the string pool for “Hello” and assign the reference to the object
s1. So s3 is totally pointing to a new object in the heap. It won’t create in the string
pool.
To make JVM search the string for “Hello” we need to call intern() after creating a string
using constructor. Now it will return true.
class Bike {
final void run () {System.out.println("running...");}
}
Finally block is used after the try and catch in exception handling. It is used to execute
important code such as closing connection, stream etc., This block is always whether the
exception is handled or not.
Finalize method is used to perform clean up processing just before the object is garbage
collected. Garbage Collector always calls just before the deletion/destroying the object
which is eligible for Garbage Collection. It is present in the object class (which is the super
class of all Java classes)
21. Difference between throw and throws
No Throw Throws
.
1. Java throw keyword is used to explicitly Java throws keyword is used to
throw an exception. declare an exception.
2. Throw is used within the method. Throws is used with the method
signature.
3. Cannot throw multiple exceptions at the can declare multiple exceptions e.g.
same time. public void method()throws
IOException, SQLException.
By providing only setter or getter method, you can make the class read-only or
write-only.
1) Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and static
methods also.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
8) A Java abstract class acan have class Members of a Java interface are public by
members like private, protected, etc. default.
o Java LinkedList class uses doubly linked list to store the elements.
43. How do you convert String to int and int to String in Java?
Difference in normal and singleton class in terms of instantiation is that, For normal
class we use constructor, whereas for singleton class we use getInstance() method.
class Singleton
{
// static variable single_instance of type Singleton
private static Singleton single_instance = null;
return single_instance;
}
}
class Main
{
public static void main(String args[])
{
// instantiating Singleton class with variable x
Singleton x = Singleton.getInstance();
Deep Comparison: Suppose a class provides its own implementation of equals() method in
order to compare the Objects of that class w.r.t state of the Objects. That means data members (i.e.
fields) of Objects are to be compared with one another. Such Comparison based on data members
is known as deep comparison.
It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing
based collections like HashMap, HashSet, HashTable….etc. This method must be
overridden in every class which overrides equals() method.
If two Objects are equal, according to the equals(Object) method, then hashCode() method
must produce the same Integer on each of the two Objects.
If two Objects are unequal, according to the the equals(Object) method, It is not
necessary the Integer value produced by hashCode() method on each of the two
Objects will be distinct.
SOAP REST
Simple Object Access Protocol depends REST supports both XML and JSON formats
primarily on XML to provide messaging for communication
services.
SOAP uses different protocol for REST is an architecture which uses HTTP
communication like HTTP, SMTP, FTP actions and methods for communication
SOAP is a heavy weight protocol REST messages are smaller in size, since it
predominantly uses JSON than XML. So it’s a
lightweight component.