Java Cheatsheet
-
Upload
manuel-martinez -
Category
Documents
-
view
176 -
download
2
Transcript of Java Cheatsheet
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Java Programming Cheat Sheet
Hello, World.
Editing, compiling, and executing.
Built-in data types.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Declaration and assignment statements.
Integers.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Floating point numbers.
Booleans.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Comparison operators.
Math library.
Command-line arguments.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
int a = Integer.parseInt(args[0]); // read int from command-line
double b = Double.parseDouble(args[1]); // read double from command-line
String c = args[2]; // read String from command-line
Type conversion.
If and if-else statements.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Nested if-else statement.
While and for loops.
Nested for loops.
If-else nested inside a while loop.
// how many fair bets until you go broke?
int bets = 0;
while (stake > 0) {
bets++;
if (Math.random() < 0.5) stake++;
else stake--;
}
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Deeper nesting.
// print out Pythagorean triples (i, j, k) such that i^2 + j^2 = k^2
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j++) {
for (int k = j; k <= N; k++) {
if (i*i + j*j == k*k) {
System.out.println("(" + i + ", " + j + ", " + k + ")");
}
}
}
}
Break statement.
Do-while loop.
Switch statement.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Arrays.
// declare and compile-time initialize an array
int[] a = { 3, 1, 4, 1, 5, 9 };
double[] b = { 3.0, 1.0, 4.0, 1.0, 5.0, 9.0 };
String[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
// declare and run-time initialize an array of integers
int N = 100;
int[] c = new int[N];
for (int i = 0; i < N; i++) {
c[i] = i;
}
double[] d = new double[N];
for (int i = 0; i < N; i++) {
d[i] = i;
}
// compute the average of the elements in the array d[]
double sum = 0.0;
for (int i = 0; i < d.length; i++) {
sum = sum + d[i];
}
double average = sum / d.length;
Two-dimensional arrays.
// declare and compile-time initialize a 5-by-5 array of doubles
double[][] p = {
{ .02, .92, .02, .02, .02 },
{ .02, .02, .32, .32, .32 },
{ .02, .02, .02, .92, .02 },
{ .92, .02, .02, .02, .02 },
{ .47, .02, .47, .02, .02 },
};
// declare and run-time initialize an M-by-N array of doubles
double[][] b = new double[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
b[i][j] = 0.0;
}
}
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
The String data type.
String s = "Hello";
String t = "World";
String u = s + " " + t; // "Hello World"
int length = s.length(); // 5 (length of string)
char c = t.charAt(2); // 'r' (character indices start at 0)
s.equals("Hello"); // compare string for equality
Our standard output library.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Our standard input library.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Our standard drawing library.
A few extra commands
StdDraw.mouseX() StdDraw.mouseY() StdDraw.mousePressed()
StdDraw.text() StdDraw.picture() StdDraw.setCanvasSize()
Our standard audio library.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Redirection and piping.
Constructors.
Instance variables.
Visit: ProgrammingKid.com
Courtesy : Robert Sedgewick and Kevin Wayne. Copyright © 2007. All rights reserved
Instance methods.
Classes.