SlideShare a Scribd company logo
Java Cheat Sheet 
Java Data Types 
byte / short / int / 
long 
‑123, 10 
float / double 235.13 
char 'U' 
boolean true, false 
String "Greetings from 
earth" 
Java Statements 
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 ( 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; 
} 
Java Data Conversions 
String to Number 
int i = Integer.parseInt(str ); 
double d = Double.parseDouble(str ); 
Any Type to String 
String s = String.valueOf(value ); 
Numeric Conversions 
int i = (int) numeric expression ; 
Java String Methods 
s .length() length of s 
s .charAt(i ) extract i th character 
s .substring(start , 
end ) 
substring from start to 
end ‑1 
s .toUpperCase() returns copy of s in ALL 
CAPS 
s .toLowerCase() returns copy of s in 
lowercase 
s .indexOf(x ) index of first occurence 
of x 
s .replace(old , 
new ) 
search and replace 
s .split(regex ) splits string into tokens 
s .trim() trims surrounding 
whitespace 
s .equals(s2 ) true if s equals s2 
s .compareTo(s2 ) 0 if equal/+ if s > s2/‑ 
if s < s2 
java.util.ArrayList Methods 
l .add(itm ) Add itm to list 
l .get(i ) Return i th item 
l .size() Return number of items 
l .remove(i ) Remove i th item 
l .set(i , val ) Put val at position i 
ArrayList<String> names = new 
ArrayList<String>(); 
java.util.HashMap Methods 
m .put(key ,value ) Inserts value with key 
m .get(key ) Retrieves value with 
key 
m .containsKey(key ) true if contains key 
HashMap<String,String> names = new 
HashMap<String, String>(); 
Updated by Saeid Zebardast 
Twitter: @saeid 
Email: saeid.zebardast@gmail.com 
Home: http://zebardast.ir 
About: http://about.me/saeid 
Java Hello World 
import java.util.Date; 
public class Hello { 
public static void main(String[] args) { 
System.out.println("Hello, world!"); 
Date now = new Date(); 
System.out.println("Time: " + now); 
} 
} 
* Save in Hello.java 
* Compile: javac Hello.java 
* Run: java Hello 
Java Arithmetic Operators 
x + 
y 
add x ‑ y subtract 
x * 
y 
multiply x / y divide 
x % 
y 
modulus ++x / 
x++ 
increment 
‑‑x / x‑‑ decrement 
Assignment shortcuts: x op = y 
Example: x += 1 increments x 
Java Comparison Operators 
x < y Less x <= y Less or eq 
x > y Greater x >= y Greater or eq 
x == 
Equal x != y Not equal 
y 
Java Boolean Operators 
! x (not) x && y (and) x || y (or) 
Java Text Formatting 
printf style formatting 
System.out.printf("Count is %dn", count); 
s = String.format("Count is %d", count); 
MessageFormat style formatting 
s = MessageFormat.format( 
"At {1,time}, {0} eggs hatched.", 
25, new Date()); 
Individual Numbers and Dates 
s = NumberFormat.getCurrencyInstance() 
.format(x); 
s = new SimpleDateFormat(""h:mm a"") 
.format(new Date()); 
s = new DecimalFormat("#,##0.00") 
.format(125.32); 
Source: http://cheatography.com/sschaub

More Related Content

PDF
Java Cheat Sheet
GlowTouch
 
PDF
Advanced java practical semester 6_computer science
Niraj Bharambe
 
PPTX
Longest Common Subsequence (LCS) Algorithm
Darshit Metaliya
 
PPT
Nfa vs dfa
raosir123
 
PPTX
Marge Sort
Ankit92Chitnavis
 
PPTX
Moore Mealy Machine Conversion
Aiman Hafeez
 
PPTX
Lecture 7 arrays
manish kumar
 
PPT
Digital Signature.ppt
SABITHARASSISTANTPRO
 
Java Cheat Sheet
GlowTouch
 
Advanced java practical semester 6_computer science
Niraj Bharambe
 
Longest Common Subsequence (LCS) Algorithm
Darshit Metaliya
 
Nfa vs dfa
raosir123
 
Marge Sort
Ankit92Chitnavis
 
Moore Mealy Machine Conversion
Aiman Hafeez
 
Lecture 7 arrays
manish kumar
 
Digital Signature.ppt
SABITHARASSISTANTPRO
 

What's hot (20)

PPTX
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
PPTX
Galois field
Niaj Morshed
 
PPTX
3. R- list and data frame
krishna singh
 
PPTX
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
DOC
Dbms lab Manual
Vivek Kumar Sinha
 
PPT
Top down parsing
ASHOK KUMAR REDDY
 
PPTX
Quine Mc Cluskey Method
Shahneel Siddiqui
 
PPTX
DISCRETE LOGARITHM PROBLEM
MANISH KUMAR
 
PPTX
Ai quantifiers
Tayyaba Jabeen
 
PDF
Java cheat sheet
Piyush Mittal
 
PDF
Java Simple Programs
Upender Upr
 
PPTX
It elective cs366 barizo radix.docx
ChristianBarizo
 
PPTX
Removing ambiguity-from-cfg
Ashik Khan
 
PPT
C program compiler presentation
Rigvendra Kumar Vardhan
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PDF
What Are Python Modules? Edureka
Edureka!
 
PDF
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
PPTX
Ambiguous & Unambiguous Grammar
MdImamHasan1
 
PPT
3.5 merge sort
Krish_ver2
 
PPTX
shell and merge sort
i i
 
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
Galois field
Niaj Morshed
 
3. R- list and data frame
krishna singh
 
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
Dbms lab Manual
Vivek Kumar Sinha
 
Top down parsing
ASHOK KUMAR REDDY
 
Quine Mc Cluskey Method
Shahneel Siddiqui
 
DISCRETE LOGARITHM PROBLEM
MANISH KUMAR
 
Ai quantifiers
Tayyaba Jabeen
 
Java cheat sheet
Piyush Mittal
 
Java Simple Programs
Upender Upr
 
It elective cs366 barizo radix.docx
ChristianBarizo
 
Removing ambiguity-from-cfg
Ashik Khan
 
C program compiler presentation
Rigvendra Kumar Vardhan
 
oops concept in java | object oriented programming in java
CPD INDIA
 
What Are Python Modules? Edureka
Edureka!
 
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
Ambiguous & Unambiguous Grammar
MdImamHasan1
 
3.5 merge sort
Krish_ver2
 
shell and merge sort
i i
 
Ad

Viewers also liked (17)

PDF
Cheat Sheet java
arkslideshareacc
 
PDF
Cheat sheet - String Java (Referência rápida)
Rafael Liberato
 
PDF
MySQL Cheat Sheet
Saeid Zebardast
 
PDF
Java for beginners
Saeid Zebardast
 
PDF
REST: putting the web back in to web services
Kerry Buckley
 
PPT
Goal4d
andyhot
 
PDF
jquery cheat sheet
johnnytomcat
 
PDF
Bash Cheat Sheet -- Bash History Cheat Sheet
Peter Krumins
 
PDF
Developing Applications with MySQL and Java for beginners
Saeid Zebardast
 
PDF
Web Components Revolution
Saeid Zebardast
 
DOCX
Java cheat sheet
Saifur Rahman
 
PDF
Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!
Noopur Gupta
 
PDF
24 Books You've Never Heard Of - But Will Change Your Life
Ryan Holiday
 
PDF
20 Quotes To Turn Your Obstacles Into Opportunities
Ryan Holiday
 
DOC
Learn Java 3D
Jay Thakkar
 
PDF
Work Rules!
Laszlo Bock
 
PPT
Cybercrime presentation
Rajat Jain
 
Cheat Sheet java
arkslideshareacc
 
Cheat sheet - String Java (Referência rápida)
Rafael Liberato
 
MySQL Cheat Sheet
Saeid Zebardast
 
Java for beginners
Saeid Zebardast
 
REST: putting the web back in to web services
Kerry Buckley
 
Goal4d
andyhot
 
jquery cheat sheet
johnnytomcat
 
Bash Cheat Sheet -- Bash History Cheat Sheet
Peter Krumins
 
Developing Applications with MySQL and Java for beginners
Saeid Zebardast
 
Web Components Revolution
Saeid Zebardast
 
Java cheat sheet
Saifur Rahman
 
Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!
Noopur Gupta
 
24 Books You've Never Heard Of - But Will Change Your Life
Ryan Holiday
 
20 Quotes To Turn Your Obstacles Into Opportunities
Ryan Holiday
 
Learn Java 3D
Jay Thakkar
 
Work Rules!
Laszlo Bock
 
Cybercrime presentation
Rajat Jain
 
Ad

Similar to Java Cheat Sheet (20)

PPTX
07. Java Array, Set and Maps
Intro C# Book
 
PPTX
CAP615-Unit1.pptx
SatyajeetGaur3
 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
PPTX
JavaVariablesTypes.pptx
charusharma165
 
PPT
Java căn bản - Chapter9
Vince Vo
 
PPTX
Java Basics 1.pptx
TouseeqHaider11
 
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
PPTX
arrays.pptx
SachinBhosale73
 
PPTX
Nalinee java
Nalinee Choudhary
 
PPSX
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
PPTX
Java introduction
Samsung Electronics Egypt
 
PPTX
VARIABLE AND DATATYPES-JAVA.pptx
Marisol Domingo
 
PDF
Sam wd programs
Soumya Behera
 
PPTX
Fundamental programming structures in java
Shashwat Shriparv
 
PPT
Basic elements of java
Ahmad Idrees
 
PDF
Vision academy classes_bcs_bca_bba_java part_2
NayanTapare1
 
PDF
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
bhagyashri686896
 
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
PPTX
Java Basic Elements Lecture on Computer Science
AnezkaJaved
 
PDF
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
07. Java Array, Set and Maps
Intro C# Book
 
CAP615-Unit1.pptx
SatyajeetGaur3
 
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
JavaVariablesTypes.pptx
charusharma165
 
Java căn bản - Chapter9
Vince Vo
 
Java Basics 1.pptx
TouseeqHaider11
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
arrays.pptx
SachinBhosale73
 
Nalinee java
Nalinee Choudhary
 
DISE - Windows Based Application Development in Java
Rasan Samarasinghe
 
Java introduction
Samsung Electronics Egypt
 
VARIABLE AND DATATYPES-JAVA.pptx
Marisol Domingo
 
Sam wd programs
Soumya Behera
 
Fundamental programming structures in java
Shashwat Shriparv
 
Basic elements of java
Ahmad Idrees
 
Vision academy classes_bcs_bca_bba_java part_2
NayanTapare1
 
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
bhagyashri686896
 
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
Java Basic Elements Lecture on Computer Science
AnezkaJaved
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 

More from Saeid Zebardast (9)

PDF
Introduction to Redis
Saeid Zebardast
 
PDF
An Introduction to Apache Cassandra
Saeid Zebardast
 
PDF
An overview of Scalable Web Application Front-end
Saeid Zebardast
 
PDF
MySQL for beginners
Saeid Zebardast
 
PDF
هفده اصل افراد موثر در تیم
Saeid Zebardast
 
PDF
What is good design?
Saeid Zebardast
 
PDF
How to be different?
Saeid Zebardast
 
PDF
What is REST?
Saeid Zebardast
 
PDF
معرفی گنو/لینوکس و سیستم عامل های متن باز و آزاد
Saeid Zebardast
 
Introduction to Redis
Saeid Zebardast
 
An Introduction to Apache Cassandra
Saeid Zebardast
 
An overview of Scalable Web Application Front-end
Saeid Zebardast
 
MySQL for beginners
Saeid Zebardast
 
هفده اصل افراد موثر در تیم
Saeid Zebardast
 
What is good design?
Saeid Zebardast
 
How to be different?
Saeid Zebardast
 
What is REST?
Saeid Zebardast
 
معرفی گنو/لینوکس و سیستم عامل های متن باز و آزاد
Saeid Zebardast
 

Recently uploaded (20)

PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Software Development Methodologies in 2025
KodekX
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 

Java Cheat Sheet

  • 1. Java Cheat Sheet Java Data Types byte / short / int / long ‑123, 10 float / double 235.13 char 'U' boolean true, false String "Greetings from earth" Java Statements 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 ( 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; } Java Data Conversions String to Number int i = Integer.parseInt(str ); double d = Double.parseDouble(str ); Any Type to String String s = String.valueOf(value ); Numeric Conversions int i = (int) numeric expression ; Java String Methods s .length() length of s s .charAt(i ) extract i th character s .substring(start , end ) substring from start to end ‑1 s .toUpperCase() returns copy of s in ALL CAPS s .toLowerCase() returns copy of s in lowercase s .indexOf(x ) index of first occurence of x s .replace(old , new ) search and replace s .split(regex ) splits string into tokens s .trim() trims surrounding whitespace s .equals(s2 ) true if s equals s2 s .compareTo(s2 ) 0 if equal/+ if s > s2/‑ if s < s2 java.util.ArrayList Methods l .add(itm ) Add itm to list l .get(i ) Return i th item l .size() Return number of items l .remove(i ) Remove i th item l .set(i , val ) Put val at position i ArrayList<String> names = new ArrayList<String>(); java.util.HashMap Methods m .put(key ,value ) Inserts value with key m .get(key ) Retrieves value with key m .containsKey(key ) true if contains key HashMap<String,String> names = new HashMap<String, String>(); Updated by Saeid Zebardast Twitter: @saeid Email: [email protected] Home: http://zebardast.ir About: http://about.me/saeid Java Hello World import java.util.Date; public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); Date now = new Date(); System.out.println("Time: " + now); } } * Save in Hello.java * Compile: javac Hello.java * Run: java Hello Java Arithmetic Operators x + y add x ‑ y subtract x * y multiply x / y divide x % y modulus ++x / x++ increment ‑‑x / x‑‑ decrement Assignment shortcuts: x op = y Example: x += 1 increments x Java Comparison Operators x < y Less x <= y Less or eq x > y Greater x >= y Greater or eq x == Equal x != y Not equal y Java Boolean Operators ! x (not) x && y (and) x || y (or) Java Text Formatting printf style formatting System.out.printf("Count is %dn", count); s = String.format("Count is %d", count); MessageFormat style formatting s = MessageFormat.format( "At {1,time}, {0} eggs hatched.", 25, new Date()); Individual Numbers and Dates s = NumberFormat.getCurrencyInstance() .format(x); s = new SimpleDateFormat(""h:mm a"") .format(new Date()); s = new DecimalFormat("#,##0.00") .format(125.32); Source: http://cheatography.com/sschaub