File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes == operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "== operator" Watch "== operator" New topic
Author

== operator

cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
 
 
subject: == operator