Java is an object-oriented language so it needs ways to write objects (in the form of classes), and then to say what these classes look like (remember - variables (properties/state) and methods (behavior)).
Classes that are related are organized into packages. slide 1 We have seen how java organizes classes into packages in the API and will look deeper into the API in lab 3.
Packages and Classes are defined by you (or in the API library) through Source files. slide 2,3
In this chapter we will talk about how to represent properties as variables and then how to use variables to make statements and write methods.
Valid Identifiers (or variable names): slide 4
Identifiers must begin with a letter and be a sequence of letters or
digits (0-9).
A letter is defined as A-Z, a-z, _, $
Case is significant (Case Sensitive)
No length requirement
Examples
Keywords: slide 5
Class names are started with capitals (Applet, Graphics, MyApplet).
Methods and variable names are started with lower case (paint, myVariable)
Interesting note - the following are reserved but not used
| cast | future | generic | inner | goto |
| operator | outer | rest | var | const |
Integer: if you type 4, you get an int with value 4 slide 8
| decimal | octal | hexadecimal |
| 2.21 | 077 | 0xDC00 |
Integer:
| Length | Name | 8 bits | byte | 16 bits | short | 32 bits | int | 64 bits | long |
slide 9, 10, 11
Floating Point (Decimal)
3.14
3.1E12
2e12
12.34f
-32.0f
Float:
| Length | Name | 32 bits | float | 64 bits | double |
If you omit the f Java assumes double. So, what would be
wrong with
float myValue = 12.3;
What would happen?
if you type 'a', you get a char with value a
\t (tab)
\b (backspace)
\n (linefeed)
\r (carriage return)
char myCharacter = 'm';
Why the single quotes?
Boolean slide 13
slide 14-19
You can also declare a bunch of variables at once:
Assignment statement:
Examples:
Operators
Precedence of operators is important
here. On computers, the order is
The % stands for remainder. I.e., what is left after you
divide. See text.
Careful with division on ints (Integers). Division on integers
always yields an integer. So 1/2 is 0 (the answer (0.5) is truncated to 0.)
What does the following yield?
Output What were the variables there for? And what about: Remember the + was concatenation. The same happens in the
following line, but the area number is first converted by Java
into a String...then it is concatenated
g.drawString("Area is " + area, 100, 100);
Consider the differences between:
type conversion
I had mentioned above that Java converted the
integer into a String. The programmer can do cast conversion
as well if desired.
The idea here is that no information is "added" - i.e., if I have 2 then it does not hurt to make it a 2.0 for computation in the method.
The Role of Expressions
Slides. Don't forget to look at chapter summaries.
A Program to dissect
Discuss:
Alternatives
Defaults
Sometimes you know what a variables value is going to be
before anyone even uses a method. Specifically, that variable
value is always the same. This is often called the default.
One can specify in the declaration the value as well if it is
known for sure. I.e.,
float pi = 3.14f;
Actually though public final static double PI is
in java.lang.Math
int length, width, area;
but it is good programming style to put variables that
are related together and ones that are different on separate lines
How variable names and values and memory work
What happens in the computer when you do
length= 20;
Calculations and Operators
What happens in the computer when you do
area = length * width;
variable = expression;
The place where it is stored (variable name for memory location) is on the left-hand side. On the right-hand side are expressions; expressions calculate to the values in those memory locations. Variables (left) are memory locations where the values of expressions (right) are stored once calculated.
More in Chapter 5 (Ch. 2 in new edition) - look ahead for some examples of operators and how to write
math expressions. In fact, the book has good examples of
common mistakes - nice to know!
meaning first (1) anything in parentheses is done, then any operator
in (2) is done left to right and then
any operator in (3) is done left to right.
(Actually, there is more, but this is good for our needs for now)
boris:/user/faculty/amk/java/test> java Division
0
0.0
Here is the file for you to use for testing future problems. Download it, compile it and run it. Then try your own expressions, recompile, run, etc
int half = 20000*(1/2)
Above, I had a java application so I used
System.out.println(b/c);
We have seen output on applets as
g.drawString("Hello", 100, 100);
g.drawString("Hello" + " there", 100, 100);
System.out.println(((float)(10+11)/2));
Yields 10.5
When an operator manipulates a mixture of int and float (or double)
values, any integers are temporarily converted to float for
the purpose of the calculation. E.g.,
7 / 2.0
7.0 / 2.0
3.5
(More on this in new text Ch2 and old text Ch5)
and at this Java tech tip
Java also can do automatic casting. For example, the
method Math.pow
public static double pow(double a,double b)
can be called with Math.pow(4,2)
Anywhere you could put an integer, you can put an integer expression.
What does that mean?
![]()