The main Method

public static void main(String[] args


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

Java Data Types

  1. Byte: Stores whole numbers from -128 to 127
  2. Short: Stores whole numbers from -32,768 to 32,767
  3. Int: Stores whole numbers from -2,147,483,648 to 2,147,483,647
  4. Long: Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  5. Float: Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
  6. Double: Stores fractional numbers. Sufficient for storing 15 decimal digits
  7. Boolean: Stores fractional numbers. Sufficient for storing 15 decimal digits
  8. Char: Stores a single character/letter or ASCII values
  9. String: Stores words

Java Operators

  1. + : adds values
  2. - : removes values
  3. * : multiplies values
  4. / : divides values
  5. % : Returns the division remainder
  6. ++ : Increases the value of a variable by 1
  7. — — : Decreases the value of a variable by 1

Java Switch

switch(expression) {
case x:
// code block
case y:
// code block
// code block


  • The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default ca


Java While Loop

while (condition) {
// code block to be executed

The Do/While Loop

do {
// code block to be executed
while (condition)

Java For Loop

for (statement 1; statement 2; statement 3) {
// code block to be executed

Java Break

Java Continue

Java Arrays

String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Java Methods

  1. To define the code once
  2. To use it many times

Create a Method

public class Main {
static void prosper(){
// code to be executed

Example Explained

  • prosper() is the name of the method
  • static means that the method belongs to the Main class and not an object of the Main class.
  • void means that this method does not have a return value. You have the option to remove this so as to return a value.

Call a Method

Java OOP

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

Java Classes/Objects

Create a Class

public class Main {
int x = 5;

Create an Object


public class Main {
int x = 5;

public static void main(String[] args) {
Main myObj = new Main();

Java Constructors

// Create a Main class
public class Main {
int x; // Create a class attribute

// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class attribute x

public static void main(String[] args) {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
System.out.println(myObj.x); // Print the value of x

// Outputs 5

Java Modifiers

  • Access Modifiers — controls the access level
  • Non-Access Modifiers — do not control access level, but provides other functionality

Access Modifiers

  1. Public :The class is accessible by any other class
  2. Default :The class is only accessible by classes in the same package. This is used when you don’t specify a modifier.
  1. Public :The class is accessible by any other class
  2. Private: The code is only accessible within the declared class
  3. Protected: The code is accessible in the same package and subclasses.

Non-Access Modifiers

  1. Final: The class cannot be inherited by other classes.
  2. Abstract :The class cannot be used to create objects (To access an abstract class, it must be inherited from another class. )
  1. Final: Attributes and methods cannot be overridden/modified
  2. Static: Attributes and methods belongs to the class, rather than an object
  3. Abstract: Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from).
  4. Transient: Attributes and methods are skipped when serializing the object containing them
  5. Synchronized: Methods can only be accessed by one thread at a time
  6. Volatile: The value of an attribute is not cached thread-locally, and is always read from the “main memory”

Java Encapsulation

  • declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

Get and Set

public class Person {
private String name; // private = restricted access

// Getter
public String getName() {
return name;

// Setter
public void setName(String newName) { = newName;

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium CopyCAT Launch

Increasing observability on Istio: The new Kiali health configuration

Turning black boxes into rainbows with Scala’s Enrich my Library Pattern

The District Weekly — December 12th

This tutorial will help you to setup the masternode on the Ubuntu 16 virtual private server and…

Git — how to stack multiple git branches and rebase them like a pro

From BFF to GraphQL: for a better service oriented architecture

How to use Deep Work to become a better programmer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Min-Max Sum | HackerRank Problem | Java Solution

Understanding Bubble Sort at grass root level — With examples that illustrate all edge cases

Repeating and Missing number in array. Brute Force to efficient approach.

Maximum Area of Triangle → Interview Bit Arrays Problem