Week 1 Meeting 2

Language constructs

Java is case sensitive.  Variable  aa is not the same as variable  aA

Identifiers (names of classes, variables, and methods):

·         Begin with letter

·         May contain: upper and lowercase letters, digits, underscore, $

By strong convention:                                  

·         Class names start with an uppercase character

·         method and variable names start with a lowercase character

·         multiple word names capitalize the first letter of the second and following words, e.g., myBooleanVariable

Reserved words

Have special meaning to the compiler and may not be used as identifiers.

Strong-typing concepts supported by Java

int j;    // an integer named j – primitive data

String s; // a String named s – an object


Kind of literal


Java type

Primitive or Object type



byte, short, int, or long


floating point


float, double







true // notice this is a reserved word, as is false








Character type

Java characters are Unicode instead of ASCII characters (ASCII characters may be stored in a single byte).


Variables and Default values

When a variable (of any kind) is declared it gets a value even if the programmer doesn’t give it one.  There is one value for each type, called the default value for that type, that is given to all variables declared to be of that type as soon as the variable becomes active.

Default value table


default value
















<the nul character> (ASCII value == 0)

any object type

null (the null reference)



//   (everything on this line is a comment)

/*   (comment opener)

Everything is a comment that follows the comment opener and appears prior to the (comment closer) */

Calling methods -- dot notation

When a variable or class name is followed by a . the following actions happen at run time:

  1. if the thing preceding the . is a variable name, follow the reference the variable is holding to the object it is pointing to.
  2. if the thing preceding the . is a class name, find the class with this name.
  3. look for a method with the name following the .
  4. ask the object or class to execute this method with the parameters specified.
  5. syntactically replace the code that called the method call with the value returned by the method.


String s = "Hello";

char c = s.charAt(0);  // get the String object

// that the reference held in the variable s points to

// ask this object to execute its charAt() method, which

// returns a char  (‘H’ in this case)

// store the ‘H’ in the variable c


String operations

Know how to use the methods shown in text section 2.3.2 (http://math.hws.edu/javanotes/c2/s3.html).

Null references vs Empty Strings

A null reference is a variable of some object type that has not been given a value, or has the value null.

An empty string is a variable holding a reference to a real string object that contains no characters.


See ../demos/W01D01.javastringCompareNullReference(String s1, String s2)

Methods from the Math class

Know how to use the methods shown in text section 2.3.1.

See ../demos/W01D01.javamathEx(double i, double j)

Assignment Statement (operator  =  )

Left side names some variable.

Right side is some expression that is evaluated.

Causes the value that results when the right side is evaluated to be stored in the memory location named by the variable on the left side.


iNumCalories = 5 + 8 ;

first adds the 5 and the 8 to get the value 13

then stores 13 in the memory location named iNumCalories


If the right side contains a variable name, the value to use in the evaluation is obtained by finding the memory location named by the variable and reading the value in this memory location.

iNumCalories = iCals ;

iCals is a variable name.  The system goes to the location holding iCals and reads the value that is there.  This value is then stored in the memory location named iNumCalories


Operator Precedence

What is the result of evaluating the following expression:

5 + 2 * 6 / 4

The  +  operator

means string concatenation if either operand is an object of type String

otherwise, it means numeric addition. 

Note:  +  is the only operator that will attempt to convert a value of a numeric type to a value of type String.

Implicit Type Conversion

Integer arithmetic

Consider the expression 2 / 5

·         note that both operands are integer values. 

·         Whenever both operands of an arithmetic expression are integers, the result is also an integer.

·         Integer division works as follows:

1.    divide the numerator by the denominator and obtain the floating point result --  2 / 5 is 0.4

2.    truncate the fractional portion of the floating point result leaving the integer shown to the left of the decimal point – 0 in this example.

Mixed Integer and Floating point arithmetic

If one operand of an expression is an integer and one is a floating point number, the arithmetic operators will automatically convert the integer value to a floating point value, but will not convert a floating point value to an integer value.

Assignment promotion

If the right side of an assignment operator is an integer and the left side names a location (variable) declared to hold a floating point value, the integer is converted to a floating point number before the assignment is done.


The assignment operator will not truncate a floating point value found on the right side if the left side names a location declared to hold an integer value.


Likewise, the assignment operator will not convert a numeric value on the right into a String value if the left side requires a String value.

Relational Operators for primitive values

== means IS EQUAL TO?

!= means IS NOT EQUAL TO?

Logical Operators

&& means AND

|| means OR

! means NOT


All relational and logical operators return a boolean value.


&& and || are short circuited operators.  If the result is decipherable from the left argument, the right argument is not evaluated.


At the end of stringCompareNullReference(String s1, String s2), there is a demo of this that has been commented out.


Printing from methods

So far, JGrasp has helped us:

·         create objects

·         call methods supported by these objects and pass values to these methods

·         View the values returned by the methods we call.


In addition to returning a value, methods can also write data to an output stream

The default output stream (also called standard output) is shown by JGrasp in the Run I/O window.

The method println() takes one parameter and writes the value passed to standard output.  The println() method is owned by the object named out in the class named System


We call this method by asking the System class for it’s out object, then asking this object to execute its println() method to print the value we pass, e.g.,


System.out.println("Hello World!");


prints a visual representation the String object “Hello World!” in the Terminal window.


The notes at this link contain more information on what the syntax System.out.println() really means.


println() takes one argument, but this argument can be an arbitrarily complex expression, e.g., the argument to println() could be the result of concatenating two String values as in:


        System.out.println( "My flavor is: " + flavor);


Remember: printing a value is not the same as returning a value.

If statements


if ( <boolean_expression> )

            <single_statement_or_block_of_statements_enclosed_in_braces> ;


            <single_statement_or_block_of_statements_enclosed_in_braces> ;


Example: if flavor is holding the string “chocolate”,

print “Oh, yes” and set calories = 0;

otherwise, print “OK, if I must have” whatever flavor is followed by “, I will.”


== is the operator to compare primitive types

do not use == to compare strings – you are not comparing the string objects themselves.  Instead you are comparing the references to the string objects.


The statement following the if statement is executed no matter which branch of the if statement is executed.

Nested If statements

if () <statement> else <statement> is a single statement, so where <statement> appears above, an entire if statement may appear.


An if statement inside an if statement is called a nested if statement.


As a good programming heuristic, do not put an if statement in the then branch of another if statement.


See ../demos/W01D01.javastringCompareNullReference(String s1, String s2)  // nested ifs demo at the bottom of the method