Shane Kelly wrote:This is my first post, and will probably be the first of many! I am studying for the OCA Java SE 8 Programmer I - Exam 1Z0-808, and I'm using the study guide to do so. I have just started recently and have a few queries regarding what I have learned so far. I thought it would be better to ask them all in one post rather than creating separate posts!
Shane Kelly wrote:
My understanding of this is that at line 8 the object is eligible for garbage collection. At line 9, although System.gc() would seem to be invoked, this is just a suggestion to Java to run garbage collection although Java may choose not to do so. So by this reasoning options B and E are correct because there is no way to tell if garbage collection will run or not.
Shane Kelly wrote:
When I was studying Java I used conditional operators in my code when needed. I wasn't aware of logical operators, & | ^, until I read about them in this book. From my reading I understand that logical operators will evaluate both sides of the expression, whereas conditional operators may not evaluate the right hand side if the result can be garnered from the left hand side. When would you need to write a logical operator in place of a conditional one? Also, with logical operators, could someone explain the difference between inclusive and exclusive OR i.e. | and ^
Shane Kelly wrote:
[code=java]So anytime you assign a String to a variable, that variable has to be final to be able to use it in a switch statement?
Shane Kelly wrote:
As for case lastName, it does not compile because although it is final it can't be constant as that parameter has been passed to this method. Is my understanding of this valid?
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I recommend creating separate posts for unrelated questions
Shane Kelly wrote:My understanding of this is that at line 8 the object is eligible for garbage collection. At line 9, although System.gc() would seem to be invoked, this is just a suggestion to Java to run garbage collection although Java may choose not to do so. So by this reasoning options B and E are correct because there is no way to tell if garbage collection will run or not.
Shane Kelly wrote:When I was studying Java I used conditional operators in my code when needed. I wasn't aware of logical operators, & | ^, until I read about them in this book. From my reading I understand that logical operators will evaluate both sides of the expression, whereas conditional operators may not evaluate the right hand side if the result can be garnered from the left hand side. When would you need to write a logical operator in place of a conditional one?
Shane Kelly wrote:Also, with logical operators, could someone explain the difference between inclusive and exclusive OR i.e. | and ^
Shane Kelly wrote:So anytime you assign a String to a variable, that variable has to be final to be able to use it in a switch statement?
As for case lastName, it does not compile because although it is final it can't be constant as that parameter has been passed to this method. Is my understanding of this valid?
Shane Kelly wrote:"Which case statements will produce a compiler error (and why)?"
case2 won't compile because the variable isn't final.
case3 - I'm not 100% sure about this one, but I think it won't compile because the case3 variable isn't a constant as it's invoking a method.
case4 won't compile because the animal variable isn't final.
Shane Kelly wrote:"Which case statements will produce a compiler error (and why)?"
case B3 won't compile because it's not final.
case2 won't compile because it's not final either.
case5 won't compile because Byte is an object, so it can't be a compile time constant.
Shane Kelly wrote:case3 and case4 are of different data types than the switch variable. However, I'm not 100% sure if switch statements employ numeric promotion, so I'm guessing that if I use this principle both of these will be OK. As I said I'm not really sure about this so if I'm wrong could you please explain how a switch statement will treat these cases.
Roel De Nijs wrote:
Using different integral types is not a problem when the switch variable is also an integral type. But there is 1 very big limitation: the value of the compile-time constant must be in range of the switch variable! Otherwise you'll get a compiler error, even if it's a compile-time constant. Let's apply this rule to the case statements you were unsure about: the switch variable is of type byte, so the range is -128 to 127 (inclusive). If we look at case3, the value is 80. That's in range, so it compiles. But the value of case4 is 130; that's out of range and so this case won't compile!
Men call me Jim. Women look past me to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|