Reputation: 11
Here's my source code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println( "Integer.MAX_VALUE = " + Integer.MAX_VALUE );
System.out.println( "Integer.MAX_VALUE + 1 = " + (Integer.MAX_VALUE + 1) );
System.out.println( "Integer.MAX_VALUE * 2 = " + ( Integer.MAX_VALUE * 2) );
System.out.println( "Integer.MAX_VALUE * 5 = " + ( Integer.MAX_VALUE * 5) );
System.out.println( "Integer.MAX_VALUE * 10 = " + ( Integer.MAX_VALUE * 10) );
System.out.println( "Integer.MAX_VALUE = " + Integer.MAX_VALUE );
System.out.println( "Integer.MIN_VALUE - 1 = " + (Integer.MIN_VALUE - 1) );
System.out.println( "Integer.MIN_VALUE * 2 = " + ( Integer.MIN_VALUE * 2) );
System.out.println( "Integer.MIN_VALUE * 5 = " + ( Integer.MIN_VALUE * 5) );
System.out.println( "Integer.MIN_VALUE * 10 = " + ( Integer.MIN_VALUE * 10) );
//Part 2
System.out.println( "Integer.MAX_VALUE + 1.0 = " + (Integer.MAX_VALUE + 1.0) );
System.out.println( "Integer.MAX_VALUE * 2.0 = " + ( Integer.MAX_VALUE * 2.0) );
System.out.println( "Integer.MAX_VALUE * 5.0 = " + ( Integer.MAX_VALUE * 5.0) );
System.out.println( "Integer.MAX_VALUE * 10.0 = " + ( Integer.MAX_VALUE * 10.0) );
System.out.println( "Integer.MIN_VALUE - 1.0 = " + (Integer.MIN_VALUE - 1.0) );
System.out.println( "Integer.MIN_VALUE * 2.0 = " + ( Integer.MIN_VALUE * 2.0) );
System.out.println( "Integer.MIN_VALUE * 5.0 = " + ( Integer.MIN_VALUE * 5.0) );
System.out.println( "Integer.MIN_VALUE * 10.0 = " + ( Integer.MIN_VALUE * 10.0) );
//Part 3
int a, b;
a = 1;
b = 2;
System.out.println( "The ints a, b are " + a + ", " + b );
System.out.println( "a + b is " + a + b );
System.out.println( "a - b is " + a - b );
System.out.println( "a * b is " + a * b );
System.out.println( "a / b is " + a / b );
//Part 4
double aD, bD;
aD = 1.0;
bD = 2.0;
System.out.println( "The doubles aD, bD are " + aD + ", " + bD );
System.out.println( "aD + bD is " + aD + bD );
System.out.println( "aD - bD is " + aD - bD );
System.out.println( "aD * bD is " + aD * bD );
System.out.println( "aD / bD is " + aD / bD );
}
}
Here's my error:
Compilation error time: 0.1 memory: 320512 signal:0
Main.java:37: error: bad operand types for binary operator '-'
System.out.println( "a - b is " + a - b );
^
first type: String
second type: int
Main.java:46: error: bad operand types for binary operator '-'
System.out.println( "aD - bD is " +aD - bD );
^
first type: String
second type: double
2 errors
I am new to java and I am still figuring out arithmetic. I thought I was doing fine but I don't understand what's gone wrong. It is most probably a real rookie mistake but could you tell me what I did wrong?
Upvotes: 0
Views: 2177
Reputation: 122008
The +
and -
considering as string operations where the expression being evaluation from left to right. To avoid the situation mark the subtraction comes under high precedence which happens when you surround with ()
System.out.println( "a - b is " + (a - b) );
Now because of the precedence the expression inside parenthesis (a-b)
evaluates first and then the whole expression from left to right.
Upvotes: 0
Reputation: 58898
"a - b is " + (a) - (b)`
means:
("a - b is " + (a)) - (b)
. The left part (("a - b is " + (a))
) is a string, and you can't subtract from a string.
You need to use parentheses:
"a - b is " + (a - b)
Upvotes: 3