Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!

Aleksandra Pestova

Ranch Hand
+ Follow
since Jun 23, 2016
Aleksandra likes ...
Firefox Browser Notepad Java
Zurich, Switzerland
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Aleksandra Pestova

Wow that's an awesome analogy! Thanks a lot!  
Now it's perfectly clear.
Thank you for a great explanation, Roel and Ganesh!  

the String literal "Hello" is interpreted as a String reference variable referring to a String object with value "Hello"

"Hello" == s   means It checks reference of "Hello" present in pool

That's what I didn't know. I understood that in a String literal pool references to the literals are stored and then got totally confused HOW Java was going to find out that I meant a literal and not an object.
I know very little about what's going on internally, so it makes harder to understand things very often.

With a temporary String reference variable it's much easier.

Thanks a lot for so many links! I'll definitely go through them.

Why do you post code snippets as screenshots and not the plain code snippets (and you should of course UseCodeTags )? If I want to compile (and execute) your code snippets, I know have to type them myself. So I'll be losing precious time which I could otherwise be spending on typing a nice reply... And another reason: attached images are not searchable using the search engine, plain text or a code snippet definitely is.  

Sorry I haven't thought about that.    It was faster to make a screenshot, next time - nice code snippet using code tags.  
In the study guide on page 153 there's a review question as on the picture.
Correct answers: A,C,D.

I've read this great article about String literal pool, as well as several similar threads on coderanch about it.

However, lines 8 and 9 still remained unclear to me.

Why is it possible to write "Hello" == s ? It confuses me that "Hello" is an object, but here references are being compared.
"Hello".equals(s) in contrast is perfectly clear to me.

According to the article I mentioned above, "Hello" is referenced from a String literal pool. How is this reference stored (in a variable...) ?

Thank you very much in advance,


Thank you all very much!

It's a big relief, that in the exam there is a number of correct answers given.
Option D slightly confused me.

I haven't chosen it, because to me it sounded like: for statements always contain an increment clause.
However, it can also be, for instance, a decrement statement or no update statement at all.

How is it with such type of questions at the exam? Is it asked more clearly? Or have I just misunderstood it because of my English?

Thanks a lot Ganesh! I didn't expect that you could just leave empty case statements.

Answer to your quiz questions: 11.
The first matching case is 5, then x becomes x=7. Since neither this case, nor following cases have a break statement, then default and case 2 are executed and x becomes 11.  
Lines 5 and 6 in the attached image seem weird to me.
I thought it would give a compiler error, but the correct answer is B: Congratulations.

I was thinking about a nested switch as well, but then default comes in a weird position.

Could someone please give me a hint what's going on here?
Thank you very much Roel and Ganesh for your discussion!

In my opinion this distinction is very confusing, so seems it's better just to memorize it for the exam.
Forgot to mention about the above example - it throws a NumberFormatException and the book says that it's thrown by programmer.
In the study guide Chapter 6 is said that:
1. ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException are thrown by the JVM and
2. IllegalArgumentException, NumberFormatException are thrown by programmer.

I don't quite understand this distinction.

I wrote following example:

I think in this case the JVM throws an exception and not a programmer.

Could someone explain it to me please or provide a good link explaining this distinction?

Thank you very much in advance,
There's a following example in the study guide:

Because Java allows only one public class per fi le, we can create two fi les,
and, in which the Lion class extends the Animal class. Assuming they are in the
same package, an import statement is not required in to access the Animal class.
Here are the contents of

And here are the contents of

Why can Lion use method getAge() directly?
Is it because Lion inherited all the methods from Animal, so getAge() now belongs to the "Lion" as well?
Thanks for providing the link! Haven't paid attention to the default packages.  
While going through review quetions I found two possible mistakes:

1. Question 19 (page 226):

Which of these classes compile and use a default constructor? (Choose all that apply)
G. public class Bird { void Bird() { }

I guess "}" is missing. The answer should be:  public class Bird { void Bird() { } } .

2. Question 22 (page 227):

What is the result of the following?


Import is missing.
Since both code snippets start with the line number 1, class OrderDrive doesn't know anything about class Order .
E is mentioned as the correct answer, but due to the missing import the correct answer should be G.