Java Intersection Type - You MUST know this

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.พ. 2025

ความคิดเห็น • 5

  • @viraj_shetty
    @viraj_shetty  11 หลายเดือนก่อน

    Hi All - Use link below for my popular Java courses.
    Virtual Threads - www.mudraservices.com/udemycoupon.html?course=vthread
    Java Generics - www.mudraservices.com/udemycoupon.html?course=jgen
    For more of my courses, check out - www.mudraservices.com

  • @amajor5460
    @amajor5460 11 หลายเดือนก่อน +1

    Useful info, thanks for sharing.
    I generally prefer to avoid using the var keyword. I'm a bit old school and think explicitly declaring the types is a good practice so it is totally clear.
    Here's how I would handle the scenario you mentioned at the end.
    interface A extends X, Y {}
    interface B extends A {
    void b();
    }
    interface C extends A {
    void c();
    }
    A result = switch(city) {
    case "New York" -> new B() {};
    default -> new C() {};
    }

    • @viraj_shetty
      @viraj_shetty  11 หลายเดือนก่อน +2

      Thanks ! You bring up some good points. The place where I usually use a var is when it improves readability without sacrificing clarity.
      For example, below is fine. It reduces clutter.
      var city = "New York"
      var map = new HashMap
      But avoid, something like
      var city = methodCall();
      For the last example - I created that example to show how JVM "infers" the type if we use var and how it generates the Intersection Type. I totally agree with your approach, if we have full control of the design.

  • @nikiforovsansanich
    @nikiforovsansanich 11 หลายเดือนก่อน +2

    Thank you, it is useful information. I didn't know about this

    • @viraj_shetty
      @viraj_shetty  11 หลายเดือนก่อน

      I am glad you like it !