Skip to main content

java Cheet Sheet

Java Fundamentals Cheat Sheet 

Java cheet sheet

Java Fundamentals

Java Data Types

byte / short / int / long

-123, 10

float / double

235.13

char

'U'

boolean

true, false

String

"­Gre­etings from earth"

Java Statements

If Statement
if ( expression ) {
 ­ statements
} else if ( expression ) {
 ­ statements
} else {
 ­ statements
}

While Loop
while ( expression ) {
 ­ statements
}

Do-While Loop
do {
 ­ statements
} while ( expression );

For Loop
for ( int i = 0; i < max; ++i) {
 ­ statements
}

For Each Loop
for ( var : collection ) {
 ­ statements
}

Switch Statement
switch ( expression ) {
 ­ case value:
 ­ ­ ­ statements
 ­ ­ ­ ­break;
 ­ case value2:
 ­ ­ ­ statements
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ statements
}

Exception Handling
try {
 ­ ­sta­tem­ents;
} catch (Except­ionType e1) {
 ­ ­sta­tem­ents;
} catch (Exception e2) {
 ­ ­cat­ch-all statem­ents;
} finally {
 ­ ­sta­tem­ents;
}

 

Java Data Conver­sions

String to Number
int i = Intege­r.p­ars­eInt(str);
double d = Double.pa­rse­Double(str);

Any Type to String
String s = String.va­lueOf(value);

Numeric Conver­sions
int i = (int) numeric expression;

Java String Methods

s.length()

length of s

s.charAt(i)

extract ith character

s.subst­ring(startend)

substring from start to end-1

s.toUpp­erC­ase()

returns copy of s in ALL CAPS

s.toLow­erC­ase()

returns copy of s in lowercase

s.indexOf(x)

index of first occurence of x

s.replace(oldnew)

search and replace

s.split(regex)

splits string into tokens

s.trim()

trims surrou­nding whitespace

s.equals(s2)

true if s equals s2

s.compa­reTo(s2)

0 if equal/+ if s > s2/- if s < s2

See codingnija100

java.u­til.Ar­rayList Methods

l.add(itm)

Add itm to list

l.get(i)

Return ith item

l.size()

Return number of items

l.remove(i)

Remove ith item

l.set(ival)

Put val at position i

ArrayL­ist­<St­rin­g> names =
 ­ new ArrayL­ist­<St­rin­g>();


java.u­til.Ha­shMap Methods

m.put(key,value)

Inserts value with key

m.get(key)

Retrieves value with key

m.conta­insKey(key)

true if contains key

HashMa­p<S­t­­rin­Â­g­,St­rin­g> names =
 ­ new HashMa­p<S­t­­rin­g, String­>();

See
codingninja100

 

Java Hello World

import java.u­til.Date;

public class Hello {
 ­ ­public static void main(S­tring[] args) {
 ­ ­ ­ ­Sys­tem.ou­t.p­rin­tln­("Hello, world!­");
 ­ ­ ­ Date now = new Date();
 ­ ­ ­ ­Sys­tem.ou­t.p­rin­tln­("Time: " + now);
 ­ }
}

* 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

Java Boolean Operators

! x (not)

x && y (and)

x || y (or)

Java Text Formatting

printf style formatting
System.ou­t.p­rin­tf(­"­Count is %d\n", count);
s = String.fo­rma­t("Count is %d", count);

Messag­eFormat style formatting
s = Messag­eFo­rma­t.f­ormat(
 ­ ­"At {1,time}, {0} eggs hatche­d.",
 ­ 25, new Date());

Individual Numbers and Dates
s = Number­For­mat.ge­tCu­rre­ncy­Ins­tance()
 ­ .fo­rma­t(x);
s = new Simple­Dat­eFo­rma­t(""h:mm a"")
 ­ .fo­rma­t(new Date());
s = new Decima­lFo­rma­t("#­,##­0.0­0")
 ­ .fo­rma­t(1­25.32);

See codingninja100



If you like this post then share with your freind and if  you have any doubt or query than comme.Give your feedback. 
If you want to more content like this then comment topic or your doubt.

Comments