SlideShare a Scribd company logo
1 of 1
Download to read offline
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

What's hot

Dart ( 2 )
Dart ( 2 )Dart ( 2 )
Dart ( 2 )
Simplife EG
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
Pierre de Lacaze
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 
Dart ( 1 )
Dart ( 1 )Dart ( 1 )
Dart ( 1 )
Simplife EG
 

What's hot (20)

Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
 
Why rust?
Why rust?Why rust?
Why rust?
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
 
Support de cours Spring M.youssfi
Support de cours Spring  M.youssfiSupport de cours Spring  M.youssfi
Support de cours Spring M.youssfi
 
Rust Primer
Rust PrimerRust Primer
Rust Primer
 
Dart ( 2 )
Dart ( 2 )Dart ( 2 )
Dart ( 2 )
 
Support JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVCSupport JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVC
 
Hexagonal architecture & Elixir
Hexagonal architecture & ElixirHexagonal architecture & Elixir
Hexagonal architecture & Elixir
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
 
ORM Injection
ORM InjectionORM Injection
ORM Injection
 
Resource Aware Scheduling in Apache Storm
Resource Aware Scheduling in Apache StormResource Aware Scheduling in Apache Storm
Resource Aware Scheduling in Apache Storm
 
Les Streams de Java 8
Les Streams de Java 8Les Streams de Java 8
Les Streams de Java 8
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)
 
All You Need to Know About Java – Advantages and Disadvantages
All You Need to Know About Java – Advantages and DisadvantagesAll You Need to Know About Java – Advantages and Disadvantages
All You Need to Know About Java – Advantages and Disadvantages
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Dart ( 1 )
Dart ( 1 )Dart ( 1 )
Dart ( 1 )
 

Viewers also liked

jquery cheat sheet
jquery cheat sheetjquery cheat sheet
jquery cheat sheet
johnnytomcat
 

Viewers also liked (18)

Java cheat sheet
Java cheat sheetJava cheat sheet
Java cheat sheet
 
Cheat sheet - String Java (Referência rápida)
Cheat sheet - String Java (Referência rápida)Cheat sheet - String Java (Referência rápida)
Cheat sheet - String Java (Referência rápida)
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
MySQL Cheat Sheet
MySQL Cheat SheetMySQL Cheat Sheet
MySQL Cheat Sheet
 
Java for beginners
Java for beginnersJava for beginners
Java for beginners
 
REST: putting the web back in to web services
REST: putting the web back in to web servicesREST: putting the web back in to web services
REST: putting the web back in to web services
 
Goal4d
Goal4dGoal4d
Goal4d
 
jquery cheat sheet
jquery cheat sheetjquery cheat sheet
jquery cheat sheet
 
Bash Cheat Sheet -- Bash History Cheat Sheet
Bash Cheat Sheet -- Bash History Cheat SheetBash Cheat Sheet -- Bash History Cheat Sheet
Bash Cheat Sheet -- Bash History Cheat Sheet
 
Developing Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginnersDeveloping Applications with MySQL and Java for beginners
Developing Applications with MySQL and Java for beginners
 
Web Components Revolution
Web Components RevolutionWeb Components Revolution
Web Components Revolution
 
Java cheat sheet
Java cheat sheet Java cheat sheet
Java cheat sheet
 
Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!
Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!
Mastering your Eclipse IDE - Tips, Tricks, Java 8 tooling & More!
 
24 Books You've Never Heard Of - But Will Change Your Life
24 Books You've Never Heard Of - But Will Change Your Life24 Books You've Never Heard Of - But Will Change Your Life
24 Books You've Never Heard Of - But Will Change Your Life
 
20 Quotes To Turn Your Obstacles Into Opportunities
20 Quotes To Turn Your Obstacles Into Opportunities20 Quotes To Turn Your Obstacles Into Opportunities
20 Quotes To Turn Your Obstacles Into Opportunities
 
Learn Java 3D
Learn Java 3D Learn Java 3D
Learn Java 3D
 
Work Rules!
Work Rules!Work Rules!
Work Rules!
 
Cybercrime presentation
Cybercrime presentationCybercrime presentation
Cybercrime presentation
 

Similar to Java Cheat Sheet

pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
parag978978
 

Similar to Java Cheat Sheet (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala
ScalaScala
Scala
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
core java
 core java core java
core java
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 

More from Saeid Zebardast

More from Saeid Zebardast (9)

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

Recently uploaded

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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: 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