File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Mock Exam

Salim Mohamed
Ranch Hand

Joined: Sep 19, 2002
Posts: 55
How can method m1() can return char whereas its "contract" is to return byte ?
static byte m1() {
final char c = 'b'-'a';
return c; // 1
}
static byte m2() {
final short s = 2;
return s; // 2
}
static byte m3(final char c) {
return c; // 3
}
static byte m4(final short s) {
return s; // 4
}
public static void main(String[] args) {
char c = 'd'-'a';
short s = 4;
System.out.print(""+m1()+m2()+m3(c)+m4(s));
}
}
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
In m1(), the value of 'c' requires only one byte. So compiler allows that and automatically does the casting while returning. Same reason is true for m2(). However, compiler doesn't allow m3() & m4() as the size of the argument will be decided during run time, which can be more than 1 byte. So compiler complains about 'loss of precision'.
I have recently started preparing for the certification exam...Please correct me, if I'm wrong here.
Thanks,
Dg
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
dg,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Valentin,
Sorry about that pardner...I have corrected it.
One more thing...what this membership status 'greenhorns' means. Like can I choose my membership status or ???
cheers
Deep
Ambapali Pal
Ranch Hand

Joined: Dec 17, 2002
Posts: 47
Hi Salim and Deep,
Does this code compile ? It is giving me error for all 4 methods. I thought atleast m2() should compile because s is a compile time constant and its value can be fitted in a byte.
I donot know if something is wrong with my compiler. I am using jdk1.2.1

Ambapali
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
It compiles for me for m1() and m2(). One more thing, if you remove 'final' from 'c' & 's' in m1() and m2() respectively, then you will again get the same error. Reason is: if they are declared as final and values are initialized as compile time constant then compiler calculates their values (whether any precision loss) would be there or not. If yes, it gives errors. I bet even if they are declared as final but not initialized then compiler will complain!
btw, i'm using java "1.4.1_01"
cheers,
deep
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
One more thing...what this membership status 'greenhorns' means. Like can I choose my membership status or ???
You will be considered a greenhorn until you reach 30 posts. After that, you'll be implicitely promoted to a ranch hand
Harry Kong
Ranch Hand

Joined: Dec 06, 2002
Posts: 41
Yea, that was also the question that confused me for a minute. But when I read the remark on the question everything became clear. I am guessing it is not a common knowledge or obvious that declaring a variable "final" makes it "tight" variable. Now this is just how I think of it. I guess it would be easy to think of "final" as a shrink wrap in this case.
[ December 31, 2002: Message edited by: Harry Kong ]

SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Mock Exam