wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char c = 'b'-'a' --No Compile error?????????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char c = Watch "char c = New topic
Author

char c = 'b'-'a' --No Compile error??????????

neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
hi
can anyone tell me why in the following code the statement char c = 'b'-'a' has no compile error
'b'-'a' might get converted to int and there is no cast either
then why doesn't compiler complain bout it
even if the substraction is in char's range a unary operator converts it to int

class testit {
public static void main (String args[]) {
byte primitiveByte = 1;
char primitiveChar = 'b'-'a';//why No compile error here ???
int primitiveInt = 1;
short primitiveShort = 1;
String s = "1";
Integer i1 = new Integer(primitiveByte);
Integer i2 = new Integer(primitiveChar);
Integer i3 = new Integer(primitiveShort);
Integer i4 = new Integer(primitiveInt);
Integer i5 = new Integer(s);
int p1 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();
System.out.print(p1);
}}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by neeta mathur:
can anyone tell me why in the following code the statement char c = 'b'-'a' has no compile error

A literal int can be assigned without explicit cast to a narrower primitive (byte, short, or char) if it falls within range. 'b'-'a' is a literal int that falls within char's range. However, 'a'-'b' does not fall within char's range, and a compiler error results.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
Note:

char primitiveChar = (char) 'a'-'b';//
Does not compile

char primitiveChar = (char) ('a'-'b');//
DOES compile (cast on final result)


This:
public static void main(String[] args)
{
char a = 'a';
char b = 'b';
char c = b - a;
}
Does not compile either


class Test5
{
public static void main(String[] args)
{
char a = 'a';
char b = 'b';
char c = (char) (b - a);
}
}
This does
Ed Villamizar
Ranch Hand

Joined: Sep 02, 2004
Posts: 47
Originally posted by marc weber:

A literal int can be assigned without explicit cast to a narrower primitive (byte, short, or char) if it falls within range. 'b'-'a' is a literal int that falls within char's range. However, 'a'-'b' does not fall within char's range, and a compiler error results.


Just wanted to point out that it looks like if it were the *compiler* making the subtraction 'b'-'a' = 1 and using this result as if it were a literal.


MBA, PMP<br />SCJP, SCJD<br />ITIL Foundations Certified.
neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
thank u guys,
had known literal types before
Piyush Jain
Ranch Hand

Joined: Apr 25, 2003
Posts: 60
Hi,

Reading all the replies, I am confused about what exactly we are trying to say here.

now I get that 'b' - 'a' does fall in the char's range and will not give any compiler error because the result an int can be converted implicitly to char.

but now here what Tom wrote:

char primitiveChar = (char)('a'-'b');

why would this be compiling since a-b is out of range even if we are casting it explicitly. i suppose if it is compiling its not gonna run. please clarify.

Also Tom,

the first subroutine you wrote why would that not compile, isn't it in the range now? is it because b-a is not a literal here and will not be converted implicitly to a lower range from int?

i am not clear about What we call as Literal?

Thanks
Piyush
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
>why would this be compiling since a-b is out of range even if we are >casting it explicitly. i suppose if it is compiling its not gonna run. >please clarify.

It IS out of range when the compiler computes the 'a' - 'b' (it is -1). But now you say cast it- casting it means take whatever bit value you have and lop off the left hand side. The match takes place as an integer (I believe, you should check this). So you lop it off.

You should look at the output of the value, that would be curious to see.

This also brings up.. what if your math goes out of range?

What if you add MAXINT-1 to MAXINT -1 does that give you a compile error if you cast it (int) (<maxint -1> + (<maxint -1> ? It overflows the virtual machine before it gets to the cast I would think ,so it would fail.. curious...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tom Tolman:
You should look at the output of the value, that would be curious to see.

Exactly: 'a'-'b' gives an int value of -1 (which is "literal" because the exact value is known at compile time). In binary, this is 32 1's. Since this is out of the range of char (which can only be positive), it cannot be assigned to char without a cast. With a cast to a 16-bit char, it discards the 16 left-most 1's, leaving us with char's maximum value of 2 raised to the 16th minus 1, which is 65535. This char value is apparently represented as a question mark.

 
Consider Paul's rocket mass heater.
 
subject: char c = 'b'-'a' --No Compile error??????????
 
Similar Threads
Implicit conversions
Dan's Wrapper Exam Question
Small Doubt
Need help on Questions of wrapper classes
Confusion with Long Constructor