Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Java var

2 min readSep 19, 2024

--

For when you’re too lazy to write String but still want to pretend you’re working hard!

Press enter or click to view image in full size

In Java, the var (Local-Variable Type Inference) is like having Java figure out the type of a variable for you, so you don’t have to write it yourself. It’s a shortcut for declaring variables without explicitly specifying their type.

Imagine going to a pizza place, and instead of telling the cashier what type of pizza you want (like cheese, pepperoni, etc.), you just say, “Give me whatever fits best,” and the cashier figures out the pizza type based on what you meant. That’s var!

How It Works:

  • Before var: You had to be specific about what type of value a variable would hold, like this:
String message = "Hello!";
int number = 10;
  • With var: Java will guess the type of the variable based on the value you assign to it:
var message = "Hello!";  // Java knows it's a String
var number = 10; // Java knows it's an int

The key thing is, Java is still strongly typed. It knows exactly what type the variable is; you just don’t need to explicitly write it. It’s not dynamic typing like in JavaScript — it just saves you from typing long type names.

Example of var:

--

--

M Business Solutions
M Business Solutions

Written by M Business Solutions

M Business Solutions delivers tech insights, industry practices, and the latest updates, empowering businesses with expert blogs and innovative solutions.

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development