/* VariableExample.java Examples of creating and assigning variables of different types including casting and some expressions */ package pkg119_02_variables.variableEx; public class VariableExample { public static void main(String[] args) { // ints are integers, 32 bits // declare variable // whose type is int // and whose name is x int x; int highestInt; int numPuppies; // assign variable // store number 3 in x // variable is LHS // value is RHS x = 3; highestInt = 2147483647; // x = 7.9; // has fractional part //numPuppies = 2147483650; // too big // declare and assign variable in one line int y = -91; // other integer types byte b = 127; // 8 bits short s = 32767; // 16 bits // long values have L at the end long l = 922337203685477580L; // 64 bits // double -- 64 bits double d = 1.2; d = 44488.0; d = -5.5; double veryBig = 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0; double verySmall = .00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001; // exponential notation allows very big or small numbers d = 5.5E5; 5.5 * 10^5 = 550000 d = 5.5E-5; 5.5 * 10^-5 = 0.000055 // float -- 32 bits // float values have f at the end float f = 3.4028235E38f; // 3.4... * 10^38 float f2 = 340282350000000000000000000000000000000f; // same number float smallfloat = 1.4E-45f; // boolean for true and false boolean numWasBig; numWasBig = true; boolean scary = false; // char for single character char lettera = 'a'; char letterA = 'A'; // different variable name and value!! char space = ' '; //char noLetters = ''; // error! every char is exactly 1 letter/number/punctuation mark long, 0 is not 1 // String for text String str = "the quick brown fox jumps over the lazy dogs"; String stringa = "a"; // single character a String stringSp = " "; // single character space String emptyString = ""; // zero characters is fine for String // java checks types on assignment -- these are errors //int badint = 4.6; // double not okay for int //boolean badbool = 3; // int not okay for boolean //double baddoub = "hello"; // string not okay for double //String badstr = 99.33; // double not okay for String // type casting // implicit cast int isInt = 9; // int implicitly converted into double -- add .0 onto end double isDoub = isInt; isDoub = 123; // implicit cast happens for operations + - * / % double result = isInt + isDoub; // explicit cast // errors - double not autmatically converted into int // because we might be losing something //isInt = 5.5; //isInt = 5.0; // still error, even though there's actually nothing to lose //isInt = isDoub; // explicitly cast double to int // throw away fractional part isInt = (int)5.5; // now 5.0 isInt = (int)isDoub; // integer division fixed with explicit casting int numerator = 22; double answer = numerator / 3; // integer division gives 7 answer = numerator / 3.0; // fixed, using a double answer = (double)numerator / 3; // fixed, cast to a double before division answer = (double)(numerator / 3); // not fixed -- casting after integer division } }