Table Of Contents
Java Programming Cheatsheet
Java is one of the most popular, most adopted and general purpose programming language used by millions of developers and billions of devices around the world. It is a class-based, object-oriented language and designed to be portable, which means that you can find it on all platforms, operating systems, and devices. It is used to develop all kinds of Android apps, desktop apps, and video games. It is also commonly used as a server-side language for enterprise-level back end development. This programming language has long-term compatibility and developers are comfortable with Java.
Cheat-sheet for Java Programming is given below:
Java Data Types
byte / short / int / long
|
10,-30 |
float / double
|
88.58 |
char
|
‘D’
|
boolean
|
true, false
|
String
|
“ยญKeep Following Techworm”
|
Java Data Converยญsions
String to Number int i = Integeยญr.pยญarsยญeInยญt(ยญstr); double d = Double.paยญrseยญDouยญbleยญ(sยญtr); |
Any Type to String String s = String.vaยญlueยญOf(ยญvaยญlue); |
Numeric Converยญsions int i = (int) |
Java Arithmetic Operators
x + y
|
add
|
x – y
|
subtract
|
x * y
|
multiply
|
x / y
|
divide
|
x % y
|
modulus
|
++x / x++
|
increment
|
–x / x–
|
decrement
|
Java Comparison Operators
x < y
|
Less
|
x <= y
|
Less or eq
|
x > y
|
Greater
|
x >= y
|
Greater or eq
|
x == y
|
Equal
|
x != y
|
Not equal
|
Java Boolean Operators
! x (not)
|
x && y (and)
|
x || y (or)
|
Java Text Formatting
printf style formatยญting System.ouยญt.pยญrinยญtf(ยญ”ยญCount is %d\n”, count); s = String.foยญrmaยญt(“Count is %d”, count);MessยญageยญFormat style formatยญting s = MessagยญeFoยญrmaยญt.fยญormat( ยญย ยญ”At {1,time}, {0} Runs Scored.”, ยญย 25, new Date());Indiยญvidual Numbers and Dates s = NumberยญForยญmat.geยญtCuยญrreยญncyยญ() ยญย .foยญrmaยญt(x); s = new SimpleยญDatยญeFoยญrmaยญt(“”yyyy/mm/dd””) ยญย .foยญrmaยญt(new Date()); |
Java Hello World
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } |
Java String Methods
s.leยญngth()
|
length of s
|
s.chยญarAยญt(i)
|
extract ith character
|
s.suยญbstยญrinยญg(ยญstart, end)
|
substring from start to end-1
|
s.toยญUppยญerCยญase()
|
returns copy of s in ALL CAPS
|
s.toยญLowยญerCยญase()
|
returns copy of s in lowercase
|
s.inยญdexยญOf(x)
|
index of first occurence of x
|
s.reยญplaยญce(ยญold, new)
|
search and replace
|
s.spยญlitยญ(rยญegex)
|
splits string into tokens
|
s.trim()
|
trims surrouยญnding whitespace
|
s.eqยญualยญs(s2)
|
true if s equals s2
|
s.coยญmpaยญreTยญo(s2)
|
0 if equal/+ if s > s2/- if s < s2
|
Java Statements
If Statemยญent
if ( expreยญssion ) { ยญย ยญstยญateยญments } else if ( expreยญssion ) { ยญย ยญstยญateยญments } else { ยญย ยญstยญateยญments }While Loop while ( expreยญssion ) { ยญย ยญstยญateยญments }Do-While Loop do { ยญย ยญstยญateยญments } while ( expreยญssion );For Loop for ( int i = 0; i < max; ++i) { ยญย ยญstยญateยญments }For Each Loop for ( var : colleยญction ) { ยญย ยญstยญateยญments }Switch Statemยญent switch ( expreยญssion ) { ยญย case value: ยญย ยญย ยญย ยญstยญateยญments ยญย ยญย ยญย ยญbreak; ยญย case value2: ยญย ยญย ยญย ยญstยญateยญments ยญย ยญย ยญย ยญbreak; ยญย ยญdefยญault: ยญย ยญย ยญย ยญstยญateยญments }Exceยญption Handling try { ยญย ยญstaยญtemยญents; } catch (ExceยญptiยญonType e1) { ยญย ยญstaยญtemยญents; } catch (Exception e2) { ยญย ยญcatยญch-all statemยญents; } finally { ยญย ยญstaยญtemยญents; } |