0% found this document useful (0 votes)
22 views2 pages

Java Syntax Cheat Sheet for CISC 121

This document provides a cheat sheet on Java syntax and concepts, including variable declarations, primitive types, strings, arrays, loops, classes, and math functions. It covers Java fundamentals like variable names, operators, conditionals, and input/output, as well as string, character, and array manipulation. Useful math formulas are also included for summations.

Uploaded by

ErwinVides
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Java Syntax Cheat Sheet for CISC 121

This document provides a cheat sheet on Java syntax and concepts, including variable declarations, primitive types, strings, arrays, loops, classes, and math functions. It covers Java fundamentals like variable names, operators, conditionals, and input/output, as well as string, character, and array manipulation. Useful math formulas are also included for summations.

Uploaded by

ErwinVides
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Cheat Sheet

reminders about Java syntax and other details for CISC 121
Variable Names: studentName middle_initial student5 remember that upper & lower case matters! Primitive Types: int double char boolean Declarations & Assignments: int x; x = 14; double d = 15.2; Comparisons (primitive types only): < > <= >= == != (Note comparison for equality is a double equal) Strings: String s = "abc" + "def" + 13; // s gets "abcdef13" int len = [Link](); // len gets 8 char c = [Link](2); // c gets 'c' if ([Link](t)) // true if t exactly the same as s if ([Link](t)) // same as above but ignores case int x = [Link](t); // 0 if they're equal // positive if s is greater // negative if t is greater String s1 = "abcdefg"; String s2 = [Link](2,5); // s2 gets "cde" String s3 = [Link](3); // s3 gets "defg" int pos = [Link]('c'); // pos gets 2 String s = " ab c "; String t = [Link](); // t gets "ab c" // String <-> int conversions int x = [Link](s); // if s = "123", x gets 123 String s = [Link](x); // if x = 123, s gets "123" Characters: // if ch is lower case, capitalize if ([Link](ch)) ch = [Link](ch); // returns true if ch is a decimal digit // (i.e. '0','1',...,'9') if ([Link](ch))... // translates ch into the equivalent int // (0 to 9) int i = [Link](ch, 10); Arrays: // create array of 10 doubles double arr[] = new double[10]; number of elements in arr: [Link] // create 2-D array of ints // with 3 rows, 4 columns int table[][] = new int[3][4]; Increment/Decrement: x++; // means x = x + 1; x--; // means x = x 1; Extended Assignment: x += 3; // means x = x + 3; x -= 7; // means x = x 7; Output: [Link]("x = " + x + " and y = " + y); // x and y can be any type If Statements: if (a < b) { [Link]("b is bigger"); c = b; } else { [Link]("a is bigger"); c = a; } // end if If else if else if ....: if (ch >= 'A' && ch <= 'Z') { [Link]("upper case"); } else if (ch >= 'a' && ch <= 'z') { [Link]("lower case"); } else if (ch >= '0' && ch <= '9') { [Link]("digit"); } else { [Link]("other"); } // end if

While: // computes 1 + 2 + ... + N int i = 0; while (i <= N) { sum += i; // same as sum = sum + i; i++; // same as i = i + 1; } // end while for: // same as preceeding while for (int i = 0; i <= N; i++) { sum += i; } // end for // prints odd numbers from 1 to 100, // in reverse order for (int i = 99; i > 0; i = i 2) [Link](i); Class Structure: public class MyClass { public static void main(String args[]) { .... } // end main } // end class MyClass

Math Class: // returns random double in [0,1) [Link](); // returns the absolute value of x [Link](x); // int & double versions // returns square root of x [Link](x) // returns maximum of x and y [Link](x,y); // int & double versions // returns minimum of x and y [Link](x,y); // int & double versions Useful Summations:

i =
i =1 n

n(n + 1) 2 n(n + 1)(2n + 1) 6

i
i =1

in general, if p 1,

i
i =1

is O(np+1)

b
i =0

1 , where b is any constant b 1

n +1

You might also like