Pregunta

      servicesDto.setPhotospath(values.get("photospath") == null ? null
              :values.get("photospath").toString());

What is represented here and why is null used in this manner: '== null ? null'

Note : I am Fresher please give me a such reference and Explain me Bro/Sis/Friends.

Thanking you...!!!

¿Fue útil?

Solución 3

This is called short form (ternary) if else your code above is equivalent to the following:

if(values.get("photospath") == null){
       servicesDto.setPhotospath(null);
}else{
       servicesDto.setPhotospath(  values.get("photospath").toString())  );
}

Otros consejos

in if else notation, this is what it means:

if(values.get("photospath") == null ) {
 servicesDto.setPhotospath(null);
}else {
 servicesDto.setPhotospath(values.get("photospath").toString());
}

It is a ternary operator, and that ternary is equivalent to

if (values.get("photospath") == null) {
  servicesDto.setPhotospath(null);
} else {
  servicesDto.setPhotospath(values.get("photospath").toString());
}

The ternary operator is using one operator to take three operands. It is a conditional operator that provides a shorter syntax for the if...else... statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned:

boolean expression ? value1 : value2
For example

boolean isTernary = true; String easyToRead = ""; if (isTernary == true) { easyToRead = "No"; } else { easyToRead = "Yes"; }
can be written by using the ternary operator:

boolean isTernary = true; String easyToRead = (isTernary == true)?"Yes":"No";

The if...else... statement which is written in full is easier to read, but sometimes the ternary operator can be a handy syntax shortcut.

You're looking at a ternary operator:

Properly formatted, one looks like this:

 foo.isSomething() ? doThis() : doThat();  

and translates to: "If the expression before the ? evaluates to true, execute the statement between the ? and the :, otherwise execute the statement after the :."

In other words, you can think of this as shorthand for:

 if (foo.isSomething()) {
     doThis();
 } else {
     doThat(); 
 }

Note that this is merely a shorthand - there's nothing you can do with a ternary operator that you can't do with if / else statements. That said, you should get familiar with them if you plan to learn programming by exploring professional code (which, incidentally, I recommend strongly), as you'll see it used a lot - not just in Java but in a multitude of languages.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top