Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

UJCS | Ultimate Java Cheat Sheet

by Welten via cheatography.com/134029/cs/27565/

Java Data Types Array Methods Bitwise Operations

byte/s​hor​t/i​nt/long 2^ (8/ 16/ 32/ int[] a; int boolean


64) a.length; //length of array ! NOT
float [32bit] / double 1.0f / 1.0d, 1.0 & && AND
[64bit] ArrayList
| || OR
char [16bit] "​U", "​±" ArrayList<Double> nums = new
^ ^ XOR
String "​Hello World" ArrayList<>();
nums.a​dd(​2.3);
Biwise Shifts
Literals nums.s​ize() == 1
~ Complement
double a = nums.g​et(0);
2 ⇒ int 2.0F ⇒ float
<< Shift left
2L ⇒ long 2.0 ; 2.0D ⇒ double Like a list in python
>> Shift right
"​u" ⇒ String 'u' ⇒ char
Type Casting >>> Shift right Zero fill
true ⇒ boolean false ⇒ boolean
int a, b;
number literal modifiers are case-i​nse​sitive Arithmetic Operator Results (+ - * / % )
a = (double) b * a //b => double
a = b * (double) a //a => double Both Argume​nts
Inc- / Decrement
a = (double) (a * b) byte, short, int ⇒ int
a++ ++a /* (a * b) will be calculated One, or Both Argume​nts
a-- --a with int logic then long ⇒ long
➜ return a++ / a-- ➜ return a type-c​asted to double */
float ⇒ float
Assignment shortcuts: x op= y double ⇒ double
Strings
Exp: x += 2;
Examples: byte * byte ⇒ int;
1. Strings are a class in Java.
double + float ⇒ double
Variables 2. Concating is possible without type-cast
String a = "Num: " + 5;
int i = 5, j; Java Operations Order
3. Parsing:
int a = 3, b = a + 1;
int i = Intege​r.p​ars​eIn​t("2​2"); 1. Members () [] .
final int c; //=> Constant
float f = Float.p​ar​seF​loa​‐ 2. Multip​lik​ation */%
c = 22; // may be init after
t("1.3"); 3. Addition +-
boolean b = Boolea​n.p​ars​eBo​ole​‐
Comments 4. XOR ^
an(​"​Tru​e");
// Single Line 5. Logical AND &&
/* Multi Line */
String Functions 6. Logical OR ||
/** Docu String */
s.equa​ls(​String s2) -> bool a++, a-- counts as 3. Addition
int a = 5, b = 8;
Array s.toLo​wer​Case()
int c = a * b++ //c is 40;
s.toUp​per​Case()
int[] a; //Declaration
a = new int[5] //Dime​nsion s.repl​ace​(char old, char new) Java Naming Convention
int[] a = new int[5]; s.repl​ace​(String old, String new)
Constants:
int[] b = {10, 20, 30}; s.inde​xOf​(String s) //-1 if not availabe MAX, PI, MIN_TIME
int[][] matrix = new int[2][3]; Variables, Methods, Packages:
s.last​Ind​exO​f(S​tring s)
Init Values: 0, "​\0", false, null s.spli​t(S​tring delimiter) -> String[] xVal, int1Arr, date, showDate
Classes:
all functions return the modified String.
Date, Databa​seH​elper
They don't modify the original String.

By Welten Published 7th July, 2021. Sponsored by CrosswordCheats.com


cheatography.com/welten/ Last updated 12th July, 2021. Learn to solve cryptic crosswords!
Page 1 of 3. http://crosswordcheats.com
UJCS | Ultimate Java Cheat Sheet
by Welten via cheatography.com/134029/cs/27565/

File IO Bolier​plate Java Statements (cont)

File file = new //Syntax for a standalone ​ ​st​at​e​ments


File("text.txt"); application in Java: }
file.e​xsi​ts(); class <cl​ass​nam​e> Do-While Loop
file.c​rea​teN​ewF​ile(); { do {
file.d​ele​te(); ​ ​ ​ ​ ​ ​public static void main(S​‐ ​ ​st​at​e​ments
Buffer​dReader = new Buffer​edR​‐ tring args[]) { } while ( expre​​ssion );
ead​er(​file); ​ ​ ​ ​ ​ ​ ​ ​ ​ ​sta​tem​ents; For Loop
while ((line = input.r​ea​dLi​ne()) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​———​—————; for ( int i = 0; i < max; ++i) {
!= null) { ​ ​ ​ ​ ​ ​ ​ ​ ​ ​———​—————; ​ ​st​at​e​ments
​//s​tat​ements with line ​ ​ ​ ​ ​ } }
} } For Each Loop
for ( vartype var : colle​​ction )
There are other classes, but just use
Bolier​plate cont. {
Buffe​rdR​eader
Steps to run the above applic​ati​on: ​ ​st​at​e​ments

abstract / Interface }
1. Type the program in IntelliJ or notepad. Switch Statem​​ent
abstract Method
Save the file with a .java extension. switch ( expre​​ssion ) {
public abstract fun();
2. The file name should be the same as the ​ case value:
abstract Class class, which has the main method. ​ ​ ​ ​st​at​e​ments
public abstract class Test{} 3. To compile the program, using javac ​ ​ ​ ​break;
compiler, type the following on the ​ case value2:
Interface
command line: ​ ​ ​ ​st​at​e​ments
Like abstract class, but with only abstract Syntax: javac <fi​len​ame.ja​va> ​ ​ ​ ​break;
functions. You don't need abstract for Example: javac abc.java ​ ​def​ault:
these
4. After compil​ation, run the program using ​ ​ ​ ​st​at​e​ments
Abstract Classes and Methods are without the Java interp​reter. }
implem​ent​ation. Syntax: java <fi​lan​ame> (without the .java
Exce​​ption Handling
You use imple​ments for Interfaces extension)
try {
Example: java abc
​ ​sta​t​em​​ents;
UI 5. The program output will be displayed on
} catch (Exce​​pti​​onType e1) {
the command line
// Pop-Up Box: ​ ​sta​t​em​​ents;
import javax.s​wi​ng.*; } catch (Exception e2) {
Java Statements
String out = JOptio​nPa​ne.s​ho​wIn​‐ ​ ​cat​c​h-all statem​​ents;
If Statem​ent
put​Dia​log​("Inp: "); } finally {
if ( expre​​ssion ) {
​ ​sta​t​em​​ents;
Regular Expres​sions (Regex) ​ ​st​at​e​ments
}
} else if ( expre​​ssion ) {
Pattern p =
​ ​st​at​e​ments
Pattern.compile("\\w+");
} else {
Matcher m = p.matc​her​("ab​c");
​ ​st​at​e​ments
while (m.find()) {
}
​ ​ ​ ​m.g​roup()
While Loop
}
while ( expre​​ssion ) {
//true if whole string matches
m.matc​hes() -> bool

By Welten Published 7th July, 2021. Sponsored by CrosswordCheats.com


cheatography.com/welten/ Last updated 12th July, 2021. Learn to solve cryptic crosswords!
Page 2 of 3. http://crosswordcheats.com
UJCS | Ultimate Java Cheat Sheet
by Welten via cheatography.com/134029/cs/27565/

IntelliJ Emmets

psvm public static void main(S​tring[] args) {}


sout System.ou​t.p​rin​tln();

By Welten Published 7th July, 2021. Sponsored by CrosswordCheats.com


cheatography.com/welten/ Last updated 12th July, 2021. Learn to solve cryptic crosswords!
Page 3 of 3. http://crosswordcheats.com

You might also like