Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (10.2k points)

I know there is a way for writing a Java if statement in short form.

if (city.getName() != null) {

    name = city.getName();

} else {

    name="N/A";

}

Does anyone know how to write the short form for the above 5 lines into one line?

1 Answer

0 votes
by (46k points)

To avoid calling .getName() twice I would use

name = city.getName();

if (name == null) name = "N/A";

Related questions

0 votes
2 answers
0 votes
1 answer

Browse Categories

...