Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java BigDecimal hashCode example 
 

In this example bigdecimal class hashCode() method working is shown. Method returns integer values, both negative & positive. For every number method generates different hash codes.

 

Java BigDecimal hashCode example

                         

In this example bigdecimal class hashCode() method working is shown. Method returns integer values, both negative & positive. For every number method generates different hash codes. Method generated hash codes does not match when similar values possess different scales.
Method generates NumberFormatException when it finds any String character value instead of a number value. 

Syntax for using the method: public int hashCode()
System.out.println(bigdecimal_objectName.hashCode()); 
                         or
int i = (this.object).hashCode();

Java_BigDecimal_hashCode.java

import java.math.BigDecimal;
import java.text.NumberFormat;

public class {
  public static void main(String args[]) {

    NumberFormat num = NumberFormat.getInstance();
    num.setMinimumFractionDigits(3);

    BigDecimal buf_0 = new BigDecimal("rose".length()),
    buf_1 = new BigDecimal("india".length());

    System.out.println("BigDecimal object buf_0 value" +
      " :" + buf_0
      "\nBigDecimal object buf_1 value :" + buf_1);
    System.out.println("\nhash code generated for buf_0 " +
      "object value : " + buf_0.hashCode()
      "\nhash code generated for buf_1 object value : "
      + buf_1.hashCode());

    BigDecimal x = new BigDecimal(10.32455666888),
    y = new BigDecimal(10.32455666888),
    ray[] x, y };
    int i = ray[0].hashCode(),
    j = ray[1].hashCode();

    System.out.println("\nhash code generated for x " +
        "object value : " + i);
    System.out.println("hash code generated for y " +
        "object value : : " + j);

    System.out.println("\n");
    boolean evaluate_0 = x.equals(y);
    if (evaluate_0 == true) {
      System.out.println(x.equals(y" : objects x " +
          "& y are equal");
    else
      System.out.println(x.equals(y" : objects x & " +
          "y are not equal");

    x = new BigDecimal(num.format(x));
    x = new BigDecimal(num.format(y));

    System.out.println("\nResult after formating");
    boolean evaluate_1 = x.equals(y);
    if (evaluate_1 == true) {
      System.out.println(x.equals(y" : objects x & " +
          "y are equal");
    else
      System.out.println(x.equals(y" : objects x & y" +
          " are not equal");
  }
}

Download the code

                         

» View all related tutorials
Related Tags: c class object objects integer method type default double decimal gd value return int if biginteger example work bigdecimal values

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.