Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code has an error please help

 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values are equal & both are from different objects


What values?
What do you think you are comparing on line 1?
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic