File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== operator

 
cybel sheriden
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Steven Bell
Ranch Hand
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic