Cheatography
https://cheatography.com
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 < <value>; ++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;
} |
|
|
Defining Methods
How to define any method in a class? A class method can be declared/defined using the syntax: public|private [static] <return_type> <method_name>([arguments])
|
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 instantiating the object. So first create an object from a class using <class_name> <obj_name> = new <class_name>() and then call the method with this object. Ex. Scanner scanner = new Scanner(System.in); int number = scanner.nextInt();
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by priyasadhale