Why are total_amount
and tax_amount
concatenated together as strings instead of added together as numbers in the below println
statement?
public class Test{
int total_amount,tax_amount;
public void cal(int total_amount,int tax_amount)
{
System.out.println("Total amount : "+total_amount+tax_amount);
}
public static void main(String[] args) {
new Test().cal(100, 20);
}
}
Output Total amount : 10020
Expected Total amount : 120
out. println(2 + 0 + 1 + 5 + "GeeksforGeeks" + (2 + 0 + 1 + 6)); It prints the addition of both 2,0,1 and 5 and 2,0,1 and 6 based due to the precedence of ( ) over +. Hence expression in ( ) is calculated first and then further evaluation takes place.
It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.
To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.
That's because of operator precedence. Basically, your code is doing the equivalent of:
System.out.println(("Total amount : " + total_amount) + tax_amount);
So when total_amount
is 100, and tax_amount
is 20, that ends up being:
System.out.println(("Total amount : " + 100) + 20);
which is evaluated as:
System.out.println("Total amount : 100" + 20);
which is evaluated as:
System.out.println("Total amount : 10020");
Options:
Use parentheses to show how you want the operations to be grouped:
System.out.println("Total amount : " + (total_amount + tax_amount));
Perform the summation first, and store it in a new variable:
int totalIncludingTax = total_amount + tax_amount;
System.out.println("Total amount : " + totalIncludingTax);
As a side note, I'd recommend:
underscores_separating_words
, e.g. taxAmount
instead of tax_amount
total_amount
but then print something different with a label of Total amount
With code formatting as well, you'd end up with:
public class Test {
public static void main(String[] args) {
calculateTotal(100, 20);
}
private static void calculateTotal(int preTaxTotal, int tax) {
int totalIncludingTax = preTaxTotal + tax;
System.out.println("Total amount: " + totalIncludingTax);
}
}
(You should also consider what you're going to do for non-integer prices... I'd recommend either using integers, but make that the number of cents/pennies/whatever, or using BigDeciml
to represent prices.)
Java assumes that you want to concatenate the values as strings. You can prevent this by using brackets:
System.out.println("Total amount : "+ (total_amount + tax_amount));
Adding the brackets makes the two values be added before concatenating the result of the addition and the "Total amount :"
string.
Doing this
System.out.println("Total amount : "+total_amount+tax_amount);
is the same as printing
the concatenation of total_amount and tax_amount after the string literal Total amount :
so no math operation is done there
total_amount is holding the value 100 and tax_amount 20 therefore the output 10020
do instead
System.out.println("Total amount : " + (total_amount + tax_amount));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With