Win a copy of Head First Android this week in the Android forum!

Jj Roberts

Bartender
+ Follow
since Oct 25, 2019
Jj likes ...
Firefox Browser MySQL Database Java Ubuntu
I dabbled in writing C code for micro controllers for a bit. Around June 2019 I was introduced to Java. I loved it from the start. I am now an Oracle Certified Java SE 11 Professional.
Cows and Likes
Cows
Total received
44
In last 30 days
0
Total given
12
Likes
Total received
133
Received in last 30 days
3
Total given
100
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt Green check

Recent posts by Jj Roberts

Part 2:
14 hours ago
My solution to part one; not great but it worked.
14 hours ago
My solution to part two (my original didn't use the new switch syntax, though. It is so much neater!):

I don't know how to do fancy stuff, so I just did a simple Submarine class with methods to update its values in the specified way. I hardly had to change it for the second part. I work in Ruby for my day job, so I'm taking this as a opportunity to remember my Java.
I'm looking forward to it, though I don't expect to get far
Congrats to all of you
1 month ago
Congratulations all!
2 months ago
Hi there, welcome to the Ranch!

This thread is 17 years old, would you like to create a new topic and give more details about what you are trying to do?
2 months ago
Try / catch is used to handle cases where an exception might be thrown but you don't want it to kill your program. It's used a lot in file IO, because most operations can throw exceptions (lots of things can go wrong with reading from or writing to a file). It will help you a lot to go through the Exceptions trail of the Java Tutorials.

Try with resources allows to open a resource (like a FileReader) in a try, and will automatically close it when it exits the try, regardless of whether an exception was thrown or not. It is covered in the Java Tutorials trail above, in the The try-with-resources Statement part.
2 months ago
Congratulations all three of you!
3 months ago
Cheat sheet:

• Rule #1: Never, ever, use null for an Optional variable or return value.
• Rule #2: Never use Optional.get() unless you can prove that the Optional is present.
• Rule #3: Prefer alternatives to Optional.isPresent() and Optional.get().
• Rule #4: It's generally a bad idea to create an Optional for the specific purpose of chaining methods from it to get a value.
• Rule #5: If an Optional chain has a nested Optional chain, or has an intermediate result of Optional<Optional<T>>, it's probably too complex.
• Rule #6: Avoid using Optional in fields, method parameters, and collections.
• Rule #7: Avoid using identity-sensitive operations on Optionals.
3 months ago
The Java Tutorials have a good explanation of this, you can read up on wildcards here.

To zoom in on just one section, it gives an example like yours and explains what the behaviour is:

The Java Tutorials wrote:Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error

Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

On the other hand, given a List<?>, we can call get() and make use of the result. The result type is an unknown type, but we always know that it is an object. It is therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected.

1. Yes, it is applied to each element of the stream

2. No, I think you're thinking of a reduction operation
4 months ago
Passing a method reference to System.out.println() isn't going to work. A method reference is just a compact way to write a lambda. String::toUpperCase is the same as (String s) -> s.toUpperCase(); in this case. Your code is not calling toUpperCase on anything.

In fact, this will just attempt to print the value of the method reference itself. Trying it gives me the following: It tells us that there is no println method which takes a functional interface as an argument.
4 months ago