• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

page 155 and 163 OCA 8 Guide: Review Questions 6 and 32/33

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

***** 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!!!)
Code:


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?

Thanks
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which guide? Please give the authors' names.

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.
 
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding your first 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.

You could also rewrite it as such:



 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Brecht Geeraerts
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!!".
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks!
Pedro
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic