6-3 Variables A variable is a “named container” that holds a value. q = q; means: 1. Read the current value of q 2. Subtract it from 100 3. Move the result back into q count 5 mov ax,q mov bx,100 sub bx,ax mov q,bx
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Objectives:• Discuss primitive data types• Learn how to declare fields and local
variables• Learn about arithmetic operators, compound
assignment operators, and increment / decrement operators
• Discuss common mistakes in arithmetic
6-3
Variables• A variable is a “named container”
that holds a value.• q = 100 - q;
means: 1. Read the current value of q 2. Subtract it from 100 3. Move the result back into q
count5
mov ax,qmov bx,100sub bx,axmov q,bx
6-4
Variables (cont’d)• Variables can be of different data types: int,
char, double, boolean, etc.• Variables can hold objects; then the type is
the class of the object.• The programmer gives names to variables.• Names of variables usually start with a
lowercase letter.
6-5
Variables (cont’d)• A variable must be declared before it can
be used:
int count;
double x, y;
JButton go;
Walker amy;
String firstName;
Type
Name(s)
6-6
Variables (cont’d)• The assignment operator = sets the
variable’s value:
count = 5;x = 0;go = new JButton("Go");firstName = args[0];
6-7
Variables (cont’d)• A variable can be initialized in its
declaration:
int count = 5;JButton go = new JButton("Go");String firstName = args[0];
6-8
Variables: Scope• Each variable has a scope —
the area in the source code where it is “visible.”
• If you use a variable outside its scope, the compiler reports a syntax error.
• Variables can have the same name when their scopes do not overlap.
{ int k = ...; ...}
for (int k = ...){ ...}
6-9
Fields• Fields are declared outside all
constructors and methods.• Fields are usually grouped together,
either at the top or at the bottom of the class.
• The scope of a field is the whole class.
6-10
Fields (cont’d)public class SomeClass{
}
Fields
Constructors and methods
Scope
public class SomeClass{
}Fields
Constructors and methodsScope
Or:
6-11
Local Variables• Local variables are declared inside a
constructor or a method.• Local variables lose their values and are
destroyed once the constructor or the method is exited.
• The scope of a local variable is from its declaration down to the closing brace of the block in which it is declared.
6-12
Local Variables (cont’d)public class SomeClass{ ... public SomeType SomeMethod (...) {
{
} } ...}
ScopeLocal variable declared
Local variable declared
6-13
Variables (cont’d)• Use local variables whenever appropriate;
never use fields where local variables should be used.
• Give prominent names to fields, so that they are different from local variables.
• Use the same name for local variables that are used in similar ways in different methods (for example, x, y for coordinates, count for a counter, i, k for indices, etc.).
6-14
Variables (cont’d)• Common mistakes:
public void someMethod (...){ int x = 0; ... int x = 5; // should be: x = 5; ...
Variable declared twice within the same scope — syntax error