//IntVariable.java //demo an int variable storing a value and varying its value. //to "understand the quintessence of programming." public class IntVariable { public static void main (String [] args) { int students; //"declare"/create an int "variable" (a variable of "type" int). //think of it as a little box in the computer but it's actually // a named location in memory. //its name is students. //it can store/hold one int value (one integer). //names of variables can consist of letters, digits and _ but // cannot start with a digit. name should be meaningful, // indicating what the variable is used for, what its purpose is. //now the variable exists but has no value. it would be an error //to try to use the value of the students variable. it must be //assigned a value first. students = 12; //assign 12 to be its value. = is the "assignment" "operator". //it is an action, not an equation. leftside must be a variable, //rightside an expression. rightside is evaluated, then that (single) //value is assigned to be the value of the leftside variable. //RHS value must be compatible type. System.out.println( "variable is now: " + students ); //+ here is string "concatenation" (joining together two strings into one) students = students + 1; //add one to the students variable. //assign its current's value plus one as its new value. //rightside is students+1 students is 12, so it's 12+1 which is 13, //so 13 is assigned to leftside variable System.out.println( "and now it's: " + students ); //syntax errors: use undeclared variable, use before assigned a value, //assign incompatible type. //arithmetic operators int num1=12, num2=5; //declare and initialize variables int sum, difference, product, quotient, remainder; sum = num1 + num2; //addition difference = num1 - num2; //subtraction product = num1 * num2; //multiplication quotient = num1 / num2; //division remainder = num1 % num2; //remainder (modulus, "mod") System.out.println( "sum: " + sum + "\ndifference: " + difference + "\nproduct: " + product + "\nquotient: " + quotient + "\nremainder: " + remainder); } } // -5 -num //try making the num2 0 and see the error. //more complex expression:2*num1 + 4*num2 //change the types of the variables to double