DEV Community

Easy Tuts
Easy Tuts

Posted on

1

🌟 Did you know ? 🌟

While Java is known for int, String, and other explicit data types... πŸ’‘ You can actually use var in Java! 🀯

Yes, Java introduced var in Java 10, and it's a game-changer for developers who love clean, readable code! πŸš€

I know, we all think of Java as the language where you have to spell everything out... String this, ArrayList that. But since Java 10, we've had this awesome little shortcut.
Let me show you what I mean:

// without using var
ArrayList<String> names = new ArrayList<>();
LinkedHashMap<Integer, String> userMap = new LinkedHashMap<>();

// with var
var names = new ArrayList<String>();
var userMap = new LinkedHashMap<Integer, String>();
Enter fullscreen mode Exit fullscreen mode

Now before you ask - nope, this isn't making Java dynamically typed like JavaScript. It's just syntactic sugar. Java is still doing all its strict type checking behind the scenes. You're just saving yourself some typing and making your code a bit cleaner to read.

A few quick things to keep in mind:

  • You can only use var for local variables
  • You have to initialize the variable right away
  • It won't work with null values (the compiler needs to know the type!)

What do you all think? Are you already using var in your Java code? Let me know in the comments!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay