***** Exercise 6 *****
(PS: The book name is OCA Oracle Certified Associate JAVA SE 8 Programmer I Study Guide from Jeanne Boyarsky and Scott Selikoff)
(PS: The exact question of the exercise below 6 below is: "What is the result of the following code? //Answer is: roar roar!!!)
I'd expect this code not to work: the variables roar1 and roar2 are defined inside the main() block of code, hence they have local scope.
I know that "smaller contained blocks can reference variables defined in the larges scoped blocks"(page 32), but that's not the case as roar() and main() are at the same level.
Question: How can roar() sucessfully reference to them?
***** Exercises 32/33 *****
On exercises 32/33 we have "inverse" ways of printing the desired formatted date:
I will not print the enire code, as the relevant part for my question is the order in which the exercises use .format, the LocalDateTime f variable, and the DateTimeFormatter d variable.
Exercise 32 codes System.out.print(d.format(f));
Exercise 33 codes System.out.print(f.format(d));
I'd expect the way exercise 33 prints it to be incorrect, but it isnt. I then also decided to print ex 33 they way I expected it to be and it also works. It seems more intuitive we'd apply .format(f) to a date, then the other way around.
Question: Why is interchangeable?
Pedro Esgueira wrote:. . . the variables roar1 and roar2 are defined inside the main() block of code . . .
They are defined anew as method parameters for the roar() method. Watch what is passed, and you will see that the parameters in roar() receive object references passed from main().
Did you correctly predict what you would see printed? Was it, “roar roar!!!”?
. . . I will not print the enire code . . .
I probably don't have that book, so I can't answer without seeing the whole question.
The Strings roar1 and roar2 are indeed declared and initialized within the scope of the main() method.
The parameters of the roar() method, which have the same name, are not the same as the ones within the main() method.
Question 2: It's not that they are "interchangeable", it's just that LocalDateTime and DateTimeFormatter both have a format() method. There's no overriding Java technicality going on.
All things are lawful, but not all things are profitable.
posted 1 month ago
Hi all, and thanks for the feedbak
I've edit my initial post where it says PS to provide the full name and authors of the book, and to give the specific question/answer for exercise 6.
**** ex 6:
hi Campbell: I've noticed the parameters in roar() receive object references passed from main. My question is: those references were defined in main, thus local scope: so how can roar() read them?
hi Brecht: you mention the parameters are not the same: but in that case, if they are not the same and they have not been defined elsewhere, how can the answer to this exercise be "roar roar!!!"?
**** ex 32/33 hi Knute: thanks! ok, so it's simply that .format() is both a method to LocalDateTime and to DateTimeFormatter. Gotchya
Pedro Esgueira wrote:hi Brecht: you mention the parameters are not the same: but in that case, if they are not the same and they have not been defined elsewhere, how can the answer to this exercise be "roar roar!!!"?
Java is pass-by-value, that means that (for objects) a copy of the reference is passed to the method.
(For primitive values, a copy of the value is passed)
The method roar() accepts two parameters: one String object and one StringBuilder object. The method performs the following actions on its arguments:
- it concatenates "!!!" to the String object
- it appends "!!!" to the StringBuilder object
Note that a String is immutable, while a Stringbuilder is not. This means that the concatenation operation returns a new String object ("roar!!!") but it is not saved into a variable. The initial String object ("roar") remains unchanged, which is why this outputs as "roar". On the contrary, a Stringbuilder is mutable and the append operation modifies the original SB object. Therefore roar2 will contain the change and outputs "roar!!!".
Beginning programmer - self-taught
Oracle Certified Associate Java SE 8
posted 1 month ago
Pedro Esgueira wrote:. . . My question is: those references were defined in main, . . .
No, they are defined in the roar() method. The fact that they have the same name as in main() doesn't mean they are the same variables, as BG has shown.
. . . how can the answer to this exercise be "roar roar!!!"? . . .
I think BG has explained that nicely already.
posted 1 month ago
Hi Brecht, thanks, I know see what you mean with the parameter variables not being the same. For the rest I will digest it a bit more, and eventualy come back to you.