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
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() {}; }
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.
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
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() {};
}
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.
Thank you, it is useful information. I didn't know about this
I am glad you like it !