Professional Documents
Culture Documents
Developing A Java Program Involves: Tool Function
Developing A Java Program Involves: Tool Function
1. 1. 2. JDK Utilities
Tool Function
javac The Java compiler. Converts Java source code into bytecodes.
java The Java interpreter. Executes Java application bytecodes directly from class files.
appletviewe
A Java interpreter that executes Java applet classes hosted by HTML files.
r
javadoc Creates HTML documentation based on Java source code and the comments it contains.
rmic Creates class files that support Remote Method Invocation (RMI).
rmiregistry Registry used to gain access to RMI objects on a specific machine.
rmid Activation system daemon for RMI object registration and activation.
Special program used to convert between standard Latin-1 Unicode characters and other
native2ascii
international encoding schemes.
Java Archive (JAR) file generator. JAR files allow multiple Java classes and resources to
jar
distributed in one compressed file.
keytool Used for security key generation and management.
Implements digital signing of JAR and class files. Allows applets to be certified by trusted
jarsigner
authorities.
policytool Allows user-installation-level security policy configuration.
1. 1. 3. See the details of the compilation: use the verbose option
You can use this information to determine which parts of a program to optim
Option Description
-cp path or -classpath
Specifies the Java classpath.
path
-d directory Sets the destination directory to store class files.
-g Generates debugging information.
-g:none Instructs the javac tool not to generate any debugging information.
-help Prints information about the options of the javac tool.
-Xlint Displays warnings against the use of deprecated classes and methods.
-Xlint:-name Disables deprecation warnings for a specified class, interface, or method.
Displays a warning when you specify a directory to compile but the directory
-Xlint:path
exist.
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines, vars, source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-cp <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-client Runs the client VM
-serve Runs the server VM, which is optimized for server syst
-classpath adirectories A list of directories or JAR or Zip archive and archiv
-cp <search path> Same as -classpath
-D name=value Sets a system property
-verbose Enables verbose output
-version Displays the JRE version number, then stops
-showversion Displays the JRE version number, then continues
-? or -help Lists the standard options
-X Lists nonstandard options
-ea or -enableassertions Enables the assert command
-ea classes or packages Enables assertions for the specified classes or packag
-esa or -enablesystemassertions Enables system assertions
-dsa or -disablesystemassertions Disables system assertions
1. options refers to the command line options that you can pass to the javadoc tool.
2. packagenames refers to multiple packages, separated by spaces, that javadoc should use to ge
3. packagesnames java.lang java.lang.reflect java.awt
4. sourcefilenames refers to the source files for which javadoc needs to generate documentation.
5. -subpackages: Generates documentation from the source files in specified packages and recurs
6. @argfiles: Refers to a file that contains Javadoc options.
The jar tool creates a JAR archive file by combining multiple files of a Java application.
The jar tool compresses content based on the ZIP and ZLIB compression formats.
You can also use the jar tool to extract the content of a JAR file.
The syntax to use the jar tool is:
Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files
Optio
Description
n
c Creates a new archive.
t Lists the contents of an archive.
x Extracts contents from an archive.
u Updates an existing archive.
v Displays output at the command prompt while the jar tool performs an operation.
f Specifies the name of the archive file.
m Includes a specified manifest file in the archive.
o Instructs the jar tool not to compress content.
Instructs the jar tool not to create a manifest file for an archive. The jar tool, by default, creates
M
archive.
C Specifies the directories to include in the archive.
1. 1. 11. The javap Tool
The javap command displays information about the methods, variables, and parameters present in a c
The output of the javap tool depends on the options used. If you do not specify any options while using
package, protected, and public fields and methods of the classes passed to the tool.
The syntax to use the javap tool is:
javap [options] class. . .
Option Description
-help Displays a help message for the javap tool.
public Shows only the public classes and members.
protected Shows only the public and protected classes and members.
private Shows all classes and members.
package Shows the package, protected, and public classes and members. This is the default op
-classpath
Specifies the class path that javap should use to locate the class file to disassemble.
path
Option Description
-b This command ensures backward compatibility with earlier versions of javap
-bootclasspath This command, followed by a path, specifies the path from which to load th
.
-c This command prints the JVM instructions for the execution of each method.
-classpath This command, followed by a user-specified class path, overrides the syste
-extdirs This command, followed by a directory, overrides the location the system s
-help This command prints information about the javap utility.
-Jflag This command passes the specified flag directly to the runtime system.
-l This command displays line and local variables.
-package This command shows only package, protected, and public classes and members
-private This command shows information about all classes and members.
-protected This command displays information about protected and public classes and m
-public This command shows information only about public classes and members.
-s This command prints internal type signatures.
-verbose This command prints additional information for each method including stack
You can use the native2ascii tool to convert files with native encoded characters to a file with Unicode
characters.
The syntax to use the native2ascii tool is:
native2ascii [options] [inputfile [outputfile]]
1. options: Refers to various options that you can use to pass information or configure the native2a
2. inputfile: Refers to the file that contains native encoded characters.
3. outputfile: Represents the file that will contain the converted text.
The jconsoletool is a JMX-compliant graphical tool for monitoring a Java virtual machine.
The jconsoletool can monitor both local and remote JVMs.
The jconsoletool can monitor and manage an application.
The syntax to use jconsoletool is:
jconsole [ options ] [ connection ... ]
Option Description
-interval=n Allows you to set the update interval to n seconds.
-notile Specifies not to tile windows initially.
-pluginpath
Specifies a list of the directories or JAR files that are searched for JConsole plug-in
plugins
-help Describes the jconsoletool option.
1. 1. 16. Set the memory available to the JVM
java -mx20m myApp // for max
java -ms20m myApp // for start
java -Xms32m -Xmx200m -XX:MaxPermSize=128m com.SomeWonderfulApp
Java has eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
These can be put in four groups:
1. Integers includes byte, short, int, and long
2. Floating-point numbers includes float and double
3. Characters includes char, like letters and numbers.
4. Boolean includes boolean representing true/false values.
byte
The smallest integer type
a range from -128 to 127.
useful when working with a stream of data from a network or file.
Byte variables are declared by use of the byte keyword.
byte b, c;
int
The most commonly used integer type
a signed 32-bit type
Ranging from -2,147,483,648 to 2,147,483,647
used to control loops and to index arrays.
the most efficient type
long
a signed 64-bit type
Type Explanation
int A 32-bit (4-byte) integer value
short A 16-bit (2-byte) integer value
long A 64-bit (8-byte) integer value
byte An 8-bit (1-byte) integer value
float A 32-bit (4-byte) floating-point value
double A 64-bit (8-byte) floating-point value
char A 16-bit character using the Unicode encoding scheme
boolean A true or false value
public static void main(String[] args) {
System.out.println("bool = " + bool);
System.out.println("by = " + by);
System.out.println("ch = " + ch);
System.out.println("d = " + d);
System.out.println("f = " + f);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("sh = " + sh);
System.out.println("str = " + str);
}
}
. 1. 4. Literals
public class MainClass {
char c = 0xffff; // max char hex value
byte b = 0x7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0x2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long.
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix (but can be confusing)
long n3 = 200;
// ! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power
}
public static void main(String[] args) {
int big = 0x7fffffff; // max int value
System.out.println("big = " + big);
int bigger = big * 4;
System.out.println("bigger = " + bigger);
}
}
big = 2147483647
bigger = -4
2. 1. 6. Wrapping a Primitive Type in a Wrapper Object: boolean, byte, char, short, int, long, float, doub
public class Main {
public static void main(String[] argv) throws Exception {
Boolean refBoolean = new Boolean(true);
boolean bool = refBoolean.booleanValue();
Byte refByte = new Byte((byte) 123);
byte b = refByte.byteValue();
Character refChar = new Character('x');
char c = refChar.charValue();
Short refShort = new Short((short) 123);
short s = refShort.shortValue();
Integer refInt = new Integer(123);
int i = refInt.intValue();
Long refLong = new Long(123L);
long l = refLong.longValue();
Float refFloat = new Float(12.3F);
float f = refFloat.floatValue();
Double refDouble = new Double(12.3D);
double d = refDouble.doubleValue();
}
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
public class Main {
/**
* @param wrapper
* a primitive wrapper type
* @return primitive type the passed in wrapper type corresponds to
*/
public static Class getPrimitive(Class wrapper) {
Class primitive;
if (Integer.class == wrapper) {
primitive = int.class;
} else if (Long.class == wrapper) {
primitive = long.class;
} else if (Double.class == wrapper) {
primitive = double.class;
} else if (Boolean.class == wrapper) {
primitive = boolean.class;
} else if (Short.class == wrapper) {
primitive = short.class;
} else if (Float.class == wrapper) {
primitive = float.class;
} else if (Byte.class == wrapper) {
primitive = byte.class;
} else if (Character.class == wrapper) {
primitive = char.class;
} else {
throw new IllegalArgumentException("The class is not a primitive wrapper type: " + w
}
return primitive;
}
}
Precedence Operator Description Association
1 ++,-- Postincrement, Postdecrement R -> L
2 ++,-- Preincrement, Predecrement R -> L
+,- Unary plus, unary minus R -> L
~ Bitwise compliment R -> L
! Boolean NOT R -> L
3 new Create object R -> L
(type) Type cast R -> L
4 *,/,% Multiplication, division, remainder L -> R
5 +,- Addition, subtraction L -> R
+ String concatenation L -> R
6 <<, >>, >>> Left shift, right shift, unsigned right shift L -> R
7 <, <=, >, >= L -> R
instanceof Type comparison L -> R
8 ==, != Value equality and inequality L -> R
==, != Reference equality and inequality L -> R
9 & Boolean AND L -> R
& Bitwise AND L -> R
10 ^ Boolean XOR L -> R
^ Bitwise XOR L -> R
11 | Boolean OR L -> R
| Bitwise OR L -> R
12 && Conditional AND L -> R
13 || Conditional OR L -> R
14 ?: Conditional Ternary Operator L -> R
15 =,+=,-=, Assignment Operators R -> L
*=,/ =,%=,
&=,^=, |=,
<<=, >> =,
>>>=
3. 1. 2. Operator Precedence
Operators with a higher precedence are executed before those of a lower precedence.
Operators on the same line have the same precedence:
Operator Precedence Group Associativity Operator Precedence
(), [], postfix ++, postfix -- left Highest
unary +, unary -, prefix ++, prefix --, ~, ! right
(type), new left
*, /, % left
+, - left
<<, >>, >>> left
< ,<= , >, >=, instanceof
==, !=
& left
^ left
| left
&& left
|| left
?: left
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=,
right lowest
^=
3. 1. 3. The op= Operators
public static void main(String[] arg) {
int count = 1;
count += 5;
System.out.println(count);
count = count + 5;
System.out.println(count);
}
6
11
The complete set of op= operators:
1. +=
2. -=
3. *=
4. /=
5. %=
6. <<=
7. >>=
8. >>>=
9. &=
10. |=
11. ^=
logical_expression ? expression1 : expression2
public class MainClass {
public static void main(String[] args) {
int v = 1;
System.out.println(v == 1 ? "A" : "B");
v++;
System.out.println(v == 1 ? "A" : "B");
}
}
A
B
3. 1. 5. Tests all the operators on all the primitive data types to show
ones are accepted by the Java compiler
public class MainClass {
public static void main(String[] a){
boolTest(true, false);
charTest('x', 'y');
byteTest((byte)0, (byte)1);
shortTest((short)0, (short)1);
intTest(1, 2);
longTest(11L, 22L);
floatTest(1.1F, 2.2F);
doubleTest(1.1, 2.2);
}
// To accept the results of a boolean test:
static void f(boolean b) {
System.out.println("f:"+b);
}
static void boolTest(boolean x, boolean y) {
// Arithmetic operators:
//! x = x * y;
//! x = x / y;
//! x = x % y;
//! x = x + y;
//! x = x - y;
//! x++;
//! x--;
//! x = +y;
//! x = -y;
// Relational and logical:
//! f(x > y);
//! f(x >= y);
//! f(x < y);
//! f(x <= y);
f(x == y);
f(x != y);
f(!y);
x = x && y;
x = x || y;
// Bitwise operators:
//! x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment:
//! x += y;
//! x -= y;
//! x *= y;
//! x /= y;
//! x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! char c = (char)x;
//! byte B = (byte)x;
//! short s = (short)x;
//! int i = (int)x;
//! long l = (long)x;
//! float f = (float)x;
//! double d = (double)x;
}
static void charTest(char x, char y) {
// Arithmetic operators:
x = (char)(x * y);
x = (char)(x / y);
x = (char)(x % y);
x = (char)(x + y);
x = (char)(x - y);
x++;
x--;
x = (char)+y;
x = (char)-y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x= (char)~y;
x = (char)(x & y);
x = (char)(x | y);
x = (char)(x ^ y);
x = (char)(x << 1);
x = (char)(x >> 1);
x = (char)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean b = (boolean)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
static void byteTest(byte x, byte y) {
// Arithmetic operators:
x = (byte)(x* y);
x = (byte)(x / y);
x = (byte)(x % y);
x = (byte)(x + y);
x = (byte)(x - y);
x++;
x--;
x = (byte)+ y;
x = (byte)- y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x = (byte)~y;
x = (byte)(x & y);
x = (byte)(x | y);
x = (byte)(x ^ y);
x = (byte)(x << 1);
x = (byte)(x >> 1);
x = (byte)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
static void shortTest(short x, short y) {
// Arithmetic operators:
x = (short)(x * y);
x = (short)(x / y);
x = (short)(x % y);
x = (short)(x + y);
x = (short)(x - y);
x++;
x--;
x = (short)+y;
x = (short)-y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x = (short)~y;
x = (short)(x & y);
x = (short)(x | y);
x = (short)(x ^ y);
x = (short)(x << 1);
x = (short)(x >> 1);
x = (short)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
static void intTest(int x, int y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
static void longTest(long x, long y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
float f = (float)x;
double d = (double)x;
}
static void floatTest(float x, float y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
double d = (double)x;
}
static void doubleTest(double x, double y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting:
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
}
}