Intro to Java Cheat Sheet
Reserved Words
| Java Reserved Words |
| abstract |
boolean |
break |
byte |
case |
catch |
clear |
class |
continue |
default |
| do |
double |
else |
extends |
final |
finally |
float |
for |
if |
implements |
| import |
instanceof |
int |
interface |
long |
native |
new |
package |
private |
protected |
| public |
return |
short |
static |
strictfp |
super |
switch |
synchronized |
this |
throw |
| throws |
transient |
try |
void |
volatile |
while |
const |
goto |
null |
true |
| false |
rest |
cast |
generic |
outer |
inner |
future |
byvalue |
var |
|
Data Types
| Primitives |
| Type |
Size (bits) |
Size (bytes) |
Values |
Example |
| byte |
8 bits |
1 byte |
-128 to 127 |
byte b = 12; |
| short |
16 bits |
2 bytes |
-32768 to 32767 |
short sh = 1000; |
| int |
32 bits |
4 bytes |
-2**31 to 2**31 - 1 |
int i = 45900; |
| long |
64 bits |
8 bytes |
2**63 to 2**63 - 1 |
long l = 1000000; |
| float |
32 bits |
4 bytes |
big |
float f = 12.34; |
| double |
64 bits |
8 bytes |
really big |
double d = 1.7e40; |
| char |
16 bits |
2 bytes |
Unicode characters |
char c = 'a'; |
| boolean |
8 bits |
1 byte |
true or false |
boolean done = false; |
Flow Control Statements
The if Statement
if ( a < b )
{
System.out.println ( a );
}
else
{
System.out.println ( b );
}
The for Loop
for ( int i = 0; i < 10; i++ )
{
System.out.println ( i );
}
The switch-case Statement
int i = ??????
switch ( i )
{
case 12:
System.out.println ( "It's twelve!" );
break;
default:
System.out.println ( "It's something else!" );
break;
}
Using Classes
Defining a Class
Filename: Student.java
public class Student extends Object
{
private int studentID;
private String name;
public Student ( int id, String n ) // constructor
{
studentID = id;
name = n;
}
public String toString()
{
return "Name: " + name + " ID: " + studentID;
}
}
Using a Class
Filename: TestStudent.java
public class TestStudent
{
public static void main (String args[])
{
Student s = new Student ( 12, "Bill" );
System.out.println ( s );
}
}
Compiling and Running
javac Student.java
javac TestStudent.java
java TestStudent