Key learning on Primitives
- Data Types
- Perform arithmetic expressions and assignment in a program code
- Perform compound assignment operator (ie +=), add comments to describe the result of operator
- Determine what is result is in a variable as a result of an data type and expression (ie integer vs double)
- Answer: if the result in a variable is an integer number or the expression is int, then the data type is integer. If the result include a decimal point or the expression is double, then it is double.
- Input Primitive data
public class DefinePrimitives {
public static void main(String[] args) {
int anInt = 100; //It can be used in app that stored user information like age
double aDouble = 89.9; //It like integer, but It can used to store information(number) after the decimal point
boolean aBoolean = true; // It can be used to define a value is true or not which we can use it in if statement
String aString = "Hello, World!"; //It can be used to print information in the screen for users to see
String aStringFormal = new String("Greetings, World!");
}
}
DefinePrimitives.main(null)
int a = 100;
int b = 200;
int c = 0;
c = (a + b) * (a - b); // c is equal to (a + b) * (a - b)
c += (100 * a)/b * a; // c is equal to c + (a + b) * (a - b)
System.out.println("c: " + c);
Determine what is result is in a variable as a result of an data type and expression (ie integer vs double)
Answer: if the result in a variable is an integer number or the expression is int, then the data type is integer. If the result include a decimal point or the expression is double, then it is double.
double a = 12.2/2;
int b = (int)(a + 0.5);
System.out.println("12.2/2 =" + a);
System.out.println("12/2 = " + (int)a);
System.out.println("12.2/2 rounded = " + b);