Crystal Kirscht

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

Recent posts by Crystal Kirscht

No. This is the exact text in the book:

High-level I/O streams can also take other high-level I/O streams as input. For example, although the following code might seem a little odd at first, the style of wrapping an VO stream is quite common in practice:

try (var ois = new ObjectInputStream(
new BufferedInputStream
new FileInputStream ("zoo-data.txt")))) {
System. out. print(ois. readObject ()) ;

In this example, the low-level File InputStream interacts directly with the file, which is wrapped by a high-level BufferedInputStream to improve performance. Finally, the entire object is wrapped by another high-level ObjectInputStream, which allows us to interpret the data as a Java object.
I don't have any next steps. I was just trying to run this exact example in the book and was wondering why it was giving an exception.
I am trying to run this code on page 815 of Chapter 14 :



But when I do I get an exception saying java.io.StreamCorruptedException: invalid stream header: 54686973

zoo-data.txt is just a text file with This is zoo-data.txt inside it in the same directory.
Any idea what is causing this exception?
There’s a 21 test now? I’ve been studying for the Java 17 certification for almost a year and I still feel like I’m a ways away from being ready to take it.
In Table 12.15 in Chapter 12 on page 700 under Option I believe the -p should be followed by <path> which is missing.

Also, in Table 12.14 for jdeps options, does it only use --module-path or does it use the -p abbreviation as well?
In Table 12.8 Java modules prefixed with jdk, the first entry says jdk.accessiblity - it should be jdk.accessibility

Jeanne Boyarsky wrote:

Crystal Kirscht wrote:I discovered the same thing. Only it only compiles in Eclipse and in VSCode (which both use Java 17). However when I run Java from the command line on the same code it does not compile. I'm not sure what is different


Can you type


My guess is your command line is using Java 21 and triggering the change in behavior Paul mentioned.



It is actually on the command line that it is NOT compiling.

D:\MyWorkspace\HelloWorld\src\chapter3>javac PatternMatching.java
PatternMatching.java:38: error: expression type Integer is a subtype of pattern type Integer
               if(value instanceof Integer data) {
                        ^
1 error

D:\MyWorkspace\HelloWorld\src\chapter3>java --version
java 17.0.4.1 2022-08-18 LTS
Java(TM) SE Runtime Environment (build 17.0.4.1+1-LTS-2)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.4.1+1-LTS-2, mixed mode, sharing)

It is in Eclipse and VSCode where it IS compiling. But I know those are using Java 17 as I have not installed any other versions and I have checked the settings in Eclipse (I'm not sure where they are in VSCode). I've attached screenshots of the code and jre settings in eclipse
I discovered the same thing. Only it only compiles in Eclipse and in VSCode (which both use Java 17). However when I run Java from the command line on the same code it does not compile. I'm not sure what is different
I think I understand it now but let me confirm:

For question 8 calling printType(11):

31: void printType(Object o) {
32:    if(o instanceof Integer bat) {
33:       System.out.print("int");
// We exit the if statement here since 11 is an Integer

34:    } else if(o instanceof Integer bat && bat ˂ 10) { Since o was an integer bat is no longer in scope and can be redefined?
35:       System.out.print("small int");
36:    } else if(o instanceof Long bat || bat ˂= 20) {
37:       System.out.print("long");
38:    } default {
39:       System.out.print("unknown");
40:    }
41: }

For questions 28 calling getFish("goldie")?

40: void getFish(Object fish) {
41:    if (!(fish instanceof String guppy))
42:       System.out.print("Eat!");
fish is an instance of String so we don't exit the if statement and guppy is still in scope

43:    else if (!(fish instanceof String guppy)) {  Since guppy is still in scope we can't redefine it
44:       throw new RuntimeException();
45:    }
46:    System.out.print("Swim!");
47: }


Yes, it was part of the question.  The correct answer was " The code contains two lines that do not compile." Here is the whole explanation:

The first two pattern-matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope. Line 36 does not compile, though. Due to flow scoping, if s is not a Long, then bat is not in scope in the expression bat ˂= 20. Line 38 also does not compile as default cannot be used as part of an if/else statement. For these two reasons, option G is correct.

I don't know where 's' came from, I'm assuming they mean 'o'
Chapter 3 has 2 review questions regarding Pattern Matching and Flow Scoping that have conflicting answers. Please explain what is different between them.

Question 8 has the following code:
What is the output of calling printType(11)?
31: void printType(Object o) {
32:    if(o instanceof Integer bat) {
33:       System.out.print("int");
34:    } else if(o instanceof Integer bat && bat ˂ 10) {
35:       System.out.print("small int");
36:    } else if(o instanceof Long bat || bat ˂= 20) {
37:       System.out.print("long");
38:    } default {
39:       System.out.print("unknown");
40:    }
41: }

In the answer explanation, it states that "The first two pattern-matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope."

However, in Question 28, the code is similar:
What is the output of calling getFish("goldie")?
40: void getFish(Object fish) {
41:    if (!(fish instanceof String guppy))
42:       System.out.print("Eat!");
43:    else if (!(fish instanceof String guppy)) {
44:       throw new RuntimeException();
45:    }
46:    System.out.print("Swim!");
47: }

This time the answer explanation states "Based on flow scoping, guppy is in scope after lines 41-42 if the type is not a String. In this case, line 43 declares a variable guppy that is a duplicate of the previously defined local variable defined on line 41. For this reason, the code does not compile..."

Why is the duplicate variable declaration allowed in question 8 but not in question 28?

Thanks in advance
On page 562 in Chapter 10 of the Java 17 study guide are the two following methods:

and


Are these Java methods? If so what class are they in? I was unable to find any reference to them anywhere.