JAVA PROGRAMMING LESSON 4 - Variables Variables Variable is a named storage location that stores data and the value of the data may change while the program is running. To name a variable, use the camelCase format ‐ starting with a lowercase letter. Types of Variables There are two types of variables in Java: Primitives 2. Reference variables 1. Primitives Variables 1. 2. 3. 4. 5. i. ii. iii. iv. A primitive can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change. Primitive variables can be declared as static variables , instance variables, method parameters, or local variables. One or more primitives, of the same primitive type, can be declared in a single line. Examples of primitive variable declarations: char huruf; boolean myBooleanPrimitive; int x, y, z; // declare three int primitives double area; Primitive types Type Name boolean char byte short int long float double Kind of value true or false integer integer integer integer integer floating-point number floating-point number Memory used 1byte 2bytes 1byte 2bytes 4bytes 8bytes 4bytes 8bytes Declaring primitive type 1. Example of declaring Java variables with primitive types int numberOfBeans; double oneWeight, totalWeight; 2. The declaration of a variable can be combined with its initialization via an assignment statement int count = 0; double distance = 55 * .5; char grade = 'A'; 3. Note that some variables can be initialized and others can remain uninitialized in the same declaration int initialCount = 50, finalCount; Reference Variables A reference variable is used to refer to (or access) an object. Store the locations of objects in the computer memory A reference variable can be used to refer to any object of the declared type, or of a subtype of the declared type (a compatible type). Reference variables can be declared as static variables, instance variables, method parameters, or local variables. One or more reference variables, of the same type, can be declared in a single line. Examples of reference variable declarations: i. Object o; ii. Animal myNewPetReferenceVariable; iii. String s1, s2, s3; //declare three String vars. Instance Variable Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated. Instance variables are the fields that belong to each unique object. For example, the following code defines fields (instance variables) for the radius of circle objects: public class Student { private String name; private double cpa; : public static void main (String[] args) { : } } The term "field," "instance variable," "property," or "attribute," mean virtually thing the same thing. Local Variable 1. 2. 3. 4. 5. 6. Local variables are variables declared within a method. That means the local variable starts its life inside the method, it's also destroyed when the method has completed. Local variables are always on the stack, not the heap. Although the value of the variable might be passed into another method that then stores the value in an instance variable, the variable itself lives only within the scope of the method. Before a local variable can be used, it must be initialized with a value. Local variable declarations can't use most of the modifiers that can be applied to instance variables, such as public (or the other access modifiers private, protected), transient, volatile, abstract, or static. Local Variable public class Addition { public static void main( String args[]){ String first, second; int number1,number2,sum; final double PI = 3.14; first=JOptionPane.showInputDialog("Enter 1st int" ); second=JOptionPane.showInputDialog("Enter 2nd int"); number1 = Integer.parseInt(first); number2 = Integer.parseInt(second); sum = number1 + number2; JOptionPane.showMessageDialog(null,"The sum is" + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit(0); 10 } } Constant Constant is a named storage location which once initialized with a value, the value cannot change during the execution of the program. 2. Java constant should be named using uppercase letters with underscore characters as separators, if several words are linked together. Example: 1. PIE MIN_HEIGHT 3. Constant is declared using the final keyword . Syntax: final datatype CONSTANTNAME =VALUE; 4. Example: final = 3 14; Static Variable Static variables store values for the variables in the common memory location. Static variable represents classwide information – all objects of the class share the same piece of data. All object of the same class are affected if one object changes the value of a static variable. Syntax to declare static variable: modifier static datatype varname; Example: static int total; private static int count; END OF LESSON 4 THE END