Aplicando el operador ternario con Java ['esp','eng']

Como muchos desarrolladores sabrán este operador es igual que aplicar “if - else” en otros lenguajes de programación y en el mismo Java, pero que ventajas puede tener este operador con respecto a un simple condicional; en verdad la diferencia puede ser poca o mucha y depende del contexto de la situación.
Con este tipo de operador la libertad es amplia para usarlo, pero el único consejo que doy es evitar anidarlo porque la lectura y sintaxis se puede volver confusa para el trabajador.

As many developers will know, this operator is the same as applying “if - else” in other programming languages and in Java itself, but what advantages can this operator have with respect to a simple conditional; In truth, the difference can be little or much and depends on the context of the situation. With this type of operator there is ample freedom to use it, but the only advice I give is to avoid nesting it because the reading and syntax can become confusing for the worker.

terna2.jpg

terna3.jpg
En este ejemplo bastante sencillo que utilizo para reforzar mis conocimientos te muestro con código de manera directa como usarlo, de manera resumida lo que ocurre es a través del método escáner recibo un valor, que debe ser un numero seguido de un punto y otro número, para evaluar si cumple con la condición u no.
La diferencia es que esta vez probe el código creando un variable del tipo “Double”, pero solo declarada sin contenido ninguno desde un inicio.

In this fairly simple example that I use to reinforce my knowledge, I show you with code directly how to use it, in summary what happens is through the scanner method I receive a value, which must be a number followed by a period and another number, to evaluate whether it meets the condition or not. The difference is that this time I tested the code by creating a variable of type “Double”, but only declared without any content from the beginning.


0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000