You are on page 1of 13

Visit: ProgrammingKid.

com

Java Programming Cheat Sheet


Hello, World.

Editing, compiling, and executing.

Built-in data types.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Declaration and assignment statements.

Integers.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Floating point numbers.

Booleans.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Comparison operators.

Math library.

Command-line arguments.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com int a = Integer.parseInt(args[0]); double b = Double.parseDouble(args[1]); String c = args[2]; // read int from command-line // read double from command-line // read String from command-line

Type conversion.

If and if-else statements.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

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--; }

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

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.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Arrays.
// declare and int[] a double[] b String[] suits compile-time initialize an array = { 3, 1, 4, 1, 5, 9 }; = { 3.0, 1.0, 4.0, 1.0, 5.0, 9.0 }; = { "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 double[][] { .02, { .02, { .02, { .92, { .47, }; and compile-time initialize a 5-by-5 array of doubles p = { .92, .02, .02, .02 }, .02, .32, .32, .32 }, .02, .02, .92, .02 }, .02, .02, .02, .02 }, .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; } }

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

The String data type.

String s = "Hello"; String t = "World"; String u = s + " " + t; int length = s.length(); char c = t.charAt(2); s.equals("Hello");

// // // //

"Hello World" 5 (length of string) 'r' (character indices start at 0) compare string for equality

Our standard output library.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Our standard input library.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Our standard drawing library.

A few extra commands


StdDraw.mouseX() StdDraw.text() StdDraw.mouseY() StdDraw.picture() StdDraw.mousePressed() StdDraw.setCanvasSize()

Our standard audio library.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Redirection and piping.

Constructors.

Instance variables.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

Visit: ProgrammingKid.com

Instance methods.

Classes.

Courtesy : Robert Sedgewick and Kevin Wayne. Copyright 2007. All rights reserved

You might also like