Show Menu
Cheatography

Java Quick Cheat Sheet Cheat Sheet (DRAFT) by

Java quick cheat sheet for a quick revision.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Data Types

Primitive Data Types
Type
Size
Range of values
boolean
1 bit
true, false
char
2 byte
'\u0000' to '\uffff'
byte
1 byte
-128 to 127
short
2 byte
-32,768 to 32,767
int
4 byte
- 2,147,­483,648 to 2,147,­483,647
long
8 byte
-9,223­,37­2,0­36,­854­,77­5,808 to 9,223,­372­,03­6,8­54,­775,807
float
4 byte
32-bit floating point
double
8 byte
64-but floating point
Non-Pr­imitive Data Types
String
 
It is a sequence of characters
Class
 
It is a user defined data type from which objects are created
Object
 
It is an instance of a class repres­enting real-life entities
Interface
 
It is similar to class but contains abstract methods by default
Array
 
It holds elements of similar data types

Classes and Objects

class Dog {
    String name;
    int age;
    
    public void info() {
        System.out.print(this.name + " is " + this.age + " years old");
    }
}


public class Main {
    public static void main(String[] args) {
        // Creating an object from the Dog class
        Dog myDog = new Dog();
        // Assigning values
        myDog.name = "Tommy";
        myDog.age = 3;
        // calling the method
        myDog.info(); // output: Tommy is 3 years old
    }
}
 

Comments

Single Line Comment
// this is a single line comment
Multiline Comment
/* This is a multiline comment */

Basic Program Structure

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Primitive Types

public class Main {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(a); // 10
        double b = 1021.0121;
        System.out.println(b); // 1021.0121
        float c = 11.001f;
        System.out.println(c); // 11.001
        boolean d = true;
        System.out.println(d); // true
        char e = 'P';
        System.out.println(e); // P
        String f = "Learning Java";
        System.out.println(f); // Learning Java
    }
}

Classes and Object Theory

A class in java is a user-d­efined data type. It is declared with the
class
keyword. It can have properties and methods.
Here in the example
name
and the
age
is the property and
info()
is the method.