• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
neeta mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u guys,
had known literal types before
 
Piyush Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic