Variables and Types
- Variable
- Data in memory is referred to by name. This is called a variable. You can think of a variable name as the human-usable equivalent of a memory address.
- Type
- Every variable has a type (what kind of thing it is). Eg, int, String, ...
- Declaration
- Every variable must be declared once before using it. This tells the
compiler that you're going to use it, and what kind of a thing it is.
String fullName;
- Value
- Every variable has a value. You can use the value in a calculation,
or assign a new value to a variable. All variables must have a value
assigned to them before they can be used.
fullName = "Michael Mortimer Maus";
Assignment statement
An assignment statement has this form.
variable = expression;
Where expression can be one of the following:
- A constant.
fullName = "Brenda Begoode";
- Another variable.
fullName = nameEntered;
- A more complex expression using method calls and operators.
fullName = JOptionPane.showInputDialog(null, "Enter your name - or else!");
Types
- Numbers -- integer and floating-point. (Wu p87...)
- Strings
- Many others.
Numeric types
- Integers - numbers without a decimal point
byte
,short
,int
,long
- We will use
int
for integer variables. int
range is -2147483648 to +2147483647 .-
int age; age = 24;
- Floating-point - numbers with a decimal point
float
,double
- We will use
double
for floating-point variables. double
range is about +/- 10308.double
precision is about 15 significant digits.-
double temperature; temperature = 65.377;
More than one variable in same declaration
It's possible to declare more than one variable in the same declaration.
double height, weight;
But I would prefer that you put them in separate declarations. It makes documenting them easier, and avoids errors in certain cases.
double height; // Height in centimeters. double weight; // Weight in kilograms.
Declaring and initializing in one statement.
Before you can use a variable, you have to declare it and initialize it.
String fullName; fullName = "nobody";
It is common to initialize a variable in the declaration.
String fullName = "nobody";
Numeric operators
- You can perform calculations on numbers with these common operations.
- Addition (
+
)8 + 3
is 11 - Subtraction (
-
)8 - 3
is 5 - Multiplication (
*
)8 * 2 is 16
- Division (
/
)8 / 2
is 4 - Remainder (
%
)5 % 3
is 2
- Addition (
- Warning: integer division produces integer result.
3 / 2
is 1, not 1.5 - If either operand is floating-point, the result is floating-point.
This applies to all numeric operations (not just division).
- 3 / 2 is 1 (integer)
- 3.0 / 2 is 1.5 (floating-point)
- 3 / 2.0 is 1.5 (floating-point)
- 3.0 / 2.0 is 1.5 (floating-point)
Precedence and the order of operations
- Multiplication, Division, and Remainder are performed before Addition and Subtraction
a + b * c is a + (b * c)
- Use parentheses to control order of evaluation.
- Equal precedence operations done left-to-right. a+b+c is (a+b)+c
String Concatenation
- Concatenation - Creates a new string by following left operand by right operand.
- If one of the operands is String, it will convert the other to String if necessary.
- Example
String s; s = "abc" + "def"; // same as s = "abcdef"; s = "abc" + 4; // same as s = "abc4" s = "abc" + (3 + 4); // same as s = "abc7" s = "abc" + 3 + 4; // same as s = "abc34"
Common String methods (Wu p61...)
String s1 = "goodbye"; String s2; int i;
- Note: characters in a String are numbered starting at 0.
- substring
s2 = s1.substring(0,3); // same as s2 = "goo" s2 = s1.substring(1, 5); // same as s2 = "oodb"
- length
i = s1.length(); // same as i = 7 s2 = s1.substring(i-1, i); // last char in s1 (assuming i=s1.length()
- searching with indexOf
i = s1.indexOf("b"); // same as i = 4 i = s1.indexOf("bye"); // same as above i = s1.indexOf("B"); // i = -1 Uppercase not same as lowercase.