Java Programming Cheat sheet
compiled by Gregg Roeten
compiled by Gregg Roeten ........................................................................................................................ 1
Hello, World.............................................................................................................................................. 2
Editing, compiling and executing. .............................................................................................................. 2
Built-in data types. .................................................................................................................................... 2
Declaration and assignment statements. ................................................................................................... 2
Access Modifiers ........................................................................................................................................... 4
Class level access modifiers table: .................................................................................................................. 4
Access Modifiers ........................................................................................................................................... 4
Member level - Method, function, interface .................................................................................................. 4
Access Modifiers ........................................................................................................................................... 4
Non-Access Modifiers .................................................................................................................................... 4
Class declaration ........................................................................................................................................... 4
Method ......................................................................................................................................................... 5
Interface ....................................................................................................................................................... 5
Constructor ................................................................................................................................................... 5
Every class has a constructor. Explicitly or implicitly. @ new obj created a constructor is invoked. ................ 5
Constructor Overloading - Multiple Constructors for a Java Class ................................................................... 5
Overloading .................................................................................................................................................. 6
Overriding ..................................................................................................................................................... 6
Create File File.createTempFile() a method of File class ........................................................................... 6
Read File readLine method of BufferedReader class. ............................................................... 6
Write File write method of BufferedWriter. ................................................................................................ 6
Floating-point numbers............................................................................................................................. 7
Booleans................................................................................................................................................... 7
Comparison operators. ............................................................................................................................. 8
Parsing command-line arguments. ............................................................................................................ 8
Math library. ............................................................................................................................................. 8
Type conversion. ....................................................................................................................................... 9
If and if-else statements. ........................................................................................................................... 9
Nested if-else statement. .......................................................................................................................... 9
While and for loops................................................................................................................................. 11
Break statement. .................................................................................................................................... 11
Do-while loop. ........................................................................................................................................ 11
Switch statement. ................................................................................................................................... 11
Arrays. .................................................................................................................................................... 13
Compile-time initialization. ..................................................................................................................... 13
Typical array-processing code. ................................................................................................................. 13
Two-dimensional arrays. ......................................................................................................................... 13
Compile-time initialization. ..................................................................................................................... 14
Our standard output library..................................................................................................................... 14
Our standard input library. ...................................................................................................................... 16
Our standard drawing library. .................................................................................................................. 16
Our standard audio library....................................................................................................................... 16
Redirection and piping. ........................................................................................................................... 18
Functions. ............................................................................................................................................... 18
Libraries of functions. ............................................................................................................................. 19
1
Our standard random library. .................................................................................................................. 20
Our standard statistics library. ................................................................................................................. 20
Using an object. ...................................................................................................................................... 22
Creating an object................................................................................................................................... 22
Object-oriented libraries. ........................................................................................................................ 23
Java's String data type. ............................................................................................................................ 24
Java's Color data type.............................................................................................................................. 25
Our input library. .................................................................................................................................... 25
Our output library. .................................................................................................................................. 25
Our picture library. .................................................................................................................................. 26
Hello, World.
Editing, compiling and executing.
Built-in data types.
Declaration and assignment statements.
2
3
Access Modifiers
Class level access modifiers table:
Other packages, i.e.
Same Package
World
Access Modifiers
public Y Y
nothing Y N
Member level - Method, function, interface
Other packages, i.e.
Same Class Same Package Subclass
World
Access Modifiers
public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N
Non-Access Modifiers
static, final, abstract,
synchronized, volatile for threads
Class declaration
public final class Grclass
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement
4
Method
public static int funcName(int a, int b) {
// body
}
public static : modifier.
int: return type
funcName: function name
a, b: formal parameters
int a, int b: list of parameters
Methods pass Parameters by Value
Method overloading same overloading rules apply same name, parameters type diff or # diff
Interface
Use when expect that unrelated classes would implement your interface. OR
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
/* File name : NameOfInterface.java */
import java.lang.*;
public interface NameOfInterface //interface is implicitly abstract. You do
//Any number of final, static fields // not need to use the abstract keyword
when
//Any number of abstract method decls // declaring an interface.
{
Constructor
Every class has a constructor. Explicitly or implicitly. @ new obj created a
constructor is invoked.
public class MyClass { // class that the constructor belongs to.
public MyClass() { // constructor , has no return type CAN take parameters
// Body of constructor, this one is an empty constructor
Constructor Overloading - Multiple Constructors for a Java Class
A class can have multiple constructors, as long as their signatures (the parameters they take) are not the same.
public class MyClass {
private int number = 0;
public MyClass() {
}
5
public MyClass(int theNumber) {
this.number = theNumber;
Overloading
Single Class AND (number of parameters is different OR parameter types are different )
public void show(String message){
public void show(String message, boolean show){ OK # of arg is diff
public void show(Integer message){ OK type of arg is diff
Overriding
public class Animal{
public class Dog extends Animal{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); //Runs the method in Dog class
Create File File.createTempFile() a method of File class
import java.io.File;
File.createTempFile()
File file = null;
File dir = new File("C:/");
file = File.createTempFile("JavaTemp", ".javatemp", dir);
Read File readLine method of BufferedReader class.
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new FileReader("c:\\filename"));
String str;
while ((str = in.readLine()) != null) {
Write File write method of BufferedWriter.
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString"
Integers.
6
Floating-point numbers.
Booleans.
7
Comparison operators.
Parsing command-line arguments.
Math library.
8
Type conversion.
If and if-else statements.
Nested if-else statement.
9
10
While and for loops.
Break statement.
Do-while loop.
Switch statement.
11
12
Arrays.
Compile-time initialization.
Typical array-processing code.
Two-dimensional arrays.
13
Compile-time initialization.
Ragged arrays.
Our standard output library.
14
15
Our standard input library.
Our standard drawing library.
Our standard audio library.
16
17
Redirection and piping.
Functions.
18
Libraries of functions.
19
Our standard random library.
Our standard statistics library.
20
21
Using an object.
Creating an object.
Instance variables.
Constructors.
Instance methods.
22
Classes
Object-oriented libraries.
23
Java's String data type.
24
Java's Color data type.
Our input library.
Our output library.
25
Our picture library.
http://www.javagenious.com/interview/java-interview-questions-and-answers-part-1.html
26