% java MyApp 2 twoHere "2" and "two" are called the command-line arguments, which the application MyApp must process. The command-line arguments passed to a Java application differ in number and in type than those passed to a C or C++ program: in Java, the application receives only one parameter String [] args which is declared as the argument of main (see example below). This is an array of strings that contains the arguments. The number of command-line arguments is obtained from the array's length attribute: int numberOfArgs = args.length. In Java, since you always know the name of the application ( it's the name of the class in which the main method is defined), this class name is not passed with the command line arguments. Thus args = ("2", "two") in the example.
public class Echo {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
int firstArg = Integer.parseInt(args[0]);Note that parseInt throws a NumberFormatException if the format of args[0] isn't valid. All of the Number classes--Integer, Float, Double, and so on--have parseXXX methods that convert a String representing a number to an object of their type.
java Echo "2 two"then it is regarded that the command-line has only one argument.
<APPLET CODE=MyAppletSubclass.class WIDTH=anInt HEIGHT=anInt >
<PARAM NAME=param1Name VALUE=Value1 >
<PARAM NAME=param2Name VALUE=Value2 >
... html code for non-Java browsers ...
</APPLET >
String val1 = getParameter("param1Name");
As usual (See Command-line Arguments), if the val1
is a numeric value, you must convert it. E.g.
int v1 = Integer.parseInt(var1);
public void Paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
...
}
Examples of attributes: (1) pen style attribute determines how lines are drawn. (2) fill style determine how the interior of a shape is filled in. (3) compositing style attributes is used when rendered objects overlap existing objects. (4) transform attribute is used to convert the rendered object from user space to device-space coordinates.
setStroke setPaint setComposite setTransform setClip
setFont setRenderingHints
When you set an attribute, you pass in the appropriate attribute object.
For example, to change the paint attribute to a blue-green gradient
fill, you would construct a GradientPaint object and then call
setPaint.
gp = new GradientPaint(0f,0f,blue,0f,30f,green); g2.setPaint(gp);Graphics2D holds references to its attribute objects -- they are not cloned. If you alter an attribute object that is part of the Graphics2D context, you need to call the appropriate set method to notify the context. Modifying an attribute object during rendering causes unpredictable behavior.
If you know about pointers, then the answer is this: whenever we pass an object to a Java method, we are passing the value of the pointer (not the pointer itself). In this sense, we are passing the object parameter "by value" (but it is not "value" in the naive sense).
To illustrate these concepts, suppose we write a simple wrapper class called MyInt, that is similar to Integer:
public class MyInt{
int val=0;
//constructor:
public MyInt(int k) { val = k; }
//access methods:
public void setVal(int n){ val=n; }
public int getVal(){ return val; }
//string method
public String toString(){ return Integer.toString(val);}
}
Consider the following attempt to assign to an object parameter inside a method.
we present two methods -- one method fails, the other succeeds.
public class RefDemo {
static public void setIntFail(int n, MyInt N){
System.out.println("n = " + n + ", N = " + N);
N = new MyInt(100);
}
static public void setIntSucc(int n, MyInt N){
System.out.println("n = " + n + ", N = " + N);
N.setVal(100);
}
public static void main(String[] args){
MyInt NN = new MyInt(0);
setIntFail(1,NN);
System.out.println("NN = " + NN + " (Failure: NN has not changed to 100)");
setIntSucc(2,NN);
System.out.println("NN = " + NN + " (Success: NN has changed to 100)");
}
}
Here is the output:
> javac MyInt.java RefDemo.java > java RefDemo n = 1, N = 0 NN = 0 (Failure: NN has not changed to 100) n = 2, N = 0 NN = 100 (Success: NN has changed to 100)NOTE: we couldn't use Integer for our demo becasue Integer class is "final".
public class Outer {
static int s;
int n;
public static class InnerStatic {
int m;
void f(){
System.out.println("s = " + s); // This is OK
m = n; // ERROR! Cannot access
// non-static member of outer class.
}
}
public class InnerNonStatic {
int m;
void f(){
System.out.println("s = " + s); // This is OK
m = n; // This is OK too
}
}
}
From InnerNonStatic, you can refer to your own instance by the
usual \verb+this+, but you can also refer to the outer
instance using \verb+Outer.this+. Both of these
references do not make sense for InnerStatic.
Why Inner Classes? This is one way to organize your programs
so that you can conveniently refer to certain methods/fields
without much fuss (and without making these methods/fields public).
This is used frequently in Java2D, e.g.,
in the the Point2D class.
import java.util.Random; // or: import java.util.*;Next, you need to set up the random number generator:
Random generator = new Random(1000); // 1000 is the seed.You may omit the seed, which is an optional argument. Now, we can use the random number generator to generate random int values, using one of the following calls:
int randInt = generator.nextInt(); int randInt = generator.nextInt(100); // generate value between 0--99.We can similarly generate random float values:
double randflot = generator.nextDouble(); // generate uniform value between 0 and 1.0 double randflot = generator.nextGaussian(); // generate Gaussian distributed value between 0 and 1.0Note that
nextDouble() and
nextGaussian() do not take any arguments.
We will discuss the three generic programming mechanisms:
(a) generic interfaces (e.g., java.util.List
Another useful technique is the notion of wildcards:
You can define a method with "wildcard type":
An annoying quirk of Java is its treatment of generic arrays.
Apparently, there is no way to initialize a generic array
in a generic class! You might try:
import java.util.ArrayList;
public class MyQueue
To use it, you can extend this generic class, or declare objects
by instantiating the generic type:
MyQueue
Perhaps the most important mistake to avoid is that
if E and T are two distinct types, then MyQueue<T> and
MyQueue<E> are not related in any way by any subclass relationship.
(You might expect that if T is a subclass of E, then
MyQueue<T> should be a subclass of MyQueue<E>).
static <T> void printMyQueue(MyQueue<T> qq) {
for (T e: c){
System.out.println(e);
}
}
void printMyQueue(myQueue<?> qq) {
for (Object e: c){
System.out.println(e);
}
}
class GenericClass
The compiler warning can be suppressed.
In general, you cannot create instances of a generic type:
T myObj = new T(); // illegal, as T is a type variable
There is a lot of discussion of this issue, especially of
"generic array creation" in the web. The following nice solution
uses reflection, and is taken from
http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation:
import java.lang.reflect.Array;
public class GenericSet
Note that the GenericSet constructor requires a Class
Go to Top