Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int variables being concatenated instead of added inside System.out.println()

Tags:

java

string

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
like image 952
dhaval joshi Avatar asked Oct 17 '16 07:10

dhaval joshi


People also ask

How do you do addition in system out Println?

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.

What happens when an int is added to a string?

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.

Can an int be concatenated to a string in Java?

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.


3 Answers

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:

  • Following Java naming conventions, using camelCase instead of underscores_separating_words, e.g. taxAmount instead of tax_amount
  • Naming variables more carefully - it's odd to have a variable called total_amount but then print something different with a label of Total amount
  • Using a static method here, as you're not actually using the fields in your object. (Those fields are confusing, as you're not using them.)

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.)

like image 96
Jon Skeet Avatar answered Oct 02 '22 15:10

Jon Skeet


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.

like image 28
Hexaholic Avatar answered Oct 02 '22 16:10

Hexaholic


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));
like image 36
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 02 '22 16:10

ΦXocę 웃 Пepeúpa ツ