lesson 4 - variables java programming. variables variable is a named storage location that stores...
TRANSCRIPT
LESSON 4 - Variables
JAVA PROGRAMMING
VariablesVariable 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 VariablesThere are two types of variables in Java:1.Primitives2.Reference variables
Primitives Variables1. 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 primitivesiv. double area;
Primitive typesType NameKind of value Memory
usedboolean true or false 1bytechar integer 2bytesbyte integer 1byteshort integer 2bytesint integer 4byteslong integer 8bytesfloat floating-point number 4bytesdouble floating-point number 8bytes
Declaring primitive type1. Example of declaring Java variables with
primitive typesint 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 VariablesA reference variable is used to refer to (or access) an
object.Store the locations of objects in the computer
memoryA 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,"
meanvirtually thing the same thing.
Local Variable1. 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.
Local Variablepublic 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}}
Constant1. 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_HEIGHT3. Constant is declared using the final keyword .
Syntax:final datatype CONSTANTNAME = VALUE;
4. Example:final = 3 14;
Static VariableStatic 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