Chapter 2
Chapter 2
Scope of Local
// Fine with no errors
public static void correctMethod()
{ int x = 1;
int y = 1;
Variables, cont.
// Fine with no errors
// i is declared
for (int i = 1; i < 10; i++)
{
public static void correctMethod() { x += i;
int x = 1; System.out.println(i);
int y = 1;
}
// i is declared // i is declared again
for (int i = 1; i < 10; i++) { for (int i = 1; i < 10; i++)
x += i; {
} y += i;
System.out.println(i);
// i is declared again
for (int i = 1; i < 10; i++) { }
y += i; }
}
} public static void incorrectMethod()
{
int x = 1; //local variable
int y = 1;
// Errors for (int i = 1; i < 10; i++)
public static void incorrectMethod() { {
int x = 0;
int x = 1; x += i;
int y = 1; System.out.println(x);
for (int i = 1; i < 10; i++) {
int x = 0; }
x += i; }
public static void main(String[] args)
} { int a = 10; //Global variable
}
correctMethod();
incorrectMethod();
}
Scope of Local
Variables,
• Caution:
cont.
• Do not declare a variable inside a block and then attempt to use it
outside the block.
Here is an
• Example of a common mistake:
for (int i = 0; i < 10; i++)
{
:
}
System.out.println(i);
• The last statement would cause a syntax error, because
variable i is not defined
outside of the for loop. for (int i = 0; i < 10; i++)
{
System.out.println(i);
}
Method
Abstraction
• You can think of the method body as a black box that
contains the detailed
implementation for the method.
Optional arguments for Input Optional return value
Method Header
Black Box
Method body
Benefits of
Methods
• Write a method once and reuse it anywhere.
• Information hiding. Hide the implementation
from the user.
• Reduce complexity.
QUIZ
Identify and correct the errors in the following program:
public class Test {
public static void main(String[] args)
{
nPrintln(5, "Welcome to Java!”);
}
The Math {
public static void main(String[] args)
{
Class
• Class constants:
double x = 28;
double y = 4;
– Others: min, max, abs, and // return the logarithm of given value when base is 10
System.out.println("log10 of x is: " + Math.log10(x));
random Methods System.out.println("log10 of y is: " + Math.log10(y));
// return a power of 2
System.out.println("exp of a is: " +Math.exp(x));
Methods
• exp(double a)
{
System.out.println(Math.pow(2, 8));
System.out.println(Math.exp(1));
System.out.println(Math.log(2.71));
– Returns e raised to the System.out.println(Math.pow(2, 3));
power of a. System.out.println(Math.pow(3, 2));
System.out.println(Math.pow(3.5, 2.5));
• log(double a) System.out.println(Math.sqrt(4));
– Returns the natural System.out.println(Math.sqrt(10.5));
logarithm of a. }
}
• log10(double a)
– Returns the 10-based OUTOUT:
Math.pow(2, 8)) return 256
logarithm of a. Math.exp(1) returns 2.71
Math.log(2.71) returns 1.0
• pow(double a, double b) Math.pow(2, 3) returns 8.0
– Returns a raised to the Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns 22.91765
power of b. Math.sqrt(4) returns 2.0
• sqrt(double a) Math.sqrt(10.5) returns 3.24
}
}
The min, max, and abs
Method
StOP here
The String
Type
• The char type only represents one character.
• To represent a string of characters, use the data type
called String.
– For example,
String message = "Welcome to Java";
• Example:
String m1 = "Welcome to Java";
System.out.println("The length of " + m1+ " is " +
m1.length());
Getting Characters from a
String
substring(beginIndex) Returns this string’s substring that begins with the character at the specified
beginIndex and extends to the end of the string, as shown in Figure
4.2.
substring(beginIndex, Returns this string’s substring that begins at the specified beginIndex
endIndex) and extends to the character at index endIndex – 1, as shown in
Figure 9.6. Note that the character at endIndex is not part of the
substring.