Show Menu
Cheatography

Java Fundamentals Cheat Sheet by

Find the basics of Java

Java Data Types

byte / short / int / long
-123, 10
float / double
235.13
char
'a', 'A', '$', '-'
boolean
true, false
String
'Alice', 'A234t'
 

Java Control Structures

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 < <va­lue­>; ++i) {
­ statements
}

For Each Loop
for ( var : collection ) {
­ statements
}

Switch Statement
switch ( expression ) {
­ case value:
­ ­ ­ statements
­ ­ ­ ­break;
­ case value2:
­ ­ ­ statements
­ ­ ­ ­break;
­ ­def­ault:
­ ­ ­ statements
}

Exception Handling
try {
­ ­sta­t­em­­ents;
} catch (Excep­t­i­onType e1) {
­ ­sta­t­em­­ents;
} catch (Exception e2) {
­ ­cat­c­h-all statem­­ents;
} finally {
­ ­sta­t­em­­ents;
}
 

Defining Methods

How to define any method in a class?
A class method can be declar­ed/­defined using the syntax: public­|pr­ivate [static] <re­tur­n_t­ype> <me­tho­d_n­ame­>([­arg­ume­nts])
How to call a static method?
Static methods are also called as class methods and they can be called directly without creating any instance.
How to call any instance method?
Instance or non-static method can only be called after instan­tiating the object. So first create an object from a class using <cl­ass­_na­me> <ob­j_n­ame> = new <cl­ass­_na­me>() and then call the method with this object. Ex.
Scanner scanner = new Scanne­r(S­yst­em.in); 
int number = scanne­r.n­ext­Int();
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Eclipse Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet

          More Cheat Sheets by priyasadhale

          ChatGPT Prompt Guide Cheat Sheet