Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the.

Similar presentations


Presentation on theme: "LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the."— Presentation transcript:

1 LESSON 4 - Variables JAVA PROGRAMMING

2 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.

3 Types of Variables There are two types of variables in Java: 1. Primitives 2. Reference variables

4 Primitives Variables 1. A primitive can be one of eight types: char, boolean, byte, short, int, long, double, or float. 2. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change. 3. Primitive variables can be declared as static variables, instance variables, method parameters, or local variables. 4. One or more primitives, of the same primitive type, can be declared in a single line. 5. Examples of primitive variable declarations: i. char huruf; ii. boolean myBooleanPrimitive; iii. int x, y, z; // declare three int primitives iv. double area;

5 Primitive types Type NameKind of valueMemory used booleantrue or false1byte charinteger2bytes byteinteger1byte shortinteger2bytes intinteger4bytes longinteger8bytes floatfloating-point number4bytes doublefloating-point number8bytes

6 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;

7 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.

8 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.

9 Local Variable 1. Local variables are variables declared within a method. 2. That means the local variable starts its life inside the method, it's also destroyed when the method has completed. 3. Local variables are always on the stack, not the heap. 4. 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. 5. Before a local variable can be used, it must be initialized with a value. 6. 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.

10 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 }

11 Constant 1. 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: PIE MIN_HEIGHT 3. Constant is declared using the final keyword. Syntax: final datatype CONSTANTNAME = VALUE; 4. Example: final = 3 14;

12 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;

13 END OF LESSON 4 THE END


Download ppt "LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the."

Similar presentations


Ads by Google