This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Dan's Mock Exam

 
pioneer
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));
}
}
 
pioneer
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • 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
pioneer
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
 
pioneer
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
pioneer
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • 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
 
pioneer
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 ]
 
It's a tiny ad. At least, that's what she said.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!