/* Variables1.java CIS 160 David Klick 2015-09-02 Demonstration of declaring and initializing variables. */ public class Variables1 { public static void main(String[] args) { // Variables are named memory locations that store data. // We have to specify a name and data type to create a variable. // Java has eight built-in data types. // Declaring integer numeric variables: byte varByte; // only uses one byte of storage internally short varShort; // uses two bytes of storage internally int varInteger; // uses four bytes of storage internally long varLong; // uses eight bytes of storage internally // Declaring floating-point numeric variables: float varFloat; // uses four bytes of storage internally double varDouble; // uses eight bytes of storage internally // Declaring character and Boolean variables: char varCharacter; boolean varBoolean; // Once a variable has been declared, you can then use it. // The first time a variable is set to a value, // it is called "initializing the variable" varByte = 23; // byte variables can only hold values from -128 through 127 varShort = -3000; // Data types that take more space can hold bigger ranges of values varInteger = -42385; // Note: commas are NOT allowed in numbers varLong = -3000000; // Note: commas are NOT allowed in numbers varFloat = 3.14f; // Floating-point can have fractional parts // Note: Since 3.14 would be a double, the trailing 'f' tells Java // it is just a float in this case so it can be stored in varFloat. // Bigger data types can not be stored in smaller data types normally. // but smaller data types can be stored in bigger data types. // So you can store a float in a double, but not a double in a float. // It would be like trying to store a gallon of water in a pint container. varDouble = 0.333333; // Some numbers, such as 1/3 can't be exactly represented varCharacter = '?'; // char variables hold only a single character varBoolean = true; // boolean variables can only be true or false } }