// Yazzy Latif // 06/26/2020 // TA: Grace Hopper // Expressions Examples // A few examples of using variables. public class Variables { public static void main(String[] args) { int x; // Declaring x = 3; // Assigning double y = 5.2; // Declare and assign on one line. y = 4.1 + 3.7; // Can reassign variables and can use expressions as the value System.out.println("x is " + x); // x is 3 x = x + 4; // Look up x, add 3, store result back into x x += 1; // Different syntax for x = x + 1 x++; // Also different syntax for x = x + 1 System.out.println("x is " + x); // x is 9 String word = "hello!"; // We can store Strings as well System.out.println(word); /* Some examples of common errors with variables. // Can't use a variable that has not been initialized to a value int number; System.out.println(number); // Can't use a variable before it's been declared // and initialized System.out.println(number2); int number2 = 5; // Can't declare a variable more than once int number3 = 102; int number3 = 45; // ERROR // Must store value of the correct type int number4 = 6.2; // ERROR int number4 = "6"; // ERROR double number5 = 10; // OKAY because 10 can be a double. */ } }