• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCA Java SE 8 - a few basic queries

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

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! So here goes:

The first question I have is regarding garbage collection. I'm fairly certain I understand how it works, I guess I'm looking for confirmation whether my reasoning is correct or not. This is question 20 from the book.

What is true about the following code? (Choose all that apply)


The possible answers are:

A. finalize() is guaranteed to be called.
B. finalize() might or might not be called.
C. finalize() is guaranteed not to be called.
D. Garbage collection is guaranteed to run.
E. Garbage collection might or might not run.
F. Garbage collection is guaranteed not to run.
G. The code does not compile.

The answers are B and E.

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.


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 ^


Finally, I came across this example when studying switch statements in chapter 2.



The book states that a case statement must be a literal, enum constant or final constant. The first case statement is OK as it uses a String literal. My question is regarding the second case statement, "this does not compile because middleName is not a final variable, despite having a known value at this particular line of execution." Would I be correct in assuming that although a String has been assigned to a variable, because that variable is not final it will not compile? 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?


Thanks in advance, I hope this post isn't too long-winded!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
Welcome to CodeRanch! In the future, I recommend creating separate posts for unrelated questions. In this case, I would have created multiple threads. (It's easy to miss a question when there is too much in a post)

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.


This is correct reasoning. That question (from ch 1) is testing whether you know whether finalize and GC are guaranteed to be called. And you are correct that they are 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 ^


I think this is a wording thing. && and & are both logical operators. Similarly || and | along with ^ are logical operators. The difference is that && and || are short circuit. This means that the if you write a && b, that b will not get evaluated if a is true. By contrast with a & b, b will get evaluated regardless of whether a is true of false.

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?


Yes.

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?


This part is tricky. It has to be a "compile time" constant to be used in a case statement. middleName is a compile time constant because the string is in the code. lastName is not a compile time constant because it is a parameter.
 
Shane Kelly
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick response Jeanne. It's good to get feedback to know you're on the right track.

I recommend creating separate posts for unrelated questions



I will keep this in mind when posting in future.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shane Kelly,

First of all, a warm welcome to CodeRanch!

Most of your questions/doubts are already answered by Jeanne's excellent post. Still I like to make a few additions.

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.


Exactly! This also means you can only know how many objects are eligible for GC at any given line in a code snippet. But you can not know how many objects will be actually GCed at any given line in a code snippet. And on the exam you could encounter a question like this one, so always read carefully

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?


&& and || are known as the conditional operators; &, | and ^ are known as the bitwise operators. The conditional operators (which are short-circuit) can only be used with booleans, so you'll see them only in conditions. The bitwise operators are used for bit operations mostly on integral types (byte, short,...) and as their name suggests they do their magic on the actual bits of the value. You could use & and | on 2 booleans as well. Bitwise operators are less commonly used and their bit-twiddling capabilities are not on the exam. For the exam you only need to know:
  • bitwise operators exist
  • how they work on booleans
  • the difference in behavior with the short-circuit operators
  • There's a huge difference between these 2 statements:

    Shane Kelly wrote:Also, with logical operators, could someone explain the difference between inclusive and exclusive OR i.e. | and ^


    Nothing better than a little code snippet to demonstrate the difference:Output:
    x y & | ^
    0 0 0 0 0
    0 1 0 1 1
    1 0 0 1 1
    1 1 1 1 0


    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?


    If you want to use a String variable in a case statement, it has to be a compile-time constant. So the variable must be marked final (otherwise it's not a constant) and its value must be known to the compiler on compilation time (not run time). Let's add a little code snippet and see if you can pick the invalid case statements. Which case statements will produce a compiler error (and why)?
    And as a final note: this requirement of using nothing but compile-time constants in case statements applies not only to Strings, but to other data types (byte, short, char, int and enums) you can use in a switch statement as well. So let's add another code snippet and ask the same question: Which case statements will produce a compiler error (and why)?

    Hope it helps!
    Kind regards,
    Roel
     
    Shane Kelly
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Roel,

    Thanks for your answers, and for the link to another GC question; I found it interesting. I will give your exercises a go, hopefully I get them right!



    "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.





    I found this one much tougher, anyway here's what I came up with:

    "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.

    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.

    Thanks
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.


    Spot-on! Even the one you are not 100% sure about, is completely correct. Well done!

    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.


    Again a perfect score! Even spotted the (tricky) capital B

    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.


    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!

    Hope it helps!
    Kind regards,
    Roel
     
    Shane Kelly
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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!



    Excellent, thanks a lot Roel, that is a great explanation. I think I've got it now. Thanks for your help!
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Glad to hear I could help!

    Nice to know: instead of making a "thank you" post, you could also the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic