Wednesday, 31 January 2018

What is method overloading and method overriding?


Method Overloading : -In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.
-Method Overloading is to “add” or “extend” more to method’s behavior.
-It is a compile time polymorphism.
-The methods must have different signature.
-It may or may not need inheritance in Method Overloading
Method Overriding: -In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
-Method Overriding is to “Change” existing behavior of method.
-It is a run time polymorphism.
-The methods must have same signature.
-It always requires inheritance in Method Overriding.

What is the difference between ‘ == ’ and ‘equals()?

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

What is the difference between java.util.Date and java.sql.Date in Java?

Both are known as Date class, but there is some difference between java.util.Date and jaca.sql.Date, for example, Former is used whenever a Date is required in Java application while later is used to read and store DATE SQL type from the database.

Another significant difference is java.util.Date stores both date and time values, while java.sql.date only stores date information, without any time apart. According to the Javadoc specification, java.sql.date is a thin wrapper around a millisecond value that lets JDBC distinguish as an SQL DATE value.

 The meaning of SQL DATE, the millisecond values bound by a java.sql.Date instance must be ‘normalised’. However, setting the hours, minutes, seconds, and milliseconds to zero in the specific time zone with which the case is connected.

Where and Why you are using Collection on your project?

We are using Collection below listed reason....

1. For using interface to represent and access a collection e.g.
     use List to store ArrayList
     useMap to store HashMap.

2. For using the collection if we need a non-synchronised list, then we go for ArrayList and not Vector.

3. For synchronized collection we are using concurrent collection over it.

4. In case of any loop like for ,forEach we are using iterator in collection.

Tuesday, 30 January 2018

How do you locate memory usage from a Java program? How much of the percent is used?

The ability to use memory related methods from java.lang.Runtime class to get the free memory, total memory and maximum heap memory in Java.
From using these methods, we know how much percent of the heap is used and how much heap space is outstanding.
-Runtime.freeMemory() return the amount of free memory in bytes.
-Runtime.totalMemory() returns the total memory in bytes.
-Runtime.maxMemory() returns the maximum memory in bytes.

Why Java is not pure Object Oriented language?

Java is not a pure object oriented because it support primitive types such as int, byte, short, long etc. And java could have wrapper objects for the primitive types but just for the representation.

But we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.

Monday, 29 January 2018

what is the difference bet hashtable and Hashmap?

1)Hashtable is synchronized whereas hashmap is not synchronised. 
2)Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know. 
3)HashMap permits null values in it, while Hashtable doesn’t permits null values. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
4)One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

How many ways thread can implemented?

There are two different ways to specify which code to run in that Thread: 
-Implement the interface java.lang.Runnable .
- pass an instance of the class implementing it to the Thread constructor. Extend Thread itself and override its run() method.

Saturday, 27 January 2018

What is Serialization? How to achieve Serialization?

..Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. 
..It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies. 
Acheive Serialization:::
1.To persist data for future use.
2.To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
3.To "flatten" an object into array of bytes in memory.
4.To exchange data between applets and servlets.

5.To store user session in Web applications.

What is thread? How can we create thread? What type is better to create thread?

..A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class.

..The first way is to extend the Thread class, override the run() method with the code you want to execute, then create a new object from your class and call start(). The second method is to pass an implementation of the Runnable interface to the constructor of Thread, then call start().


.. Implementing Runnable Interface.

What is Singleton design pattern? How to create Singleton class?

..In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java Virtual Machine. It is used to provide global point of access to the object.
…. In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.
create singleton class———
1.Make constructor as private.

2.Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialisation in used to write this static method.

What is HashMap collision ? How we can solve?

In an HashMap the key is an object, that contains hashCode() and equals(Object) methods.

When you insert a new entry on the Map, it checks whether the hashCode is already known. Then, it will iterate through all objects with this hashcode, and test their equality with .equals(). If an equal object is found, the new value replace the old one. If not, it will create a new entry in the map.

Usually, talking about maps, you use collision when two objects have the same hashCode but they are different. They are internally stored in a list.
solve
Problem:

There are chances that the hashFunction returns same index for 2 different keys.  This is what we call a collision.

Solution:

Separate Chaining: Have an array of lists. If there is more than one key hashed to the same index, append to the list at that index in the array. A decent hash function guarantees that the keys are evenly distributed across the indices. This is what Java uses.
2-Choice: It is similar to Separate Chaining, just that there are 2 hash functions instead of 1. Get the indices from both the hash function. Compare the sizes of the list at these indices and append the new key to the smaller list. This can be extended to k-Choice.

Cuckoo hashing: You have 2 hash functions. Let the first hash function return x. If vacant, put the key K there, if not, try the second hash function and let's say it returns y, put the key there if vacant. If not, kick out the key L at y and store K there. Run both the hash functions on L and try the other index ignoring the index at which it previously it used. Repeat the process. If you cannot find a spot for it after 15 retries, put it in the overflow table.

Hashmap internal working?

In Java, a HashMap works on the concept of hashing.

A HashMap in Java stores both key and value objects, in a bucket. It is stored as an Entry object that implements Map.Entry interface.

The key object used in a HashMap has to provide implementation for hashCode() and equals() methods.

When put() method is used to store a key-value pair, the HashMap implementation calls hashCode() method on Key object to calculate a hash that is used to find a bucket where Entry object will be stored.

When get() method is used to retrieve a value stored against a key object, we first calculate a hash of Key object. Then we use this hash to find the bucket in which that particular key is stored.


Once Key object’s location is found, it may happen that more than one Key is stored in same location. So now we use equals() method to find the exact Key object. Once the exact Key object is found we use it to get Value object.

What is difference between Abstract class and Interface?

Abstract class

Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
.Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).
.When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.

Interface

.Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.
.A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.

.A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).