Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...