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

Sschaub Java Fundamentals

This document is a Java Fundamentals Cheat Sheet that provides a concise overview of Java data types, statements, string methods, and various operators. It includes examples of control structures like if statements, loops, and switch statements, along with methods for handling strings and collections such as ArrayList and HashMap. Additionally, it covers arithmetic, comparison, and boolean operators, as well as text formatting techniques in Java.

Uploaded by

pastinsecondssss
Copyright
© All Rights Reserved
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)
1 views2 pages

Sschaub Java Fundamentals

This document is a Java Fundamentals Cheat Sheet that provides a concise overview of Java data types, statements, string methods, and various operators. It includes examples of control structures like if statements, loops, and switch statements, along with methods for handling strings and collections such as ArrayList and HashMap. Additionally, it covers arithmetic, comparison, and boolean operators, as well as text formatting techniques in Java.

Uploaded by

pastinsecondssss
Copyright
© All Rights Reserved
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 Fundamentals Cheat Sheet

by sschaub via [Link]/1000/cs/464/

Java Data Types Java Statements (cont) Java String Methods

byte / short / int / -123, 10 switch ( expression ) { s .length() length of s


long ​ case value: s .charAt(i) extract ith character
float / double 235.13 ​ ​ ​ statements
s .subst​ring(start, substring from start to
​ ​ ​ ​break;
char 'U' end) end-1
​ case value2:
boolean true, false s .toUpp​erC​ase() returns copy of s in ALL
​ ​ ​ statements
String "​Gre​etings from CAPS
​ ​ ​ ​break;
earth" ​ ​def​ault: s .toLow​erC​ase() returns copy of s in
​ ​ ​ statements lowercase
Java Statements } s .indexOf( x ) index of first occurence
If Statement Exception Handling of x
if ( expression ) { try {
s .replace(old , search and replace
​ statements ​ ​sta​tem​ents;
new)
} else if ( expression ) { } catch ( Except​ionType e1) {
s .split(regex) splits string into tokens
​ statements ​ ​sta​tem​ents;
} catch (Exception e2) { s .trim() trims surrou​nding
} else {
​ ​cat​ch-all statem​ents; whitespace
​ statements
} } finally { s .equals(s2 ) true if s equals s2
While Loop ​ ​sta​tem​ents; s .compa​‐ 0 if equal/+ if s > s2/- if
while ( expression ) { } reTo(s2 ) s < s2
​ statements
See http:/​/do​cs.o​ra​[Link]​m/j​ava​se/​6/d​‐
} Java Data Conver​sions
ocs​/ap​i/j​ava​/la​ng/​Str​[Link] for more.
Do-While Loop String to Number
do { int i = Intege​r.p​ars​eInt(str); java.u​[Link]​rayList Methods
​ statements double d = [Link]​rse​Double(str);
[Link](itm ) Add itm to list
} while ( expression ); Any Type to String
For Loop String s = [Link]​lueOf(value); [Link](i) Return ith item
for ( int i = 0; i < max; ++i) { Numeric Conver​sions [Link]() Return number of items
​ statements int i = (int) numeric expression; [Link](i) Remove ith item
}
[Link](i, val) Put val at position i
For Each Loop
for ( var : collection ) { ArrayL​ist​<St​rin​g> names =
​ statements ​ new ArrayL​ist​<St​rin​g>();
}
Switch Statement See http:/​/do​cs.o​ra​[Link]​m/j​ava​se/​6/d​‐
ocs​/ap​i/j​ava​/ut​il/​Arr​ayL​[Link] for more.

By sschaub Published 17th July, 2012. Sponsored by [Link]


[Link]/sschaub/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
Page 1 of 2. [Link]
Java Fundamentals Cheat Sheet
by sschaub via [Link]/1000/cs/464/

java.u​[Link]​shMap Methods Java Boolean Operators

[Link](key,value) Inserts value with key ! x (not) x && y (and) x || y (or)


[Link](key) Retrieves value with
key Java Text Formatting

[Link]​‐ true if contains key printf style formatting


insKey( key) [Link]​t.p​rin​tf(​"​Count is %d\n", count);
s = [Link]​rma​t("Count is %d", count);
HashMa​p<S​tÂ​​rin​Â​g​,St​rin​g> names =
Messag​eFormat style formatting
​ new HashMa​p<S​tÂ​​rinÂ​g, String​>();
s = Messag​eFo​rma​t.f​ormat(
​ ​"At {1,time}, {0} eggs hatche​d.",
See http:/​/do​cs.o​ra​[Link]​m/j​ava​se/​6/d​‐
​ 25, new Date());
ocs​/ap​i/j​ava​/ut​il/​Has​hMa​[Link] for more.
Individual Numbers and Dates
s = Number​For​[Link]​tCu​rre​ncy​Ins​tance()
Java Hello World
​ .fo​rma​t(x);
import java.u​[Link]; s = new Simple​Dat​eFo​rma​t(""h:mm a"")
public class Hello { ​ .fo​rma​t(new Date());
​ ​public static void main(S​tring[] args) { s = new Decima​lFo​rma​t("#​,##​0.0​0")
​ ​ ​ ​Sys​[Link]​t.p​rin​tln​("Hello, world!​"); ​ .fo​rma​t(1​25.32);
​ ​ ​ Date now = new Date();
See http:/​/do​cs.o​ra​[Link]​m/j​ava​se/​6/d​‐
​ ​ ​ ​Sys​[Link]​t.p​rin​tln​("Time: " + now);
ocs​/ap​i/j​ava​/te​xt/​pac​kag​e-f​ram​[Link] for
​}
Messag​eFormat and related classes
}

* Save in [Link]
* Compile: javac [Link]
* Run: java Hello

Java Arithmetic Operators

x+y add x-y subtract


x*y multiply x/y divide
x%y modulus ++x / x++ increment
--x / x-- decrement

Assignment shortcuts: x op= y


Example: x += 1 increments x

Java Comparison Operators

x<y Less x <= y Less or eq


x>y Greater x >= y Greater or eq
x == y Equal x != y Not equal

By sschaub Published 17th July, 2012. Sponsored by [Link]


[Link]/sschaub/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
Page 2 of 2. [Link]

You might also like