java.util
by krishna
- Collection vs List vs Map vs Set
Collection List Map Set Duplicate Elements Duplicate Elements No Duplicate Keys No Duplicate Elements Extends Collection Each key can map to 1 value Extends Collection - HashMap vs HashTable vs HashSet vs TreeSet vs TreeMap
HashMap HashTable HashSet TreeSet TreeMap Implements Map Implements Map Implements Set Implements SortedSet Implements SortedMap Not Synchronized Synchronized Not Synchronized Not Synchronized Not Synchronized - LinkedHashMap vs LinkedHashSet vs LinkedList
LinkedHashMap LinkedHashSet LinkedList Extends HashMap Extends HashSet & implements Set Implements List Not Synchronized Not Synchronized Not Synchronized Fail fast behavior of an iterator cannot be guaranteed. Fail fast behavior of an iterator cannot be guaranteed. Fail fast behavior of an iterator cannot be guaranteed. - String vs StringBuffer vs StringTokenizer
String StringBuffer StringTokenizer Represents character strings. Is like a string, but can be modified. Extends Object & Implements Enumeration Immutable String Mutable String Are safe for use by multiple threads. Set of delimiters (characters that separate tokens) may be specified either at creation time. Methods are synchronized Fail fast behavior of an iterator cannot be guaranteed. - LinkedList vs ArrayList
LinkedList ArrayList Sequential accessing. Random accessing - Interface vs Abstract Class
Interface Abstract Class A class may implement several interfaces. A class may extend only one abstract class. An interface cannot provide any code at all, much less default code. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden. If all the various implementations share is the method signatures, then an interface works best. If the various implementations are all of a kind and share a common status and behavior, usually an abstract class works best. If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change. Interface_1 extends Interface_2, Interface_3 Class_1 extends Class_2 - What’s the main difference between a Vector and an ArrayList
Java Vector class is internally synchronized and ArrayList is not. - Java Serialization(java.io.Serializable)
The Java serialization mechanism illustrates two of the best characteristics of the Java programming language:
simplicity and flexibility
Serialization allows you to create persistent objects, that is, objects that can be stored and then reconstituted for later use.
Object serialization is the ability of an object to write a complete state of itself and of any objects that it references to an output stream, so that it can be recreated from the serialized representation at a later time. - What is Reflection?
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions. - OOPS concepts
Encapsulation: Allows changes to code to be more easily made since one can be confident that ‘outsiders’ are not relying on too many details. It has purpose information hiding & modularity.
Example: Private , protected & public on variable / class / interfaceInheritance: The process by which one object acquires the properties of another object
Example: ClassA extends ClassBPolymorphism: This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method) as it applies to each situation.
Example: method overriding from interface or abstract classSynchronization: A mechanism to guarantee that only one thread can access an object at a time. It has purpose to avoid problems that can arise when two threads can both modify the same object at the same time.
Information Hiding:Hiding details so as to reduce complexity. It allow private information and methods to be changed without affecting other objects and to make sure that private information and methods are accessed only through a public interface.
- Primary benefits of OO application frameworks
Modularity: Frameworks enhance modularity by encapsulating volatile implementation details behind stable interfaces. Framework modularity helps improve software quality by localizing the impact of design and implementation changes. This localization reduces the effort required to understand and maintain existing software.
Reusability: The stable interfaces provided by frameworks enhance reusability by defining generic components that can be reapplied to create new applications. Framework reusability leverages the domain knowledge and prior effort of experienced developers in order to avoid re-creating and re-validating common solutions to recurring application requirements and software design challenges. Reuse of framework components can yield substantial improvements in programmer productivity, as well as enhance the quality, performance, reliability and interoperability of software.
Extensibility: A framework enhances extensibility by providing explicit hook methods that allow applications to extend its stable interfaces. Hook methods systematically decouple the stable interfaces and behaviors of an application domain from the variations required by instantiations of an application in a particular context. Framework extensibility is essential to ensure timely customization of new application services and features.
Inversion of control: The run-time architecture of a framework is characterized by an “inversion of control.” This architecture enables canonical application processing steps to be customized by event handler objects that are invoked via the framework’s reactive dispatching mechanism. When events occur, the framework’s dispatcher reacts by invoking hook methods on pre-registered handler objects, which perform application-specific processing on the events. Inversion of control allows the framework (rather than each application) to determine which set of application-specific methods to invoke in response to external events (such as window messages arriving from end-users or packets arriving on communication ports). - Can an inner class declared inside of a method access local variables of this method?
It’s possible if these variables are final. - Why would you use a synchronized block vs. synchronized method?
Synchronized blocks place locks for shorter periods than synchronized methods. - Explain the usage of the keyword transient?
This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers). - What’s the difference between a queue and a stack?
Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule - Other Questions
- synchronized indicates that the method modifies the internal state of the class or the internal state of an instance of the class in a way that is not thread-safe.
- static that means a variable or method belongs to the class and not to the instances
- transient indicates a field that is not part of an object’s persistent state and needs not be serialized with the object
- throw to indicate a throw statement
- throws indicate a throws clause
- volatile indicates that the field is used by synchronized threads and that the compiler should not attempt to perform optimizations with it
- data abstraction groups the pieces of data that describe some entity, so that programmers can manipulate that data as a unit
- procedural abstraction has advantage when using a certain procedure, a programmer does not need to worry about all the details of how it performs its computations; he or she only needs to know how to call it and what it computes
- identity Unlike primitive values, objects possess a unique identity that makes them distinguishable from one another, even when they are in the same state ; e.g. Two strings, s1=”x” and s2=new String(s1), are equal but have different identity, so s1.equals(s2) is true but s1==s2 is false.
- multithreading A mechanism that provides support for multiple threads of execution to run concurrently within a program without interfering with each other
- synchronized class method does not run until Java obtains a lock on the class to ensure that no other threads can be modifying the class concurrently
- inner class A nested class whose instance exists within an instance of its enclosing class and has direct access to the instance members of its enclosing instance
- instance method A method that executes in the context of a particular object; it has access to the instance variables of the given object, and can refer to the object itself using the ‘this’ keyword, Any method that is invoked with respect to an instance of a class
- instance variable A data item present in all the instances of a class, normally used to implement associations and attributes, Any item of data that is associated with a particular object.
- final method to increase efficiency by eliminating dynamic method lookup
- finalize method method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state
- uninitialized object An object that has been declared but has not been assigned a value
- thread An object used in multithreaded software systems that represents a sequence of program execution & is a lightweight process
- polymorphic operation An operation where the program decides, every time an operation is called, which of several identically-named methods to invoke
- object-oriented paradigm An approach to software design and programming in which software is primarily thought of as a collection of classes that each have responsibilities for various operations, and which are instantiated at run time to create objects
- procedural paradigm organizes code into procedures that each manipulate different types of data
- instantiation The process of creating a new instance of a class
- invocation The process of calling a method , i.e., executing its body, passing arguments to be associated with the method’s formal parameters.
- subclassing The process of creating a new class that inherits from an existing class
Collection vs List vs Map vs Set Collection List Map Set Duplicate Elements Duplicate Elements No Duplicate Keys No Duplicate Elements Extends Collection Each key can map to 1 value Extends Collection HashMap vs HashTable vs HashSet vs TreeSet vs TreeMap HashMap HashTable HashSet TreeSet TreeMap Implements Map Implements Map Implements Set Implements SortedSet Implements…
Recent Comments
Archives
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- February 2012
- January 2012
- December 2011
- October 2011
- August 2011
- July 2011
- May 2011
- January 2011
- November 2010
- October 2010
- September 2010
- July 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- December 2007
- April 2007
- January 2007