Converting Data Types
Sometimes we need to convert from one data type to another. For example, math operations cannot be performed on Strings. To convert data types we can use the following:
Change String s to an integer
Change String s to an integer
Change double x2 to an integer
Example:
String num = "50";
int x;
double y = 25.2;
Arithmetic Operations
Arithmetic operations in Java follow the order of operations. Math calculations on integers and floating point numbers can be performed using the following operators:
Math.pow() Exponent
/ Division
% Modulus
* Multiplication
+ Addition
- Subtraction
Writing Code
Tracing through code by hand is a useful technique to help find errors in your code. If the value of a variable changes, write the new value in that variable’s column.
Use the following chart to trace the given java code.
int x = 2;
int y = 5 * 3;
int y = 5 * 3;
int y = 5 * 3;
int y = 5 * 3;
x = (int) Math.pow(y, 2);
num = "5";
System.out.print(num + 2);
System.out.print(Integer.parseInt(num)+2);
y = y + x;