solve using java expressions 2 + “(int) 2.0” + 2 * 2 + 2?
Since there’s a string constant embedded in the expression, I assume the result is supposed to be a string. Which is weird, but maybe it’s a trick question from your teacher.
A Java program to print the answer is:
class Homework {
public static void main (String[] args) {
String x;
x = “” + 2 + “(int) 2.0” + 2 * 2 + 2;
System.out.println (x);
System.exit (0);
}
}
I think it’ll print “2(int) 2.06” which is weird.
Are you sure there’s a string embedded in it?
If there’s no string, the program could be:
class Homework {
public static void main (String[] args) {
int x;
x = 2 + (int) 2.0 + 2 * 2 + 2;
System.out.println (x);
System.exit (0);
}
}
That’ll print 10, I think.
Sure is weird that there’s an embedded string. Since there is, I guess the answer is “2(int) 2.06”.
2 + “(int) 2.0” + 2 * 2 + 2