• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

code has an error please help

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic