Showing posts with label toc2. Show all posts
Showing posts with label toc2. Show all posts

Saturday, 14 May 2011

Implementing equals

Object class have methods like equals() and hashcode(). These methods have their own contracts which have to be fulfilled to override them.
 
Default implementation of equals() in object class

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.

 
 
Contract of equals 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.

Steps to be taken when implementing equals()

Sunday, 17 April 2011

Set tutorial in java

A Set(in the API reference documentation) is a Collection(in the API reference documentation) 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

 

Operations on Sets

 

SortedSet

 

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

 

Monday, 14 March 2011

Servlets (index)

JSP (index)

Reflections (toc or index)

Before we being reflections we should note the following:

Note of caution for reflection
Now we can proceed further.

Examining Classes

Following is required to examine a class:


  • Retrieving Class Objects
    First things first. Before you can find out anything about a class, you must first retrieve its corresponding Class object.

  • Getting the Class Name
    It's easy to find out the name of a Class object. All you have to do is invoke the getName method.



  • Discovering Class Modifiers
    This section shows you the methods you need to call to find out what modifiers a particular class has.



  • Finding Superclasses
    In this section you'll learn how to retrieve all of the Class objects for the ancestors of a given class.



  • Identifying the Interfaces Implemented by a Class
    If you want to find out what interfaces a class implements, then check out this section.



  • Examining Interfaces
    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.



  • Identifying Class Fields
    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.



  • Discovering Class Constructors
    This section, which introduces the Constructor class, explains how to get information about a class's contructors.



  • Obtaining Method Information
    To find out about a class's methods, you need to retrieve the correspondingMethod 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 a Button 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


    Working with Arrays

    Summary of Classes

    Saturday, 12 March 2011

    The SortedSet interface

    A SortedSet(in the API reference documentation)is a Set(in the API reference documentation)that maintains its elements in ascending order, sorted according to the elements' natural order, or according to a Comparator 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 Comparator used 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.

    Dealing with Exceptions

    How to throw Exceptions

    Custom Exceptions

    Nested Exceptions

    Runtime Exceptions--The Controversy

    Exceptions and control flow

    Finally and catch

    Some practices related to exception: