I was working on a problem, and I wanted to know how we can check if a variable holds any value inside the if condition in Java. Like in JavaScript, if we simply do like
Javascript code
let amount;
let discount;
if(amount){
discount=10;
}
I can simply check here if amount holds a value or not in JavaScript.
This is the code I need help in:
case: if I want to check if a variable holds a value in it?
class Shop {
private double amount;
private int discount;
public int getDiscount(){
if(amount){ // err: cannot convert double to bool
this.discount=10;
}
else{
this.discount=0;
}
return this.discount;
}
public static void main(String[] args) {
Shop s1= new Shop s2;
System.out.println("Discount="+ s1.getDiscount());
}