2Organization of Programming Languages-Cheng (Fall 2004)
Contents
3Organization of Programming Languages-Cheng (Fall 2004)
4Organization of Programming Languages-Cheng (Fall 2004)
5Organization of Programming Languages-Cheng (Fall 2004)
output
6Organization of Programming Languages-Cheng (Fall 2004)
7Organization of Programming Languages-Cheng (Fall 2004)
8Organization of Programming Languages-Cheng (Fall 2004)
9Organization of Programming Languages-Cheng (Fall 2004)
10Organization of Programming Languages-Cheng (Fall 2004)
11Organization of Programming Languages-Cheng (Fall 2004)
12Organization of Programming Languages-Cheng (Fall 2004)
13Organization of Programming Languages-Cheng (Fall 2004)
14Organization of Programming Languages-Cheng (Fall 2004)
15Organization of Programming Languages-Cheng (Fall 2004)
16Organization of Programming Languages-Cheng (Fall 2004)
17Organization of Programming Languages-Cheng (Fall 2004)
18Organization of Programming Languages-Cheng (Fall 2004)
19Organization of Programming Languages-Cheng (Fall 2004)
20Organization of Programming Languages-Cheng (Fall 2004)
21Organization of Programming Languages-Cheng (Fall 2004)
22Organization of Programming Languages-Cheng (Fall 2004)
switch (variable or an integer expression)
{
case constant: //Java code ;
case constant: //Java code ;
default: //Java code ;
}
23Organization of Programming Languages-Cheng (Fall 2004)
public class SwitchCaseExample2
{
public static void main(String args[])
{
int i=2;
switch(i)
{
case 1:
System.out.println("Case1 ");
case 2:
System.out.println("Case2 ");
case 3:
System.out.println("Case3 ");
case 4:
System.out.println("Case4 ");
default:
System.out.println("Default "); } } }
25Organization of Programming Languages-Cheng (Fall 2004)
public class SwitchCaseExample2
{ public static void main(String args[])
{ int i=2;
switch(i) {
case 1:
System.out.println("Case1 ");
break;
case 2:
System.out.println("Case2 ");
break;
case 3:
System.out.println("Case3 ");
break;
case 4: System.out.println("Case4 ");
break;
default: System.out.println("Default "); } } }
Output:
Case2
26Organization of Programming Languages-Cheng (Fall 2004)
27Organization of Programming Languages-Cheng (Fall 2004)
28Organization of Programming Languages-Cheng (Fall 2004)
29Organization of Programming Languages-Cheng (Fall 2004)
30Organization of Programming Languages-Cheng (Fall 2004)
31Organization of Programming Languages-Cheng (Fall 2004)
32Organization of Programming Languages-Cheng (Fall 2004)
33Organization of Programming Languages-Cheng (Fall 2004)
34Organization of Programming Languages-Cheng (Fall 2004)
35Organization of Programming Languages-Cheng (Fall 2004)
36Organization of Programming Languages-Cheng (Fall 2004)
37Organization of Programming Languages-Cheng (Fall 2004)
38Organization of Programming Languages-Cheng (Fall 2004)
39Organization of Programming Languages-Cheng (Fall 2004)
40Organization of Programming Languages-Cheng (Fall 2004)
41Organization of Programming Languages-Cheng (Fall 2004)
42Organization of Programming Languages-Cheng (Fall 2004)
43Organization of Programming Languages-Cheng (Fall 2004)
44Organization of Programming Languages-Cheng (Fall 2004)
45Organization of Programming Languages-Cheng (Fall 2004)
46Organization of Programming Languages-Cheng (Fall 2004)
static Methods, static Fields and Class Math
To execute any method, that method should be called/invoked.
There are two ways to invoke any method:
By using the object of a class of that mehod
By using the class-name of that method making/declaring the method
static.
It’s common for classes to contain convenient static methods to perform
common tasks.
Static method is invoked as follows:
ClassName.methodName (arguments)
We use various Math class methods here to present the concept of static
methods. Class Math provides a collection of methods that enable you to
perform common mathematical calculations. For example, you can
calculate the square root of 900.0 with the static method call
Math.sqrt(900.0)
47Organization of Programming Languages-Cheng (Fall 2004)
Note Class Math is part of the java.lang package, which is
implicitly imported by the compiler, so it’s not necessary to import
class Math to use its methods.
Math Class Constants PI and E
Class Math declares two fields that represent commonly used
mathematical constants — Math.PI and Math.E.
Math.PI (3.141592653589793) is the ratio of a circle’s
circumference to its diameter.
Math.E (2.718281828459045) is the base value for natural
logarithms (calculated with static Math method log).
These fields are declared in class Math with the modifiers
public, final and static.
48Organization of Programming Languages-Cheng (Fall 2004)
49Organization of Programming Languages-Cheng (Fall 2004)
Why Is Method main Declared static?
When you execute the Java Virtual Machine (JVM) with the java
command, the JVM attempts to invoke the main method of the class.
when no objects of the class have been created.
Declaring main as static allows the JVM to invoke main without
creating an instance of the class.
When you execute your program, specify its class name as an
argument to the command java, as in
java ClassName
The JVM loads the class specified by ClassName and uses that class
name to invoke method main.
In the preceding command, ClassName is a command-line
argument to the JVM
50Organization of Programming Languages-Cheng (Fall 2004)
The keyword static enables the main method (another static
method) to call maximum without qualifying the method name
with the class name MaximumFinder
static methods in the same class can call each other directly.
Any other class that uses maximum must fully qualify the
method name with the class name.
51Organization of Programming Languages-Cheng (Fall 2004)
Declaring Methods with
Multiple Parameters
import java.util.Scanner;
public class MaximumFinder {
public static void main (String[] mohseen) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 floating-point values separated by spaces: " );
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double result = maximum(number1, number2, number3);
System.out.println("Maximum is: " + result);
}
public static double maximum(double x, double y, double z) {
double maximumValue = x;
if (y > maximumValue)
maximumValue = y;
if (z > maximumValue)
maximumValue = z;
return maximumValue;
}
}
52Organization of Programming Languages-Cheng (Fall 2004)
Using max() method of built-in Math class
import java.util.Scanner;
public class MaximumFinder1 {
public static void main (String[] mohseen) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three floating-point values separated by spaces: " );
double n1= input.nextDouble();
double n2= input.nextDouble();
double n3= input.nextDouble();
double result = Math.max(n1, Math.max(n2,n3));
System.out.println("Maximum is: " + result);
}
}
53Organization of Programming Languages-Cheng (Fall 2004)
Scope of Declarations
The basic scope rules are as follows:
1.The scope of a parameter declaration is the body of the method in which
the declaration appears.
2.The scope of a local-variable declaration is from the point at which the
declaration appears to the end of that block.
3.The scope of a local-variable declaration that appears in the initialization
section of a for statement’s header is the body of the for statement and
the other expressions in the header.
4.A method or field’s scope is the entire body of the class. This enables
non-static methods of a class to use the fields and other methods of the
class.
shadowing If a local variable or parameter in a method has the same
name as a field/variale of the class, the field/variable is “hidden” until the
block terminates execution—this is called shadowing.
54Organization of Programming Languages-Cheng (Fall 2004)
public class Scope {
private static int x = 1;
public static void main(String[] mohseen) {
int x = 5;
System.out.println("local x in main is " + x);
useLocalVariable();
useField();
useLocalVariable();
useField();
System.out.println("\nlocal x in main is " + x);
}
public static void useLocalVariable() {
int x = 25;
System.out.println(“\nlocal x on entering method useLocalVariable is" + x);
++x;
System.out.println("local x before exiting method useLocalVariable is" +x);
}
public static void useField() {
System.out.println("\nfield x on entering method useField is " + x);
x *= 10;
System.out.println("field x before exiting method useField is " + x);
}
}
55Organization of Programming Languages-Cheng (Fall 2004)
public static void useField() {
System.out.println("\nfield x on entering method useField is " + x);
x *= 10;
System.out.println("field x before exiting method useField is " + x);
}
}
56Organization of Programming Languages-Cheng (Fall 2004)
57Organization of Programming Languages-Cheng (Fall 2004)
Method Overloading
Methods of the same name can be declared in the same class, as long as they
have different sets of parameters (determined by the number, types and order
of the parameters) is called method overloading.
When an overloaded method is called, the compiler selects the appropriate
method by examining the number, types and order of the arguments in the call.
Method overloading is commonly used to create several methods with the same
name that perform the same or similar tasks, but on different types or different
numbers of arguments.
For example, Math methods abs, min and max are overloaded with four
versions each:
1. One with two double parameters.
2. One with two float parameters.
3. One with two int parameters.
4. One with two long parameters.
58Organization of Programming Languages-Cheng (Fall 2004)
public class MethodOverload {
public static void main(String[] args) {
System.out.println("Square of integer 7 is " + square(7));
System.out.println("Square of double 7.5 is " + square(7.5));
}
public static int square(int intVal) {
System.out.println("\nCalled square with int argument: " + intVal);
return intVal * intVal;
}
public static double square(double doubleVal) {
System.out.println("Called square with double argument:”+ doubleVal);
return doubleVal * doubleVal;
}
}