This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code has an error please help Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "code has an error please help" Watch "code has an error please help" New topic
Author

code has an error please help

Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
hi

please find the code as below: -

File---> Typ.java

code:-

public class Typ
{
int i=0;
public static void main(String args[])
{
System.out.println(i);
}
}


FIle:----> RefOpt.java

code: -

public class RefOpt
{
int x,y;
public static void main(String args[])
{
RefOpt acc =new RefOpt();
Typ obj=new Typ();

obj.i=100;
acc.x=100;

if(obj==acc) //1
{
System.out.println("Same Object belonging");
}
else
{
System.out.println("NOt Same Object belonging");
}

if (obj.equals(acc)) // 2
{
System.out.println("Equals");
}
else
{
System.out.println("Not Equals");
}
}
}

here //1 genrates the error Incomparible type.
//2 genrates the output "not equals".

Why is it happing.The values are equal & both are from different objects

Please correctthe code.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The values are equal & both are from different objects


What values?
What do you think you are comparing on line 1?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
For the == operator to work ... I think the two objects compared must be of same type or must belong to the same hierarchy .

Here your two object reference variables are vastly different and neither reference type extends the other , so theres no way both reference variables can refer to same object(== tests if they refer to same object). Hence == cannot be applied.
Kevin Liu
Greenhorn

Joined: Mar 17, 2007
Posts: 10
File---> Typ.java

code:-

public class Typ
{
int i=0;//static int i = 0;
public static void main(String args[])
{
System.out.println(i); //or System.out.println(new Typ().i);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code has an error please help
 
Similar Threads
XMLEncoder - Stackoverflowerror - overriding equals
Why HashSet is adding duplicates even if I get same hashcode for two obects & passes true fromequals
Problem with hash map
the famous ; error revisited
Using java.util.HashSet for Custom Class