[OCP 11 Complete Study Guide] [OCP 11 Programmer I Book] [OCP 11 Programmer II Book] [OCP 11 Practice Tests Book] [OCA 8 Book] [OCP 8 Book] [OCP 8 Practice Tests Book] [Blog]
Felipe Kunzler wrote:
According to that, shouldn't line 3 compile?
Felipe Kunzler wrote:
And shouldn't this last line give an error?
What does actually happen when Java is trying to compile the code in such cases?
Felipe Kunzler wrote:This should happen as well:
Felipe Kunzler wrote:Please correct me if I'm not correct.
Roel De Nijs wrote:There are many alternatives to change this code snippet and make it compile without errors. Can you find at least 3 different alternatives (without using methods from the Double class)?
Roel De Nijs wrote:Just one little question: is there a specific reason why you didn't include the Character wrapper class in the above overview as a Character is also considered to be a numeric type
Roel De Nijs wrote:Let's see if you have fully understand the above. Which lines in this code snippet will give a compiler error? And why?
Felipe Kunzler wrote:Would it be the following?
Felipe Kunzler wrote:This one just blew my mind right now!
Felipe Kunzler wrote:Your example got me thinking, and I realized that it is because the compiler only auto downcast if it is a compile time constant, so the compiler can check if the size fits that type!
Felipe Kunzler wrote:commenting about each line
line1: Error because the compiler cannot guarantee that i1 (runtime variable) fits in a short. The literal value 2_000 instead would be ok.
line2: Same reason, i2 is final but still runtime variable.
line3: Same here
line4: This one would work because it is compile time variable, thus, the compiler can check if the value fits in a short!
line5: Error, 32768 can't fit in a short.
Felipe Kunzler wrote:By the way, are those kind of conversion between primitives and wrappers covered in the OCA 8 exam?
As you can see this exam objective is (just like all others) very generally worded, so it's very hard to tell to what extent you need to know about wrapper classes.OCA8 Exam Topics wrote:Develop code that uses wrapper classes such as Boolean, Double, and Integer.
Roel De Nijs wrote:
Almost a perfect score! All your explanations are spot-on, except for line3. The local variable i3 is definitely a compile-time constant, but its value is not in the range of a short and therefore this line doesn't compile. If you change the value to e.g. 20_000, line3 will compile without errors as well.!
Roel De Nijs wrote:
As you can see this exam objective is (just like all others) very generally worded, so it's very hard to tell to what extent you need to know about wrapper classes.
Wrapper classes are a very popular topic on this forum and there are a few excellent topics about wrapper classes and related subjects like autoboxing and using the == operator. If you have a good, solid understanding of everything explained in this topic, this one and this one you'll be able to answer wrapper class related questions on the exam very confidently and (more importantly) flawlessly
Felipe Kunzler wrote:Ohh I see, I didn't actually know all the rules of when a variable becomes a compile-time constant, found this topic very helpful to clear this doubt.
Roel De Nijs wrote:Hi Felipe Kunzler,
First of all, a warm welcome to CodeRanch!
Felipe Kunzler wrote:This should happen as well:
That's really a very common mistake. And thus an all-time classic for the actual exam Many people think because an int fits into a double, an int can be converted into a Double (and/or an Integer IS-A a Double). But that's completely wrong! The compiler sees an int and thanks to autoboxing it can be converted into an Integer. And an Integer IS-NOT-A Double, so although you can cast an int to a double, you can not cast an Integer to a Double (you'll get a compiler error if you try do so).
There are many alternatives to change this code snippet and make it compile without errors. Can you find at least 3 different alternatives (without using methods from the Double class)?
Felipe Kunzler wrote:Please correct me if I'm not correct.
You are spot-on! Just one little question: is there a specific reason why you didn't include the Character wrapper class in the above overview as a char is also considered to be a numeric type[code=java]char c = 65;
OCAJP 7
Dana Ucaed wrote:I met in many posts the expression: You are spot-on!.
I search on the dictionary but I don't understand what are the meaning of expression.
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|