0% found this document useful (0 votes)
9 views

Sschaub Java-Fundamentals

This document is a cheat sheet for Java fundamentals. It covers Java data types, statements, operators, and methods for String, ArrayList, HashMap, formatting numbers and dates. It also includes a basic Hello World example.

Uploaded by

Pas DEN
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)
9 views

Sschaub Java-Fundamentals

This document is a cheat sheet for Java fundamentals. It covers Java data types, statements, operators, and methods for String, ArrayList, HashMap, formatting numbers and dates. It also includes a basic Hello World example.

Uploaded by

Pas DEN
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
You are on page 1/ 2

Java Fundamentals Cheat Sheet

by sschaub via cheatography.com/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​cle.co​m/j​ava​se/​6/d​‐
} Java Data Conver​sions
ocs​/ap​i/j​ava​/la​ng/​Str​ing.html for more.
Do-While Loop String to Number
do { int i = Intege​r.p​ars​eInt(str); java.u​til.Ar​rayList Methods
​ statements double d = Double.pa​rse​Double(str);
l.add(itm ) Add itm to list
} while ( expression ); Any Type to String
For Loop String s = String.va​lueOf(value); l.get(i) Return ith item
for ( int i = 0; i < max; ++i) { Numeric Conver​sions l.size() Return number of items
​ statements int i = (int) numeric expression; l.remove(i) Remove ith item
}
l.set(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​cle.co​m/j​ava​se/​6/d​‐
ocs​/ap​i/j​ava​/ut​il/​Arr​ayL​ist.html for more.

By sschaub Published 17th July, 2012. Sponsored by CrosswordCheats.com


cheatography.com/sschaub/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Java Fundamentals Cheat Sheet
by sschaub via cheatography.com/1000/cs/464/

java.u​til.Ha​shMap Methods Java Boolean Operators

m.put(key,value) Inserts value with key ! x (not) x && y (and) x || y (or)


m.get(key) Retrieves value with
key Java Text Formatting

m.conta​‐ true if contains key printf style formatting


insKey( key) System.ou​t.p​rin​tf(​"​Count is %d\n", count);
s = String.fo​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​cle.co​m/j​ava​se/​6/d​‐
​ 25, new Date());
ocs​/ap​i/j​ava​/ut​il/​Has​hMa​p.html for more.
Individual Numbers and Dates
s = Number​For​mat.ge​tCu​rre​ncy​Ins​tance()
Java Hello World
​ .fo​rma​t(x);
import java.u​til.Date; 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​tem.ou​t.p​rin​tln​("Hello, world!​"); ​ .fo​rma​t(1​25.32);
​ ​ ​ Date now = new Date();
See http:/​/do​cs.o​ra​cle.co​m/j​ava​se/​6/d​‐
​ ​ ​ ​Sys​tem.ou​t.p​rin​tln​("Time: " + now);
ocs​/ap​i/j​ava​/te​xt/​pac​kag​e-f​ram​e.html for
​}
Messag​eFormat and related classes
}

* Save in Hello.java
* Compile: javac Hello.java
* 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 CrosswordCheats.com


cheatography.com/sschaub/ Last updated 12th May, 2016. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like