Borislav Mirchev

Greenhorn
+ Follow
since May 13, 2021
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Borislav Mirchev

It is very interesting and is probably not because of the brackets.

You can execute the following procedure:

1. Copy the following code:



in the text area of the following site:

https://www.jdoodle.com/online-java-compiler/

2. There is a dropdown under: "Execute Mode, Version, Inputs & Arguments" in this dropdown select: "JDK 11.0.4"

3. Click the "Execute" button.

It is displayed:

/Q14.java:7: error: ')' expected
} catch (Exception e || RuntimeException e) {
                    ^
/Q14.java:7: error: illegal start of expression
} catch (Exception e || RuntimeException e) {
                     ^
/Q14.java:7: error: ';' expected
} catch (Exception e || RuntimeException e) {
                                        ^
/Q14.java:7: error: not a statement
} catch (Exception e || RuntimeException e) {
                                         ^
/Q14.java:7: error: ';' expected
} catch (Exception e || RuntimeException e) {
                                          ^
/Q14.java:10: error: 'finally' without 'try'
} finally {
 ^
/Q14.java:12: error: reached end of file while parsing
} }}
   ^
7 errors

When I repeated the same procedure by only changing the (the code in the test from the book)
to
(the fixed version)

The result is:

/Q14.java:4: error: incompatible types: try-with-resources not applicable to variable type
try (StringBuilder reader = new StringBuilder()) {
                   ^
   (StringBuilder cannot be converted to AutoCloseable)
/Q14.java:7: error: Alternatives in a multi-catch statement cannot be related by subclassing
} catch (Exception | RuntimeException e) {
                     ^
 Alternative RuntimeException is a subclass of alternative Exception
/Q14.java:9: error: cannot find symbol
throw new FileNotFoundException();
          ^
 symbol:   class FileNotFoundException
 location: class Q14
3 errors

As you can see the message:

/Q14.java:12: error: reached end of file while parsing
} }}

is not here by only removing the text "e |"
from the source code (nothing else is changed).

It makes me to think there is not an issue with the brackets.
The only issue is with an incorrect text in the book:
It should be changed to:



I'm not sure if I'm properly understanding you. Are squigglies curly brackets? "}"

If yes - on line 12 there are 3 closing curly brackets - one for finally, one for the main method and one for the class Q14. (Added the class in order to execute the code. In the test there is no class. I mean the code with  "} catch (Exception e || RuntimeException e) {")
Yes. It is not a drama here. The explanation is a little confusing.

When I ran the following code:



Using the online compiler below:

https://www.jdoodle.com/online-java-compiler/

(Java 11.04)

The exact text is:

/Q14.java:7: error: ')' expected
} catch (Exception e || RuntimeException e) {
                    ^
/Q14.java:7: error: illegal start of expression
} catch (Exception e || RuntimeException e) {
                     ^
/Q14.java:7: error: ';' expected
} catch (Exception e || RuntimeException e) {
                                        ^
/Q14.java:7: error: not a statement
} catch (Exception e || RuntimeException e) {
                                         ^
/Q14.java:7: error: ';' expected
} catch (Exception e || RuntimeException e) {
                                          ^
/Q14.java:10: error: 'finally' without 'try'
} finally {
 ^
/Q14.java:12: error: reached end of file while parsing
} }}
   ^
7 errors

Counted as 7 errors.

When I fix the multi-catch clause as below:



The same online compiler says:

/Q14.java:4: error: incompatible types: try-with-resources not applicable to variable type
try (StringBuilder reader = new StringBuilder()) {
                   ^
   (StringBuilder cannot be converted to AutoCloseable)
/Q14.java:7: error: Alternatives in a multi-catch statement cannot be related by subclassing
} catch (Exception | RuntimeException e) {
                     ^
 Alternative RuntimeException is a subclass of alternative Exception
/Q14.java:9: error: cannot find symbol
throw new FileNotFoundException();
          ^
 symbol:   class FileNotFoundException
 location: class Q14
3 errors

Counted as 3 errors.

And the explanations fit exactly as is supposed to be.

What you said from pedagogical perspective and the question's main idea is correct. The concepts here are very useful.
At the beginning of the book there is an assessment test.

In the question 14's text is written:

10: } catch (Exception e || RuntimeException e) {

It is an incorrect way to handle multiple exceptions.

The correct is:

10: } catch (Exception | RuntimeException e) {

This will makes the explanation below correct:

"Finally, line 10 does not compile as
RuntimeException is a subclass of Exception in the multi-catch
block, making it redundant."
TABLE 10.6 Sample stored procedures

In the read_e_names()'s description is written: Returns all rows in the names table that have a name beginning with an E
In fact it returns all the names, not only the ones, starting with E.

If we take look at:
https://www.selikoff.net/2019/12/22/ocp-11-book-bonus-creating-a-hsql-database-stored-procedure-in-java-11/

We see: String noParams = "CREATE PROCEDURE read_e_names() "
                   + "READS SQL DATA DYNAMIC RESULT SETS 1 "
                   + "BEGIN ATOMIC "
                   + "DECLARE result CURSOR WITH RETURN FOR SELECT * FROM names; "
                   + "OPEN result; "
                   + "END";

There is not a WHERE clause for the query.

The testing code I used to verify this behaviour is:



It prints:
Elsa
Zelda
Ester
Eddie
Zoe

Instead of:
Elsa
Ester
Eddie
Jeanne,
Excuse me for the verbose initial post and missing the appearance of "C instead of B" in the errata list and the topic dedicated to it!
I also mentioned two more things - "by usage" and "system dependent paths". (The last two paragraphs of the initial post.) What about them?



I think another source of difficulty is Java became "too big". It is 25+ years old. I suppose it is hard to be covered by a book with reasonable number of pages.  The situation is very interesting for the newcomers to the language. I think it is possible to be a reason some companies to switch to languages like Kotlin. After some period writing to such a language it becomes a challenge to return back to Java even having a good experience with it. I think our discussion is interesting, but went too far from the initial subject.
For me from a practical perspective it is important to know there are such options available. After some experiments, online searching and testing the right combination of options will be found.

In general I like programming. But when it is done for business under pressure, limitations and not good interaction with people the situation is possible to be changed.

The software industry is rapidly changing and writing/reading adequate books following its steps I suspect is not an easy task. Furthermore - it is human to err. I hope thinking on the details makes us better developers.
Hi Jesse:

Thank you for the attention!

Q22 is like the following:

Assuming the path referenced by m exists as a file, which statements about the following method are
correct? (Choose all that apply.)

A. If the path referenced by x does not exist, then it correctly copies the file.
B. If the path referenced by x does not exist, then a new file will be created.
C. If the path referenced x does not exist, then an exception will be thrown at runtime.
D. If the path referenced x exists, then an exception will be thrown at runtime.
E. The method contains a resource leak.
F. The method does not compile.

It is amazing how much time and efforts you put on exploring this subject.

I spent some time on reading the suggested thread.

On my opinion what best explains the APPEND's behaviour is:
// append to an existing file, fail if the file does not exist
   out = Files.newOutputStream(path, APPEND);

This is a possible reason to have C, E correct but not B, E as is written in the book.
In Chapter 9 (NIO.2) review question 22. According to the book is with correct answers B and E.

Decided to test the method provided with the following code:



Manually created the file C:\data\file19.txt with the following content:

"File 12344
rrrr
ssss"

The file: C:\data\file22.txt does not exists.

Executed the sequence of commands:

C:\Users\borko\IdeaProjects\2Chapter9\src\test\q22>set JAVA_HOME=C:\Program Files\Java\jdk-11.0.11

C:\Users\borko\IdeaProjects\2Chapter9\src\test\q22>set Path=%JAVA_HOME%\bin;%Path%

C:\Users\borko\IdeaProjects\2Chapter9\src\test\q22>java --version
java 11.0.11 2021-04-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)

C:\Users\borko\IdeaProjects\2Chapter9\src\test\q22>java Q22.java
Exception in thread "main" java.nio.file.NoSuchFileException: C:\data\file22.txt
       at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
       at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
       at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
       at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:235)
       at java.base/java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:478)
       at java.base/java.nio.file.Files.newOutputStream(Files.java:220)
       at java.base/java.nio.file.Files.newBufferedWriter(Files.java:2920)
       at java.base/java.nio.file.Files.newBufferedWriter(Files.java:2963)
       at test.q22.Q22.duplicateFile(Q22.java:10)
       at test.q22.Q22.main(Q22.java:20)

Based of this answer C is correct instead of answer B => the correct answers are C and E.

My English is poor. I have a question. Is "by" optional in the statements like "referenced by x"?
In the list of possible answers "by" is written in A and B, but is missing in C and D.

The paths in some of the questions are system dependent. If a reader uses Windows OS some of the explanations are affected.
For example when explaining the answer of  review question 1 we see: "Finally, if the
value on line 5 was assigned to path and printed on line 6, then option A would be correct."
In fact if we use Windows the output is possible to be: ..\..\ lion instead of ../../lion making option D would be correct instead of A. (because of the separators)



Jeanne,
Thank you for the cow and improving the book!
Math is one of my hobbies. Probably because of this my brain is burdened. I thought about two intersecting sets/definitions. Sometimes math is good for programming, but the opposite also happens.
Jeanne,
Thank you for answering this question! It was very useful to see your idea and some of the possible interpretations. I observed that (at least for me) it is very hard to forget the moments when I stuck on.

Let's verify my understanding with a practical example: if we have a collection x - containing the elements 1, 2, 3, 4 - if the elements are always in the order 1, 2, 3, 4 or 4, 3, 2, 1 - the collection is sorted and ordered.
In case we have a guaranteed/predictable order of the elements like 2, 1, 4, 3 - it is ordered, but not sorted.

Is that correct?

I use the book to get ready for some future job interviews and trying to understanding the material sometimes asking myself additional questions like:

What is a 'natural ordering'? (I suppose - it is the default way the JVM compares the collection's items. / lexicographic for Strings and based on memory addresses for other objects /)  

How can we guess if a collection is ordered by looking at its base classes and interfaces? (I suppose - a reading of the Javadoc is required.)

If you have time can give a light on these questions. It is possible to miss "the right questions" while trying to understand the material, so you can add more questions if needed.

Thank you for the response. What you wrote is actually correct.

I suppose there is a misunderstanding and the reason is in the code there are two lines containing Future.

Before line n1 we have:
It should be:
( '>' is used instead of '<' after the word 'Future' )

When I executed on original (as is in the book) version: java PrintCounter.java

I see:

PrintCounter.java:13: error: '(' or '[' expected
           var r = new ArrayList<Future>?>>();

There is not an available answer for this message.

Thank you very much for spending time on this post and improving the quality of the book!

Please correct my family in the errata list! It is Mirchev, not Merchev. I suppose Bulgarian names are not meaningful to many non Bulgarians.

I found more potential mistakes (nothing critical). Some are written by me as posts on the forum and when I have time - can add more.

In Chapter 7 (Concurrency) Review question 20 the source code is:



Should be:



The reasons for these changes are:
1. <Future>?> cannot be compiled.
2. The expressions used in streams are supposed to be effectively final. (Can test it by replacing .execute with .submit as described in the answers)