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.
|
byte/short/int/long
|
2^ (8/ 16/ 32/ 64)
|
|
float [32bit] / double [64bit]
|
1.0f / 1.0d, 1.0
|
|
char [16bit]
|
"U", "±"
|
|
String
|
"Hello World"
|
|
2 ⇒ int
|
2.0F ⇒ float
|
|
2L ⇒ long
|
2.0 ; 2.0D ⇒ double
|
|
"u" ⇒ String
|
'u' ⇒ char
|
|
true ⇒ boolean
|
false ⇒ boolean
|
|
a++
|
++a
|
|
a--
|
--a
|
|
➜ return a++ / a--
|
➜ return a
|
x += 2;
int i = 5, j;
int a = 3, b = a + 1;
final int c; //=> Constant
c = 22; // may be init after
// Single Line
/\* Multi Line \*/
/\*\* Docu String \*/
int[] a; //Declaration
a = new int[5] //Dimension
int[] a = new int[5];
int[] b = {10, 20, 30};
int[][] matrix = new int[2][3];
int[] a;
a.length; //length of array
ArrayList<Double> nums = new ArrayList<>();
nums.add(2.3);
nums.size() == 1
double a = nums.get(0);
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 \*/
String a = "Num: " + 5;int i = Integer.parseInt("22");float f = Float.parseFloat("1.3");boolean b = Boolean.parseBoolean("True");|
s.equals(String s2) -> bool
|
|
s.toLowerCase()
|
|
s.toUpperCase()
|
|
s.replace(char old, char new)
|
|
s.replace(String old, String new)
|
|
s.indexOf(String s) //-1 if not availabe
|
|
s.lastIndexOf(String s)
|
|
s.split(String delimiter) -> String[]
|
|
int
|
boolean
|
|
|
!
|
NOT
|
|
|
&
|
&&
|
AND
|
|
|
|
||
|
OR
|
|
^
|
^
|
XOR
|
|
~
|
Complement
|
|
<<
|
Shift left
|
|
>>
|
Shift right
|
|
>>>
|
Shift right Zero fill
|
|
Both Arguments
|
|
|
byte, short, int
|
⇒ int
|
|
One, or Both Arguments
|
|
|
long
|
⇒ long
|
|
float
|
⇒ float
|
|
double
|
⇒ double
|
byte * byte ⇒ int; double + float ⇒ double
|
1. Members
|
() [] .
|
|
2. Multiplikation
|
* / %
|
|
3. Addition
|
+ -
|
|
4. XOR
|
^
|
|
5. Logical AND
|
&&
|
|
6. Logical OR
|
||
|
int a = 5, b = 8;int c = a * b++ //c is 40;
MAX, PI, MIN_TIMExVal, int1Arr, date, showDateDate, DatabaseHelperFile file = new File("text.txt");
file.exsits();
file.createNewFile();
file.delete();
BufferdReader = new BufferedReader(file);
while ((line = input.readLine()) != null) {
//statements with line
}
BufferdReader
|
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
|
implements for Interfaces
// Pop-Up Box:
import javax.swing.*;
String out = JOptionPane.showInputDialog("Inp: ");
Pattern p = Pattern.compile("\\w+");
Matcher m = p.matcher("abc");
while (m.find()) {
m.group()
}
//true if whole string matches
m.matches() -> bool
//**Syntax for a standalone application in Java:**
class <classname>
{
public static void main(String args[]) {
statements;
————————;
————————;
}
}
.java extension.javac <filename.java>javac abc.java**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 ( vartype var : collection ) {
statements
}
**Switch Statement**
switch ( expression ) {
case value:
statements
break;
case value2:
statements
break;
default:
statements
}
**Exception Handling**
try {
statements;
} catch (ExceptionType e1) {
statements;
} catch (Exception e2) {
catch-all statements;
} finally {
statements;
}
|
psvm
|
public static void main(String[] args) {}
|
|
sout
|
System.out.println();
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.