File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple question" Watch "simple question" New topic

simple question

rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
hi ,

class JSC201 {
static byte m1() {
final char c1 = '\u0001';
return c1; // 1
static byte m2(final char c2) {return c2;} // 2
public static void main(String[] args) {
char c3 = '\u0003';
System.out.print(""+m1()+m2(c3)); // 3

i understand why i'm getting the error on line no 2 cause the found return value is character and the return type is byte

but shouldn't i get the complile time error at line 1 also cause it is also
returning the character value even though the return type is byte....
[ September 05, 2007: Message edited by: deeksha mehra ]

Ranch Hand

Joined: Apr 04, 2007
Posts: 188
hi deeksha..

you are right. you should get a compiler error there also
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
No, line 1 compiles fine. The variable is char but it is a final that is assigned a literal, so it is resolved at compile time. Java knows that the return value cannot be any other than \u0001, or 1. And so it knows that it fits a byte.
Change that value to \u0101 and you will see it won't compile, because it knows the return value doesn't fit a byte.

SCJP 1.5
I agree. Here's the link:
subject: simple question
It's not a secret anymore!