Memory Concepts o Variables O Every variable has a name, a type, a size and a value Name corresponds to location in memory When new value is placed into a variable replaces(and destroys) previous value O Reading variables from memory does not change them Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 11 Memory Concepts ⚫Variables Every variable has a name, a type, a size and a value ⚫Name corresponds to location in memory When new value is placed into a variable, replaces (and destroys) previous value Reading variables from memory does not change them
2.6 Memory concepts o Visual representation OSum=0: number1=1: number2= 2 number1 SUn number2 2 O Sum number 1 number 2 after execution of statement lumber 1 SUIn number2 2 Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 12 2.6 Memory Concepts ⚫Visual Representation Sum = 0; number1 = 1; number2 = 2; Sum = number1 + number2; after execution of statement sum 0 sum 3 number1 number2 1 2 number1 number2 1 2
Variables Used to store data in a program // Compute the first area radius=1.0 area = radius radius*3.14159 System. out. println("The area is"+ area +"for radius"+radius) // Compute the second area radius =2.0 area= radius radius *3. 14159 System. out. println("The area is "+ area+ " for radius"+radius) Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 13 Variables Used to store data in a program // Compute the first area radius = 1.0; area = radius * radius * 3.14159; System.out.println("The area is " + area + " for radius "+radius); // Compute the second area radius = 2.0; area = radius * radius * 3.14159; System.out.println("The area is “ + area + " for radius "+radius);
Declaring Variables Giving the name and the data type of variables int x l Declare x to be an / integer variable double radius:// Declare radius to ll be a double variable char a: / Declare a to be a character variable Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 14 Declaring Variables Giving the name and the data type of variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable;
Java data types ● Data type is the Java Types classification of forms of Primitive Reference information boolean char o Data type is Numeric d leclare ed using keywords Integral Floating Java is strongly[byte short long typed fioat double Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 15 Java data types ⚫ Data type is the classification of forms of information ⚫ Data type is declared using keywords ⚫ Java is strongly typed