7
In a if(!aplicaDescontoDe(valor));
in which the method aplicaDescontDe
is a boolean, how it works not understood?
In that Example:
public boolean aplicaDescontoDe(double porcentagem) {
if(porcentagem >0.3) { //se desconto for maior que 30%
return false;
}
this.valor -= this.valor * porcentagem;
return true;
}
...
if(!livro.aplicaDescontoDe(0.3)) {
System.out.println("Desconto no livro nao pode ser maior que 30%");
} else {
System.out.println("Valor no livro com desconto: " + livro.getValor());
}
Why do I need the !
(exclamation) in if
?
The operator
!
functions as a negation, i.e., if it returns 1, the function will be denied and passed as 0, if it returns 0 will be denied and passed as 1.– MarceloBoni
@Marcelobonifazio, cannot use the operator
!
(not) in integer values, when you say1
and0
you meantrue
(1
) andfalse
(0
)?– Fernando Leal
@Fernando was an unfortunate example rs, what I meant was:
if(!funcao())
if the function return is 1 ortrue
, it will be denied, and vice versa, if the return by chance is aint,
ai gives aexception
(believe)– MarceloBoni
@Marcelobonifazio, I suspected, I only quoted because beginners can see this and get confused, and in case you try to make a denial in a whole:
if(!1)
, for example, it will generate a build error and not run error, as can be seen here.– Fernando Leal