• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

what rules for ==  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what rules apply to the == operator for compile-error
the conversion rules or the casting rules
i.e. if a = ( type) b is correct
and a = b gives error
will a == b give compile error ?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mateen
The according to the JLS section 15.21.3

In your example the cast you show will be accepted by the compiler (it will accept all casts) because the validity of a cast isn't checked until run time.
hope that helps you out
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited October 05, 2001).]
[This message has been edited by Dave Vick (edited October 05, 2001).]
 
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mateen Nasir:
what rules apply to the == operator for compile-error
the conversion rules or the casting rules
i.e. if a = ( type) b is correct
and a = b gives error
will a == b give compile error ?


No. You will not get a compile error.
Example:

This will give a compile error for line 1. If you comment out line 1, this will compile successfully and return false.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas
I think the example you give is a special case because one of the objects you use is an Object and anything can be cast to an Object. In the sample code I gave I got a compiler error at the lines I commented out because neither could be cast to the other. Or did I get the error for a different reason?

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mateen,
Ur message:
what rules apply to the == operator for compile-error
the conversion rules or the casting rules
i.e. if a = ( type) b is correct
and a = b gives error
will a == b give compile error ?
There wont be an compile error for the condition u have specified.
For eg,consider this class:
class tree
{
void leaf()
{System.out.println("tree leaf");}
}
class appletree extends tree
{
void leaf()
{System.out.println("apple-tree leaf");}
}
class pinetree extends tree
{
void leaf()
{System.out.println("pine-tree leaf");}
}

public class casting
{
public static void main(String[] args)
{
tree t = new tree();
appletree a = new appletree();
tree t1 = new appletree(); //1
//a = t1; // compile error,explicit casting needed.
a = (appletree)t1; //ok
if(a == t1) System.out.println("a==t1"); //ok
}
}
The output will be a==t1, there will not be any compile error. As 't1' is assigned to 'a' both references will point to same object. So a==t1 will return true. Hope this helps.
Regards,
Priyha.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mateen,
'cast' has a higher precedence than the equality operator '==' which means the cast will be applied first and then the comparison will be made.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
if a = ( type) b is correct
and a = b gives error
will a == b give compile error ?
*/

[This message has been edited by Marilyn deQueiroz (edited October 08, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

True, but comparing class LittleTest to class Test does not fulfill the requirement (that it be castable to the other class but not assignable)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello MAteen
The JLS answer your question in 15.21.3
" A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (�5.5). The run-time values of the two operands would necessarily be unequal.
"
In the example of Marilyn
t==bt compiles but t==lt won't so I would say that the compiler would complain only if the operands are not hierarquically related. I think this is esier to remember.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct. As long as one of the items can be legally cast to the other, the == will compile.
Here's a couple of examples:

Since a Button can't be cast to a String, you will get a compile time error on line 1 and line 2.

This will compile but will give a run time casting error at line 1.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!