Chapter 6 - Data types, variables, and arithmetic. In this chapter, the learning objectives are: 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.
Trang 1Data Types, Variables,
and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Trang 2Objectives:
• Discuss primitive data types
• Learn how to declare fields and local
Trang 3mov ax,q mov bx,100 sub bx,ax mov q,bx
Trang 4Variables (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
Trang 7JButton go = new JButton("Go");
String firstName = args[0];
Trang 8Variables: 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 = ) {
.
}
Trang 9Fields
• 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
Trang 10public class SomeClass
{
}
Fields
Construct ors and methods Scope
Or:
Trang 11Local 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
Trang 12Local Variables (cont’d)
public class SomeClass
Local variable declared
Trang 13variables, etc.).
Trang 15Declares a local variable
radius; the value of the field radius remains 0.0
Trang 16Java Methods
Trang 17Strings
• String is not a primitive data type
• Strings work like any other objects, with two exceptions:
Strings in double quotes are recognized as literal constants
+ and += concatenate strings (or a string and a number or an object, which is converted into a string)
"Catch " + 22 "Catch 22"
Trang 19Symbolic Constants
• Symbolic constants are initialized final
variables:
private final int sideLength = 8;
private static final int BUFFER_SIZE = 1024; public static final int PIXELS_PER_INCH = 6;
Trang 20Why Symbolic Constants?
• Easy to change the value throughout the program, if necessary
• Easy to change into a variable
• More readable, self-documenting code
• Additional data type checking by the
compiler
Trang 21Arithmetic
• Operators: +, -, /, * , %
• The precedence of operators and
parentheses is the same as in algebra
• m % n means the remainder when m is
divided by n (for example, 17 % 5 is 2;
2 % 8 is 2)
• % has the same rank as / and *
• Same-rank binary operators are performed in order from left to right
Trang 22Arithmetic (cont’d)
• The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in
expressions
• If one operand is an int and another is a
double, the result is a double; if both
operands are ints, the result is an int
Trang 23The double type of
the result doesn’t
help: ratio still gets the value 0.0.
Trang 24Arithmetic (cont’d)
• To get the correct double result, use double
constants or the cast operator:
double ratio = 2.0 / 3;
double ratio = 2 / 3.0;
int m = , n = ;
double factor = (double)m / (double)n;
double factor = (double)m / n;
double r2 = n / 2.0;
Casts
Trang 25Arithmetic (cont’d)
int ptsOnDie = (int)(Math.random() * 6) + 1; int miles = (int)(km * 1.61 + 0.5);
Returns a
double
Converts kilometers to miles, rounded to the nearest integer
Trang 27a = a + 1; a++;
a = a - 1; a ;
Do not use these in larger expressions
Trang 28From Numbers to Strings
• The easiest way to convert x into a string is to concatenate x with an empty string:
String s = x + "";
• The same rules apply to System.out.print(x)
'A' 123 -1 1 3.14 Math.PI
Trang 29From Objects to Strings
• The toString method is called:
public class Fraction
Trang 30• What is meant by the scope of a variable?
• What is the scope of a field?
• What is the scope of a local variable?
Trang 31Review (cont’d):
• Is it OK to give the same name to variables in different methods?
• Is it OK to give the same name to a field and
to a local variable of the same class?
• What is the range for ints?
• When is a cast to double used?
Trang 32what is the value of dC?
• When is a cast to int used?
• Should compound assignment operators
be avoided?