String myString = "Hello";
length()
returns the number of characters in the
string""
has length 0"b" + 4
produces "b4"
Integer.parseInt(string)
converts string to an intDouble.parseDouble(string)
and Float.parseFloat(string)
work similarlysubstring(start, pastEnd)
returns pastEnd
characters starting from start
positionstring1.equals(string2)
returns true if the characters
in string1 and string2 are identical
String s1 = "Hello"; String s2 = "Hello";
boolean equals1 = s1.equals(s2);
// equals1 is true (s1 and
s2 have same characters)==
to compare stringsboolean equals2 = s1 == s2;
// equals2 is false (s1 and s2
have different addresses)String s3 = s1; boolean equals3 = s1 == s3;
// equals3 is
true (s1 and s3 point to the same string)equalsIgnoreCase
returns true if the strings are equal
ignoring case"HTML".equalsIgnoreCase("html")
// returns truetoLowerCase()
and toUpperCase()
convert all letter characters in the string to small or capital letters"HTML".equals("html".toUpperCase())
// returns truecharAt
(position)
returns the character at
the specified position in the string"Java".charAt(0)
// returns 'J'
type
[]
variablename = new type[number]
;double[] x = new double[10];
// x is an array of 10
double-precision numbersint y[] = new int[n];
// y is an array of n
integers; [] can come after the type or variable nameint[] primes = { 2, 3, 5, 7, 11 };
// an array of prime
numbersint i = x.length;
// i is 10variablename
[index]
double z = x[3];
// set z equal to the double in position 3z = x[10];
arraySize
to store the number
of array positions actually containing data.while (i = Keyboard.readInt() > 0) { y[arraySize++] = i; }
array2 = array1;
// array1 and array2 are references to the
same arrayarray1[5] = 32;
// array2[5] is also 32clone()
method and cast to array of type.double[] prices = (double[]) data.clone();
// prices is a
copy of dataint[] b = new int[2 * a.length]; for (int i = 0; i < a.length; i++)
b[i] = a[i];
add
(i, object)
adds object in position i
in the array.remove
(i)
removes the object in position
i.(type) get(n)
returns the nth object in the ArrayList
and casts it to an object type.size
()
returns the number of elements in
the ArrayList.A vector is a list with limited capacity that can be increased (doubled by default). Methods include:
addElement
(element)
adds element to the
end of the vector.removeElement
(element)
removes the first
occurrence of the element.firstElement
()
and lastElement
()
return the first and last element.isEmpty
()
returns true if the vector
contains no elements, false otherwise.contains
(searchKey)
returns true if the
search key is in the vector, false if not.indexOf
(element)
returns the first
location of the argument.size
()
returns the number of elements in
the vector.capacity
()
returns the number of elements
that can be stored.trimToSize
()
reduces the capacity of the
vector to its current size.The Math class contains no objects; all its methods are class methods.
Most of the following methods take a double
parameter and return
a double
.
double sqrt(double x)
// square root, e.g. sqrt(4.0)
returns 2.0double pow(double x, int y)
// x to the power y, e.g.
pow(2, 3) returns 8.0double sin(double x)
// sine of x, e.g. sin(0) returns
0 (cos is cosine, tan
is tangent, asin is arcsine, etc.)double toRadians(double x)
// converts degrees to
radians, e.g. toRadians(180) returns 3.14159...double toDegrees(double x)
// converts radians to
degrees, e.g. toDegrees(Math.PI) returns 180double exp(double x)
// e to the power x, e.g. exp(1)
returns 2.7182818...double log(double x)
// logarithm of x, e.g. log(1)
returns 0double round(double x)
// rounds to the nearest
integer (ceil rounds up, floor rounds down)double abs(double x)
// absolute value of x; same as
(x < 0 ? -x : x)double x = Math.random();
// x is double precision number
where 0 <= x < 1int i = (int)Math.random() * n
// i is an integer from 0 to
n - 1int i = (int)Math.random() * n + 1;
// i is an integer from
1 to ntoString()
.To select a choice from the menu, type a number and press Enter.
java myprogram < input.txt
// redirect input
of a program to get input from a text filejava myprogram > output.txt
// redirect
output of a program to create a text file (overwrites output
.txt
if it exists)java myprogram >> output.txt
// append
output of a program to the end of an existing text filejava program1 | java program2
// pipe the output of
program1 to be used as the input of program2To run your program on more than one data set, create a DOS batch file or UNIX shell script:
input1.txt
,
input2.txt
and input3.txt
.java myprogram < input1.txt
java myprogram < input2.txt
java myprogram < input3.txt
myprogram.bat
chmod +x myprogram.bat
./myprogram.bat