Show Menu
Cheatography

UJCS | Ultimate Java Cheat Sheet by

The Ultimate Java Cheat Sheet for programmers who are already familiar with programming in a different language. CS without specific package info. Optimized for printing.

Java Data Types

byte/s­hor­t/i­nt/long
2^ (8/ 16/ 32/ 64)
float [32bit] / double [64bit]
1.0f / 1.0d, 1.0
char [16bit]
"­U", "­±"
String
"­Hello World"

Literals

2 ⇒ int
2.0F ⇒ float
2L ⇒ long
2.0 ; 2.0D ⇒ double
"­u" ⇒ String
'u' ⇒ char
true ⇒ boolean
false ⇒ boolean
number literal modifiers are case-i­nse­sitive

Inc- / Decrement

a++
++a
a--
--a
➜ return a++ / a--
➜ return a
Assignment shortcuts: x op= y
Exp:
x += 2;

Variables

int i = 5, j;
int a = 3, b = a + 1;
final int c; //=> Constant
c = 22; // may be init after

Comments

// Single Line
/* Multi Line */
/** Docu String */

Array

int[] a; //Declaration
a = new int[5] //Dimension
int[] a = new int[5];
int[] b = {10, 20, 30};

int[][] matrix = new int[2][3];
Init Values: 0, "­\0", false, null

Array Methods

int[] a;
a.length;     //length of array

ArrayList

ArrayList<Double> nums = new ArrayList<>();
nums.add(2.3);
nums.size() == 1
double a = nums.get(0);
Like a list in python

Type Casting

int a, b;
a = (double) b * a    //b => double
a = b * (double) a    //a => double

a = (double) (a * b)
/* (a * b) will be calculated 
with int logic then 
type-casted to double */

Strings

1. Strings are a class in Java.
2. Concating is possible without type-cast
String a = "Num: " + 5;


3. Parsing:
int i = Intege­r.p­ars­eIn­t("2­2");

float f = Float.p­ar­seF­loa­t("1.3");

boolean b = Boolea­n.p­ars­eBo­ole­an(­"­Tru­e");

String Functions

s.equa­ls(­String s2) -> bool
s.toLo­wer­Case()
s.toUp­per­Case()
s.repl­ace­(char old, char new)
s.repl­ace­(String old, String new)
s.inde­xOf­(String s) //-1 if not availabe
s.last­Ind­exO­f(S­tring s)
s.spli­t(S­tring delimiter) -> String[]
all functions return the modified String. They don't modify the original String.
 

Bitwise Operations

int
boolean
 
!
NOT
&
&&
AND
|
||
OR
^
^
XOR

Biwise Shifts

~
Complement
<<
Shift left
>>
Shift right
>>>
Shift right Zero fill

Arithmetic Operator Results (+ - * / % )

Both Arguments
byte, short, int
⇒ int
One, or Both Arguments
long
⇒ long
float
⇒ float
double
⇒ double
Examples:
byte * byte ⇒ int;  double + float ⇒ double

Java Operations Order

1. Members
() [] .
2. Multip­lik­ation
* / %
3. Addition
+ -
4. XOR
^
5. Logical AND
&&
6. Logical OR
||
a++, a-- counts as 3. Addition
int a = 5, b = 8;

int c = a * b++     //c is 40;

Java Naming Convention

Constants:
MAX, PI, MIN_TIME

Variables, Methods, Packages:
xVal, int1Arr, date, showDate

Classes:
Date, Databa­seH­elper

File IO

File file = new File("text.txt");

file.exsits();
file.createNewFile();
file.delete();

BufferdReader = new BufferedReader(file);
while ((line = input.readLine()) != null) {
 //statements with line
}
There are other classes, but just use
Buffer­dReader

abstract / Interface

abstract Method
public abstract fun();
abstract Class
public abstract class Test{}
Interface
Like abstract class, but with only abstract functions. You don't need abstract for these
Abstract Classes and Methods are without implem­ent­ation.
You use
implements
for Interfaces

UI

// Pop-Up Box:
import javax.swing.*;
String out = JOptionPane.showInputDialog("Inp: ");

Regular Expres­sions (Regex)

Pattern p = Pattern.compile("\\w+");
Matcher m = p.matcher("abc");

while (m.find()) {
    m.group()
}

//true if whole string matches
m.matches() -> bool
 

Bolier­plate

//Syntax for a standalone application in Java:
class <classname>
{
      public static void main(String args[])  {
          statements;
          ————————;
          ————————;
      }
}

Bolier­plate cont.

Steps to run the above applic­ation:

1. Type the program in IntelliJ or notepad. Save the file with a
.java
extension.
2. The file name should be the same as the class, which has the main method.
3. To compile the program, using javac compiler, type the following on the command line:
Syntax:
javac <fi­len­ame.ja­va>

Example:
javac abc.java

4. After compil­ation, run the program using the Java interp­reter.
Syntax: java <fi­lan­ame> (without the .java extension)
Example: java abc
5. The program output will be displayed on the command line

Java Statements

If Statem­ent
if ( expre­ssion ) {
 ­ ­st­ate­ments
} else if ( expre­ssion ) {
 ­ ­st­ate­ments
} else {
 ­ ­st­ate­ments
}

While Loop
while ( expre­ssion ) {
 ­ ­st­ate­ments
}

Do-While Loop
do {
 ­ ­st­ate­ments
} while ( expre­ssion );

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

For Each Loop
for ( vartype var : colle­ction ) {
 ­ ­st­ate­ments
}

Switch Statem­ent
switch ( expre­ssion ) {
 ­ case value:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ case value2:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ ­st­ate­ments
}

Exce­ption Handling
try {
 ­ ­sta­tem­ents;
} catch (Exce­pti­onType e1) {
 ­ ­sta­tem­ents;
} catch (Exception e2) {
 ­ ­cat­ch-all statem­ents;
} finally {
 ­ ­sta­tem­ents;
}

IntelliJ Emmets

psvm
public static void main(S­tring[] args) {}
sout
System.ou­t.p­rin­tln();
               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          PHP Cheat Sheet
          Python Cheat Sheet