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; } |