Ultimate Java Programming Cheatsheet

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;
}
Darshan Savla
Darshan Savlahttps://www.techworm.net
Look at the sky. We are not alone. The whole universe is friendly to us and conspires only to give the best to those who dream and work.

3 COMMENTS

  1. I love the cheat sheet. I find it very interesting. It presents a whole Java text. I have read several Java text but this is a template for people like us. Complete it showing us how each is used, when to use and how to use them

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Read More

Suggested Post