Show Menu
Cheatography

Java Basics to Intermediate Cheat Sheet (DRAFT) by

A complete cheat sheet for Beginner to Intermediate Java programmers

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Primitive Data Types

type
size
range of values
byte
8-bit signed 2's comp
(-128 -> 127)
short
16-bit signed 2's comp
(-32,768 -> 32,767)
int
32-bit signed 2's comp
(-231 -> 231-1)
long
64-bit signed 2's comp
(-263 -> 263-1)
long
64-bit unsigned
(0 -> 264-1)
float
single­-pr­ecision 32-bit signed
(-3.40­282347 x 1038 -> 3.40282347 x 1038)
double
double­-pr­ecision 64-bit signed
(-1.79­769­313­486­231570 x 10308 -> 1.7976­931­348­6231570 x 10308)
char
16-bit unsigned Unicode character
(0 -> 65,535)
boolean
size not defined
true / false

Hello World

{{noshy}}public class HelloWorld 
{
    public static void main(String[] args) 
    {
        System.out.println("Hello World!"); 
    }
} 
 

Declaring and Inital­izing

Integers
int a, b;
<-- Declare two integer variables
a = 100;
<-- Initialize 'a' with a value of 100
b = 18;
<-- Initialize 'b' with a value of 18
int c = a + b;
<-- Declare and initialize c with the value of a plus b

Double
double a, b;

a = 1.57;

b = 9.8765;

double c = a + b;


It is the same for every primitive data type.

Operations on Number Variables

Integer
sign
+ -
+99 -or- -99
 
add
+
5 + 3 = 8
 
subtract
-
5 - 3 = 2
 
multiply
*
5*3 = 15
 
divide
/
5/3 = 1 no fractional part
 
remainder
%
5 % 3 = 2
Floati­ng-­Point Numbers
add
+
3.141 + 2.0 = 5.141
 
subtract
-
3.141 - 2.0 = 1.111
 
multiply
*
3.141 * 2.0 = 6.282
 
divide
/
3.141 / 2.0 = 1.5705

Boolean Operations

Values
Literals
Operations
Operators
true
true
and
&&
false
false
or
||
   
not
!
 
a
b
a && b
a || b
false (0)
false (0)
false
false
false (0)
true (1)
false
true
true (1)
false (0)
false
true
true (1)
true (1)
true
true
 

Comparison Operators

Operator
Meaning
true
false
==
equal
2 == 2
2 == 3
!=
not equal
3 != 2
2 != 2
<
less than
2 < 13
2 < 2
<=
less than or equal
2 <= 2
3 <= 2
>
greater than
13 > 2
2 > 13
>=
greater than or equal
3 >= 2
2 >= 3
Examples:
Check if a number is a multiple of 2
(x % 2 == 0)
returns true if x is a multiple of 2

Check months
(month >= 1) && (month <= 12)
returns true if month is between 1 and 12

Printing and Parsing

Printing to console
System.ou­t.p­rin­t(S­tring s);
print s

System.ou­t.p­rin­tln­(String s);
print s followed by newline

System.ou­t.p­rin­tln();
print a newline

Parse comman­d-line args
int Intege­r.p­ars­eIn­t(S­tring s);
convert s to an int value

double Double.pa­rse­Dou­ble­(String s);
convert s to a double value

long Long.p­ars­eLo­ng(­String s);
convert s to a long value