18
CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

Embed Size (px)

Citation preview

Page 1: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

CS 0401

Debugging Hints and Programming Quirks for Java

byJohn C. Ramirez

University of Pittsburgh

Page 2: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

2

• These slides may be updated throughout the term to help you with your Java programming and debugging

• They are more or less in the order that the problem / issue comes to my attention

• Be sure to refer to the textbook and regular notes for additional programming and debugging information

Page 3: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

3

String Literals in your Program

• Java String literals cannot extend past the end of a line4 For example, the following is illegal:System.out.println(“I am printing out a very

long String so I am using two lines!”);

4 This is a compilation error: • “unclosed string literal”• Will occur on both lines in which literal appears

4 Note that “wrapped” lines are ok • Editor may move part of a line to the next line,

even though you never hit “Enter”

Page 4: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

4

Method Variable Initialization

• Java method variables (i.e. variables declared within methods) are not automatically initialized4 If you try to access the value of an

uninitialized method variable in a Java program you will get a compilation error• Error will occur even if variable “might” not be

initializedEx:

int i, j;j = 3 * i;

– “variable i might not have been initialized”

Page 5: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

5

Method Variable Initialization

Ex:int max;

String ok;System.out.println(“Do you want to continue? (y/n)”);ok = inScan.next(); // inScan is a legal Scannerif (ok.equals(“y”)){

System.out.println(“Enter max value”); max = inScan.next();

}System.out.println(“Max is “ + max);

• Still gives the error since if condition could be false

Page 6: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

6

Infinite Loops

• Recall the while loop syntaxwhile (booleanExpression)

loop_body; // Java statement, which could

// be a block

4 A common problem with while loops (and loops in general) is the infinite loop• Loop condition is never false

4 This can occur for many reasons, but a common one for intro programmers is a neglect to update the components of booleanExpression within the loop body• If expression doesn’t change, it will never be

false

Page 7: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

7

Infinite Loops

Ex:int count = 1, max = 10, sum = 0;

while (count <= max){

sum += count;}System.out.println(“Sum is “ + sum);

• Within loop, count does not change and neither does max

– Therefore, once true the condition will never be false

• Luckily, infinite loops are relatively easy to detect

– Program runs much longer than it should– Output is repeated over and over

Page 8: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

8

Entry Loop Programming

• Recall that entry loops test a condition to get into the loop body4 Repeats this test prior to each iteration4 When user input determines if loop should

continue or not, we can generalize the behavior as:

READ (read an initial value from the user)TEST (the loop entry condition)PROCESS (the loop body if the condition

was true)READ (read the next value from the user)

4 Note that the first READ is OUTSIDE of the loop

LOOP BODY

Page 9: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

9

Entry Loop Programming

• The idea is that we initially have to “set” the entry condition prior to the loop

• We then have to “reset” it at the end of the loop body, so it can be tested again for the next iteration

Ex:System.out.println(“Enter a pos. num.”);int num = inScan.nextInt();while (num > 0){ System.out.println(“You entered “ +

num); System.out.println(“Enter a pos.

num.”); num = inScan.nextInt();}System.out.println(“Good-bye”);

Page 10: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

10

Declaration Within Blocks

• As we discussed in lecture, the scope of method variables is the point in the block where they are declared to the end of that blockEx:

{// bunch of statementsint i;// bunch more statements

}

SCOPE OF i

Page 11: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

11

Declaration Within Blocks

4 When you have nested control structures, it is fairly common to declare variables within a block such as a loop body

4 Be careful to note that any variables declared within an inner block (such as an inner loop or inner if statement) will NOT be accessible once that block terminates• Note that this applies to variables declared in

the headers of for loops!!!

4 If there is any doubt, declare your variables in the outermost block of your method

Page 12: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

12

Declaration Within Blocks

for (int i = 0; i < 10; i++){

System.out.println(“i is “ + i);}System.out.println(“i is “ + i); // ERROR

System.out.println(“Enter two numbers”);int one = myScanner.nextInt(); int two = myScanner.nextInt();if (one >= two){

int max = one;}else{

int max = two;}System.out.println(“Max is “ + max); // ERROR

Page 13: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

13

Accidentally Declaring a Variable Twice

• Java variables can be declared only one time within a block (or a nested subblock)4 It you redeclare the same variable, the

compiler will give you an error:varname is already defined in …

4 A common way to get this error is via “copy and paste”• You declare and assign the variable in one

statement in some part of your code• You want to use it again somewhere else, so

you copy and paste the code (including the declaration)

• However, only the assignment should be copied!

Page 14: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

14

Accessing a null Reference Variable

• Java reference variables are used to access objects and their instance methods

• If we have not yet assigned an object to a variable, we typically initialize it to null4 This means that the variable exists but no

object yet exists• The reference does not point to anything

Ex:String S = null;

S

Page 15: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

15

Accessing a null Reference Variable

4 However, instance methods are associated with Objects• Thus, if the object does not exist, the methods

cannot be called

4 A common error in Java is an attempt to call an instance method from a null referenceEx: String S = null;

if (S.equals(“Wacky”))System.out.println(“This is

wacky!”);else

System.out.println(“Not wacky!”);

• Note that the result of this is NOT “Not wacky!” • Rather it is an error because the instance

methods do not even exist

Page 16: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

16

Accessing a null Reference Variable

• We cannot call S.equals() since there is no object referred to by S

• The error will be an exception, occurring during run-time:

java.lang.NullPointerException

• This is because the compiler cannot know if an object will exist or not, so it cannot check the error

• But when the method is actually called during execution, the interpreter discovers that no object exists (i.e. the reference is null)

4 Be careful to recognize this error – it is very common!

Page 17: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

17

Comparing References vs. Objects

• A common logic error is that of comparing references when you mean to compare objects4 We know that the == operator is used to

compare primitive types4 This operator also works with reference

types, but it compares the references, NOT the data within the objects that they refer to• To compare the data in the objects, we need to

call a method, typically the equals() method– Actual comparison depends on how equals() is

implemented in a given class

Page 18: CS 0401 Debugging Hints and Programming Quirks for Java by John C. Ramirez University of Pittsburgh

18

Comparing References vs. Objects

4 For example:String S1, S2, S3;S1 = new String(“Wacky”);S2 = new String(“Wacky”);S3 = S1;if (S1 == S3){System.out.print(“Same reference means the “);System.out.println(“same physical object”);

}if (S1 != S2){System.out.print(“Not the same reference / object “);System.out.println(“even though the data is the same”);

}if (S1.equals(S2)){System.out.println(“Same data in different objects”);

}