CSCI15a: Chapter 3

CSCI15a: Notes Chapter 3 Chapter 1 in second edition
Basic Java Structural Components


Any language has a Syntax or Grammar. Computer languages are no different. SUN provides a Learning the Java Language Tutorial ... and the book is good to read as well.

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):
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
Interesting note - the following are reserved but not used
cast future generic inner goto
operator outer rest var const

slide 6, 7
Literals: (or variable values)
Java uses five basic (primitive) element types: integer, floating point, boolean, character, and String*.
(Theoretically: four integer types, two floating-point types, char and boolean)

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?

Boolean
true false

Character (char): slide 12

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

A Program to dissect

Discuss: // calculate area of rectangle - version 1 import java.awt.*; import java.applet.Applet; public class Calculation extends Applet { public void paint(Graphics g) { int length; //declarations int width; int area; length= 20; // assignments width = 10; area = length * width; g.drawString("Area is " + area, 100, 100); // display the answer } }

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

You can also declare a bunch of variables at once:
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;

Assignment statement:
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.

Examples:

int n = 10; n = n + 1; n++; // an increment expression - if the + is post (after) then it adds after it uses n += 1; // essentially the same as n = n + 1; An increment expression has the side effect of incrementing the value by 1. If the operator ++ is a suffix (post), the value of the increment expression is the value of the variable before it is incremented. If the operator ++ is a prefix, the value of the increment expression is the value of the variable after it is incremented. (I.E., if the ++ is first it adds first.) Use sparingly to reduce confusion (often used only to increment loop variables). For example: int i = 0; int j = i++; // j is 0; i is 1 j = ++i; // if the + is pre (before) then it adds before it uses // j and i are both 2

Operators
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!

Precedence of operators is important here. On computers, the order is

  1. ()
  2. *, /, %
  3. +, -
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)

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.)

public class Division { public static void main (String args[]) { int b = 1; int c = 2; float d = b/c; System.out.println(b/c); System.out.println(d); } } Yields
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

What does the following yield?
int half = 20000*(1/2)

Output
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);

What were the variables there for? And what about:
g.drawString("Hello" + " there", 100, 100);

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:

g.drawString("Answer is " +1+2, 100, 100); and g.drawString("Answer is " +(1+2), 100, 100);

type conversion

I had mentioned above that Java converted the integer into a String. The programmer can do cast conversion as well if desired.
System.out.println(((float)(10+11)/2));
Yields 10.5

Very important

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.03.5
(More on this in new text Ch2 and old text Ch5) and at
this Java tech tip

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
Anywhere you could put an integer, you can put an integer expression.
What does that mean?

Slides. Don't forget to look at chapter summaries.