Interesting change in calling methods in Java 17

We have this code in our Java 11 practice exam book. What do you think it prints?

public class Hippo {
    private static void hippo(short num1, short num2) {
        System.out.println("shorts");
    }
    private static void hippo(int... nums) {
        System.out.println("varargs");
    }
    private void hippo(long num1, long num2) {
        System.out.println("longs");
    }
    private void hippo(int num1, int num2) {
        System.out.println("nums");
    }
    public static void main(String... args) {
        hippo(1, 5);
    } }

Reasoning it through, we know, it doesn’t print longs or nums because the main() is static and therefore we can’t be calling an instance methods. The answer is varargs because we have ints. This seems nice and reasonable.

I then ran the same code in Java 17 and it doesn’t compile!

% java17 Hippo.java
Hippo.java:15: error: non-static method hippo(int,int) cannot be referenced from a static context
hippo(1, 5);
^
1 error
error: compilation failed

i do agree that hippo(int, int) can’t be called. I was surprised at the behavior where it no longer compiles. If I remove the two instance variables, Java still prints varargs like I was expecting.

Happy Book Birthday! OCP Java 17 Book Released!

Jeanne and I over the moon to announce the release of our new OCP Java 17 Developer Complete Study Guide! This book is the culmination of years of knowledge that we’ve accumulated writing study guides for Java. It is for anyone who wants to expand their knowledge of Java, or who wants to pass Oracle’s new Java SE 17 Developer 1Z0-829 exam and become a Java 17 Oracle Certified Professional.

Lambdas and streams? Covered! Concurrency, JDBC, and NIO.2? Covered! New Java 17 switch expressions, records, sealed classes, pattern matching? Definitely covered! Modules? Ok, we don’t use them much either, but they are covered for the exam! It’s great for those who are familiar with Java, but want to acquire a deeper understanding of the language. Learn features of the language that will help you write better code!

Our Java 17 book is a streamlined version of our previous books, including all of the new material and changes that you need to know for the exam. Previously, we wrote one book for each of the two exams. Starting with Java 17, there’s only one exam, allowing us to cover the material in a much more concise and straight-forward manner. Put simply, it weighs less than our previous Complete Study Guide!

Want to win a free copy? Our home away from home, CodeRanch, will be running a book promotion next week starting on May 10th!

[devnexus 2022] pattern matching for Java

Speaker: Neha Sardana

Twitter: @nehasardana09

Link to table of contents

———————

Records

<missed this part; I was late>

Sealed class

  • Want to control children
  • More declarative than access modifiers
  • Can make widely accessible interface without widely extensible
  • Sets stage for pattern matching.

Pattern Matching

  • Pattern matching created in 1960s
  • Helps with clean code, avoiding repetition/bugs

Instanceof

  • matches target object to type pattern
  • sets binding variable – special case of local variables, can be assigned, can shadow field declarations
  • flow scoping – places in program where variable definitely assigned

Switch expressions

  • Limitations on switch/case are accidents of history.
  • Java 17 preview allows pattern matching in case
  • Java 19 – write ”when” instead of && in case
  • Can assign result if all cases covered. (enums and sealed classes can be listed exhaustively

Future

  • Record patterns (deconstrution patterns). ex if (r instanceof Rectangle (Point ul, Point lr))
  • Can also deconstruct arrays

My take

I thought this topic woud be fully review, but I still learned something (“when”). It was great to see Neha’s first public in person presentation! Good job!