Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashcode Problem

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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";
 
Steve McCann
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes, Now it is working. Thanks for your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic