Write a program to count the occurrences of an element in an array is one of the basic question which will be asked in an Java Interview. After the introduction of Collection framework performing this operation is a pretty easy task. Let see how we can count the occurrence using HashMap. [Read more…]
Both the Integer.parseInt() and Integer.valueOf() method is used to convert a String into Integer in Java, both does the same work then why there is a need for two different method will be the question here. They both does the same job but has a slight difference among them. Lets look into the difference between parseInt vs valueOf in Java [Read more…]
What is Tower of Hanoi ?
Tower of Hanoi is also called as Tower of Brahma or Lucas Tower. It is one of the most popular problem which makes you understand the power of recursion. Tower of Hanoi is a mathematical puzzle which consist of 3 poles and number of discs of different sizes. Initially all the discs will be places in the single pole with the largest disc at the bottom and smallest on the top. We need to move all the disc from the first pole to the third pole with the smallest disc at the top and the largest at the bottom under the below conditions
- Only one disc can be moved at a time.
- Larger disc cannot be placed on a smaller disc.
Multiple Inheritance is nothing but one class extending more than one class. In Java Multiple Inheritance is not supported due to the reason it will be quiet complex to manage the dependencies. Lets look into the most famous problem called “Diamond Problem“ which occurs if Java supported Multiple Inheritance.
Write a program to Check if a number is even or odd is one of basic question which will be asked in the interview for fresher, anyone can solve this but always this question comes with a twist given at the end stating that we should not use the modulo or division operator in Java. Before getting into the actual code to begin with lets use the modulo and division operation to check if the given number is odd or even. [Read more…]
1. What is a Constructor in Java?
Constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.
2. What are the Rules for defining a constructor?
- Constructor name should be the same as the class name
- It cannot contain any return type
- It can have all Access Modifiers are allowed (private , public, protected, default)
- It Cannot have any Non Access Modifiers (final ,static, abstract, synchronized)
- No return statement is allowed
- It can take any number of parameters
- Constructor can throw exception, we can have throws clause
1. What is static keyword in Java?
Static is a Non Access Modifier. Static can be applied to variable, method, nested class and initialization blocks (static block).
2. What is a static variable ?
- A Static variable gets memory allocated only once during the time of class loading.
- All the instance of the class share the same copy of the variable, a static variable can be accessed directly by calling “<<ClassName>>.<<VariableName>>” without need to create instance for the class.
- value of a static variable will be common for all instances
Polymorphism is the ability to take more than one form. Polymorphism is one of the most important concept in OOPS ( Object Oriented Programming Concepts). Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
In Java, there are 2 ways by which you can achieve polymorphic behavior
1. Method Overloading
2. Method Overriding [Read more…]
Immutable class is the one whose state cannot be changed after it is created. The best example for this is String,Integer class in Java, once instantiated the value never be changed.
Immutable class has lots of advantages such as they can be used for the caching purposes as you don’t need to worry about the value changes. Immutable class is thread-safe and hence you don’t need to worry about synchronization issues and can be easily used in the multi-threaded environment. [Read more…]
How a HashMap Works internally has become a popular question in almost all the interview. As almost everybody knows how to use a HashMap or the difference between HashMap and Hashtable. But many fails when the question is how does a hashmap internally works.
So the answer to the question how does a hashmap works is that is it works based on the hashing principle but it is not as simple as it sounds. Hashing is the mechanism of assigning the unique code to a variable or attribute using some algorithm to enable easy retrieval. A true hashing mechanism should always return the same hashCode() when it is applied to the same object. [Read more…]