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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the ternary operator:
I think you have the conditions backwards – if it’s null, you want the value to be “N/A”.
What if city is null? Your code *hits the bed in that case. I’d add another check: