Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's Mock Exam

 
Salim Mohamed
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic