background preloader

Java fundamentals

Facebook Twitter

The things that can be asked in an interview

Java - When to use LinkedList<> over ArrayList<>? Singleton. Singleton classes represent objects for which only one single instance should exist.

Singleton

The important question here is this: will any actual harm come to the system if more than 1 object is created? If the answer is "no" (and it usually is), then there's no need whatsoever to use a singleton. If the answer is "yes", then you will need to consider it. The main point is that a singleton should only be used if it's really necessary. Here's an article from Oracle describing them. Example 1 This is the preferred style of implementing singletons. Public enum SantaClaus { INSTANCE; public void distributePresents(){ } public static void main(String... aArgs){ SantaClaus fatGuy = SantaClaus.INSTANCE; fatGuy.distributePresents(); } } Example 2 Here's an alternate style.

Public final class Universe { public static Universe getInstance() { return fINSTANCE; } private static final Universe fINSTANCE = new Universe(); private Universe() { } } Generics, Inheritance, and Subtypes (The Java™ Tutorials > Learning the Java Language > Generics (Updated)) As you already know, it is possible to assign an object of one type to an object of another type provided that the types are compatible.

Generics, Inheritance, and Subtypes (The Java™ Tutorials > Learning the Java Language > Generics (Updated))

Wildcards (The Java™ Tutorials > Bonus > Generics) Consider the problem of writing a routine that prints out all the elements in a collection.

Wildcards (The Java™ Tutorials > Bonus > Generics)

Here's how you might write it in an older version of the language (i.e., a pre-5.0 release): void printCollection(Collection c) { Iterator i = c.iterator(); for (k = 0; k < c.size(); k++) { System.out.println(i.next()); } } And here is a naive attempt at writing it using generics (and the new for loop syntax): The problem is that this new version is much less useful than the old one.

Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which, as we've just demonstrated, is not a supertype of all kinds of collections! 5 Reasons to Use Composition over Inheritance in Java and OOP. Favor composition over inheritance is a one of the popular object oriented design principle, which helps to create flexible and maintainable code in Java and other object oriented languages.

5 Reasons to Use Composition over Inheritance in Java and OOP

Many times I have seen people suggesting use composition instead of inheritance, in fact my favorite books like Head first Design Patterns, also advocates this design principle. Head first books, has its own way of explaining, why composition is better than inheritance and though its long its quite interesting and informative. It was the first chapter of this book, which helped me a lot on understanding this key OOPS concept. Inheritance versus composition: Which one should you choose? One of the fundamental activities of an object-oriented design is establishing relationships between classes.

Inheritance versus composition: Which one should you choose?

Two fundamental ways to relate classes are inheritance and composition. Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition. This article will compare these two approaches to relating classes and will provide guidelines on their use. First, some background on the meaning of inheritance and composition. About inheritance In this article, I'll be talking about single inheritance through class extension, as in: class Fruit { class Apple extends Fruit { How to create Immutable Class and Object in Java - Tutorial Example. We Help Coders Get Hired. For every idea you have at an interview, it's super important to be able to evaluate its efficiency.

We Help Coders Get Hired

Efficiency is measured by computing the underlying algorithm's time and memory complexity. Why is complexity so important? Solving problems is not just about finding a way to compute the correct answer. The solution should work quickly enough and with reasonable memory usage. Otherwise, even the smartest solution is not useful. Algorithm Tutorials. Computational Complexity: Section 1 By misofTopCoder Member In this article I'll try to introduce you to the area of computation complexity.

Algorithm Tutorials

The article will be a bit long before we get to the actual formal definitions because I feel that the rationale behind these definitions needs to be explained as well - and that understanding the rationale is even more important than the definitions alone. Algorithm Tutorials. Hibernate: Understanding Lazy Initialization. Hibernate offers two types of initializations.

Hibernate: Understanding Lazy Initialization

First one is Eager and the second one in Lazy initialization. On the one hand Eager initialization fetches an object and all of its related objects at load time, but on the other hand, in Lazy initialization related objects information fetched only when it is asked. Thus in terms of performance Lazy is better than Eager. It requires minimum database hits as it only loads an object's one-to-many and many-to-many relationships when these fields are accessed. Simply Singleton. The Singleton pattern is deceptively simple, even and especially for Java developers.

Simply Singleton

In this classic JavaWorld article, David Geary demonstrates how Java developers implement singletons, with code examples for multithreading, classloaders, and serialization using the Singleton pattern. He concludes with a look at implementing singleton registries in order to specify singletons at runtime. Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples.

Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. 10 Singleton Pattern Interview Questions in Java - Answered. Why Enum Singleton are better in Java. How to write Thread-Safe Code in Java. How to Iterate Over a Map in Java.