• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Hashcode Problem

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am tried to complie this pgm,but I am getting errors. Why?

public class Wr
{
public static void main(String s[])
{
String str = new String("Hello World");

Double b2 = Double.valueOf("3.5");

Integer b4 = new Integer("77");

int x = 14;

int obj1 = str.hashcode();
int obj2 = b2.hashcode();
int obj3 = b4.hashcode();
int obj4 = x.hashcode();

System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println(obj4);
}
}



Wr.java:13: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.String
int obj1 = str.hashcode();
^
Wr.java:14: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.Double
int obj2 = b2.hashcode();
^
Wr.java:15: cannot resolve symbol
symbol : method hashcode ()
location: class java.lang.Integer
int obj3 = b4.hashcode();
^
3 errors
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method is named "hashCode()" -- with a capital "C". Case is significant in Java. You're also going to have problems with this line:

int obj4 = x.hashcode();

because "x" is an int, because you can't call methods on ints or other primitive types.

One more thing, while you're listening: There's essentially never any reason to write

String str = new String("Hello World");

in Java. Strings are immutable so don't need to be copied. Instead, just write

String str = "Hello World";
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Niyas
The 'c' in your hascode() method should be a capital 'C'. Change your code to

Also, an int it a literal and does not have methods so your line

will not work.

Steve
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Now it is working. Thanks for your reply.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic