Java cheat sheet

Purpose

People forget. Especially if you write in various languages. If you read it before writing, you may make a lot of progress.

Basic data type Data Type

Mold Description range
byte Integer 8 bits -128~127
short Integer 16 bits -32,768~32,767
int Integer 32 bits -2,147,483,648~2,147,483,647
long Integer 64 bits -9,223,372,036,854,775,808~9,223,372,036,854,775,807
char Unicode character 16 bits \u0000~\uFFFF
float Floating point number 32 bits (abridgement)
double Floating point number 64 bits (abridgement)
boolean - true, false

Operators Operators

Arithmetic Operators

5 + 2;		//7 addition
5 - 2;		//3 subtraction
5 * 2;		//10 multiplication
5 / 2;		//2 division
5 % 2;		//1 remainder

y = ++x;	//Prefix y= (x += 1);
y = x++;	//Postfix y= x; x = x + 1;

Comparison Operators

x < y;		//Less than
x <= y;		//Less than
x >= y;		//that's all
x > y;		//Excess
x == y;		//Equivalent
x != y;		//Not equivalent

Logical operators Boolean Operators

x && y;		// AND
x || y;		// OR
!x;			// NOT

String String

String str = "String";			//Initialization

str.length();					//length
str.subString(start, end);		//start to end-Cut out 1
str1.eq­ual­s(str2);				//Equivalence judgment
str1.co­mpa­reT­o(str2);			  //Comparison
// str1 < str2 -> -1
// str1 == str2 -> 0
// str1 > str2 -> 1

char c = str.charAt(i);				// String ->char i th character
char[] cs = str.toCharArray();		// String ->whole char

Type conversion Data Conversion

int i = Intege­r.p­ars­eIn­t(­str);		// int <- String
int i = (int) d;					  // int <- double

double d = Double.pa­rse­Dou­ble­(s­tr);	// double <- String
double d = (double) i;				  // double <- int

String s = String.va­lue­Of(­i);			// String <- int
String s = String.va­lue­Of(­d);			// String <- double

Numerical processing Math

Math.max(2, 5);		//5 larger
Math.min(2, 5);		//2 smaller
Math.abs(-3);		//3 Absolute value

Math.ceil(1.5);		// 2.0 round up
Math.floor(1.5);	// 1.0 truncation
Math.round(1.5);	// 2.0 rounding

Math.pow(2, 10);	//1024 power
Math.sqrt(2);		// 1.414… Square root
Math.log10(1000);	//3 Common logarithm

Math.PI				//π pi
Math.E				//e base of natural logarithm

String Formatter

I may write it soon.

Control syntax Statements

if

if ( i > 0 ) {
 ­ ;
} else if ( i < 0 ) {
 ­ ­;
} else {
 ­ ­;
}

for / for-each

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

for ( var : colle­ction ) {
 ­ ­;
}

while / do-while

while ( i > 0 ) {
 ­ ­;
}

do {
 ­ ­;
} while ( i > 0 );

switch

switch ( i ) {
 ­ case 0:
 ­ ­ ­ ­;
 ­ ­ ­ ­break;
 ­ case 1:
 ­ ­ ­ ­;
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ ­;
}

Array Array

One-dimensional array

//Initialization
int[] num = new int[10];
Arrays.fill(num, 0);

//Deep copy deep copy
String[] copySrc = new String[10];	//Original
String[] copyDst = new String[10];	//Copy to
copyDst = Arrays.copyOf(copySrc, copySrc.length);

Multidimensional array

//Initialization
int[][] num = new int[10][10];
for (int i = 0; i < num.length; i++) {
	Arrays.fill(num[i], 0);
}

//Deep copy
String[][] copySrc = new String[10][10];	//Original
String[][] copyDst = new String[10][10];	//Copy to
for (int i = 0; i < copySrc.length; i++) {
	copyDst[i] = Arrays.copyOf(copySrc[i], copySrc[i].length);
}

List ArrayList

//Initialization
List<String> list = new ArrayList<String>();

//Manipulation by object
list.add(str);		//add to
list.remove(str);	//Delete
list.indexOf(str);	//Get index
list.contains(str);	//Existence check

//Operation by index
list.get(i);		//Get i th
list.set(i, str);	//Replace i th

HashMap HashMap

//Initialization
// key -> int
// value -> String
HashMa­p<Integer, St­rin­g> hMap =­ new HashMa­p<Integer, String­>();

hMap.pu­t(­i­, ­str);		//add to
hMap.ge­t(­i);			//Get
hMap.co­nta­ins­Key­(i);	//Existence check

Priority Queue

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
pQueue.offer(i);	//add to
pQueue.peek(i);		//Get (do not remove from queue)
pQueue.poll(i);		//Get (remove from queue)

Recommended Posts

Java cheat sheet
Java Stream API cheat sheet
C # cheat sheet for Java engineers
JMeter cheat sheet
Competitive programming private cheat sheet (Java)
javac, jar, java command cheat sheet
Kotlin cheat sheet
[Docker cheat sheet]
[Java] Data type / string class cheat sheet
Mockito + PowerMock cheat sheet
Eclipse Collections cheat sheet
Rails Tutorial cheat sheet
Spring Boot2 cheat sheet
SCSS notation cheat sheet
Oreshiki docker-compose cheat sheet
Docker command cheat sheet
[Eclipse] Shortcut key cheat sheet
Java
Java
A cheat sheet for Java experienced people to learn Ruby (rails)
Getting Started with Doma-Criteria API Cheat Sheet
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Technology for reading source code (cheat sheet)
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review