• 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

== operator

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
== operator looks only for the bit pattren then why not test for a boolean and a char variable be performed(though they are of incompatible type) ....why does it give compiler error and not a false for the test performed going by the bit pattern.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler wants you to provide compatible types. Some languages let internal knowledge leak through, like false is zero and true is one, or maybe anything not zero. Java chose not to do that. They probably decided the kind of shortcut that lets you compare different types is too much opportunity for error.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is because the Sun gods (pun intended) said so. The long answer is as Stan described. The creators of Java decided that boolean and char are not compatible types. This means that your statement that "== operator looks only for the bit pattren" [sic] is not completely accurate. The == operator also looks at the variables' types before doing any comparison.

Layne
 
cybel sheriden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if == checks for compatible type variable before checking the bit pattern then why does the following code give a error. switch internally does == test to check the matching case value.



class test2
{

public static void amin(String[] args)
{
int a =128;
byte b=2;
if (!(a==b)) //this line of code compiles fine
switch(b)
{
case 128:
}
}
}
test2.java:11: possible loss of precision
found : int
required: byte
case 128:
^
1 error
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ints, bytes, chars, and shorts are all integer types. They can be compared and, with some restrictions, be set to the value of one another.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by cybel sheriden:
switch internally does == test to check the matching case value.



Well, not literally. There are two JVM instructions (tableswitch and lookupswitch) into which switches can be compiled, depending on the distribution of the cases. The compiler doesn't emit specific JVM equality test bytecodes.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As EFH implied above, the switch statement does not perform the same type of premotions that are done when using the == operator. When a byte is given to a switch statement, then the values for each case must fit the range of a byte.

Layne
 
reply
    Bookmark Topic Watch Topic
  • New Topic