Saturday, 18 June 2011
Saturday, 14 May 2011
Implementing equals
The default implementation of equals() in Object class is based on the == operator: Two objects are equal if and only if they are the same object. Naturally, most classes should define their own alternative implementation of this important method.
Implementing equals() correctly is not straightforward. The equals() method has a contract that says the equality relation must meet these demands:
- It must be reflexive. For any reference x, x.equals(x) must return True.
- It must be symmetric. For any two nonnull references x and y, x.equals(y) should return the exact same value as y.equals(x).
- It must be transitive. For any three references x, y, and z, if x.equals(y) and y.equals(z) are True, then x.equals(z) must also return True.
- It should be consistent. For any two references x and y, x.equals(y) should return the same value if called repeatedly (unless, of course, either x or y were changed between the repeated invocations of equals()).
- For any nonnull reference x, x.equals(null) should return False.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Monday, 9 May 2011
Working with Directory tutorial
Following topics can be covered in this tutorial
- Creating a Directory recursively
- Copying a Directory or file
- Traversing files and directories
- Printing the directory hierarchy
- Display all the files in the directory
- Display all the directories in the directory
- Getting the Current Working Directory
- Getting the last modified time of a File or Directory
- Listing Files or Subdirectories
- Deleting a File
- Deleting a directory in java
- Checking whether directory is empty or not
- Getting the parent directory of a file in java
- Searching all files inside directory
Java IO tutorial
- Type of Steams in java
- The Stream Classes (Covers various classes present in java IO)
- Where do streams come from?
Saturday, 7 May 2011
Wednesday, 4 May 2011
Friday, 22 April 2011
OOP in java
Class in java
Object creation and destruction
Access Control in java
Nested classes
Inheritance in java
- Inheritance : cpp vs java
- Using super in java
- Multilevel inheritance : Calling order of Constructors
- Abstract classes
- Interfaces in java
- Multiple inheritance in java
- Inheritance among interfaces in java
- Multiple inheritance in java
- Abstract classes : cpp vs java
- Abstract classes vs Interfaces
- Abstract-Interface or skeletal implementations
- Various interfaces
- Final in java: Preventing inheritance
Thursday, 21 April 2011
Wednesday, 20 April 2011
Nested Class : Index
Tuesday, 19 April 2011
Generics : Index or TOC
Motivation behind Generics-Dealing with casting of objects
The Generics Facility
Creating a Generic class
Writing a Generic Method
Naming convention in generics
Generics are syntactic sugar - Type Erasure with Generics
Advantage of Generics
Subtyping a generic type
Wildcards in Generics
Generics classes in java vs templates in c++
Some mistakes to be avoided with generics:
Beginner's mistake of using Object as type parameter to make method generic
Generic methods: How generic are generic method?
Wildcards in Generics
Bounded parametric types
Covariance, contravariance, invariance
Upper bound boundedness in generics
Lower bound boundedness in generics
Multiple bounds in generics
Get and Set principle in generics
Restrictions on wildcards in generics
Expressing dependencies in type parameters : Wildcards vs type parameters
Class objects as type literals
Generic types are not covariant
Type erasure in generics
Making return type of method as generic
Covariant parameter types
Sunday, 17 April 2011
Set tutorial in java
Set
is a Collection
that cannot contain duplicate elements. Set models the mathematical set abstraction. The Set interface contains no methods other than those inherited from Collection. It adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set objects with different implementation types to be compared meaningfully. Two Set objects are equal if they contain the same elements.Set Interfaces
Set Implementations
- LinkedHashSet
Creating a set that retains the order of insertion - HashSet HashSet class in java
- TreeSet
TreeSet class in java – constructors
Converting ArrayList to HashSet
Operations on Sets
SortedSet
- SortedSet Interface
- SortedSet Operations
- Standard constructors for SortedSet interface
- Range-view operations on SortedSet interface
- Creating a sorted set
How is set implemented internally?
Set and serializations
Sets and synchronization
Iterators returned by set implementation and concurrent modification
When to use which set implementation?
Performance of set interface implementations
Tuesday, 12 April 2011
Monday, 14 March 2011
Reflections (toc or index)
Now we can proceed further.
Examining Classes
Following is required to examine a class:First things first. Before you can find out anything about a class, you must first retrieve its corresponding
Class object.It's easy to find out the name of a
Class object. All you have to do is invoke the getName method.This section shows you the methods you need to call to find out what modifiers a particular class has.
In this section you'll learn how to retrieve all of the
Class objects for the ancestors of a given class.If you want to find out what interfaces a class implements, then check out this section.
In this section you'll learn how to tell if a
Class object represents an interface or a class. You'll also get some tips on how to get more information about an interface.This section shows you how to discover what fields belong to a class, and how to find out more about these fields by accessing
Field objects.This section, which introduces the
Constructor class, explains how to get information about a class's contructors.To find out about a class's methods, you need to retrieve the corresponding
Method objects. This section shows you how to do this.Manipulating Objects
Software development tools, such as GUI builders and debuggers, need to manipulate objects at run time. For example, a GUI builder may allow the end-user to select aButton from a menu of components, create the Buttonobject, and then click the Button while running the application within the GUI builder. If you're in the business of creating software development tools, you'll want to take advantage of the reflection API features described in this lesson. - Creating Objects
How can you create an object if you don't know its class name until run time? You'll find the answer in this section. - Getting Field Values
In this section you'll learn how to get the values of an object's fields, even if you don't know the name of the object, or even its class, until run time. - Setting Field Values
Not only can you get field values at run time, you can also set them. This section shows you how. - Invoking Methods
This section shows you how to dynamically invoke methods. Given an object, you can find out what methods its class defines, and then invoke the methods.
Creating Objects
- Using No-Argument Constructors
- Using Constructors that Have Arguments
- Getting Field Values
- Setting Field Values
- Invoking Methods
Working with Arrays
Summary of Classes
Saturday, 12 March 2011
The SortedSet interface
SortedSetSetComparator provided at SortedSet creation time. (Natural order and Comparators are discussed in the previous section, on Object Ordering.) In addition to the normal Set operations, the Set interface provides operations for: - Range-view: Performs arbitrary range operations on the sorted set.
- Endpoints: Returns the first or last element in the sorted set.
- Comparator access: Returns the
Comparatorused to sort the set (if any).
The SortedSet interface is shown below:
public interface SortedSet extends Set {
// Range-view
SortedSet subSet(Object fromElement, Object toElement);
SortedSet headSet(Object toElement);
SortedSet tailSet(Object fromElement);
// Endpoints
Object first();
Object last();
// Comparator access
Comparator comparator();
}
SortedSet Operations
Standard Constructors
Range-view Operations on SortedSet
Example – Creating a SortedSet
Exceptions (TOC)
Introduction to exception
How java implements Exception handling?
Types of Exceptions in java
Checked vs unchecked vs Error type exceptions
Java's catching or throwing exception
This section discusses the reasoning behind this requirement and what it means to you and your Java programs.
- Example of class which has to deal with example
- Catching and handling exception with try-catch-finally
- Specifying the exceptions thrown by method using throws clause
Custom Exceptions
- Adding custom exceptions to the project design
- Creating the custom exception example
- Naming convention for exceptions in java
Nested Exceptions
Runtime Exceptions--The Controversy
Some practices related to exception: