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.

Ads

Tutorials   
Java Bigdecimal Java bigdecimal toBigIntegerExact example Java bigdecimal shortValue example Java bigdecimal shortValueExact Java bigdecimal signum example Java bigdecimal stripTrailingZeros example Java bigdecimal subtract Java bigdecimal subtract method example Java bigdecimal toBigInteger example Java bigdecimal toEngineeringString example Java bigdecimal toPlainString example Java bigdecimal toString example Java bigdecimal ulp example Java bigdecimal unscaledValue example Java bigdecimal valueOf double example Java bigdecimal valueOf long example Java bigdecimal valueOf long method Java bigdecimal setScale method example Java bigdecimal setScale example Java bigdecimal scaleByPowerOfTen example Java bigdecimal scale example Java bigdecimal round example Java bigdecimal remainder method example Java bigdecimal remainder example Java bigdecimal precision example Java bigdecimal pow method example Java bigdecimal pow example Java bigdecimal plus method example Java bigdecimal plus example Java bigdecimal negate method example Java bigdecimal negate example Java bigdecimal multiply method example Java bigdecimal multiply example Java BigDecimal movePointRight example Java bigdecimal movePointLeft example Java bigdecimal min example Java bigdecimal max example Java BigDecimal divideAndRemainder example Java BigDecimal divideAndRemainder method example Java BigDecimal divide examples Java BigDecimal divideToIntegral example Java BigDecimal divideToIntegral method example Java BigDecimal doubleValue example Java BigDecimal equals example Java BigDecimal hashCode example Java BigDecimal intValue example Java BigDecimal intValueExact example Java BigDecimal longValue example Java BigDecimal longValueExact example Java BigDecimal abs example
Ads

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

Advertisements

Share on Google+Share on Google+

Java BigDecimal hashCode example

Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java BigDecimal hashCode example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads