Hi. I'm

Hello there! I'm Maxon, a dedicated UI/UX designer on a mission to transform digital experiences into intuitive, user-centric journeys.

Free Courses

html-icon css-icon javascript-icon c-language-icon c++-icon java-icon php-icon python-icon
Maxon

Java Cheatsheet

maxoncodes.online





Basics

Basic syntax and functions from the Java programming language.

Boilerplate

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

Showing Output

It will print something to the output console.

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

Taking Input

It will take string input from the user

import java.util.Scanner;
class HelloWorld
{
        public static void main(String args[])
        {
              Scannner sc=new Scanner(System.in);
              String name=sc.nextLine();
              System.out.println(name);
        }
}

It will take integer input from the user

import java.util.Scanner;
class HelloWorld
{
        public static void main(String args[])
        {
              Scannner sc=new Scanner(System.in);
              int x=sc.nextInt();
              System.out.println(x);
        }
}

It will take float input from the user

import java.util.Scanner;
class HelloWorld
{
        public static void main(String args[])
        {
              Scannner sc=new Scanner(System.in);
              int x=sc.nextFloat();
              System.out.println(x);
        }
}

It will take double input from the user

import java.util.Scanner;
class HelloWorld
{
        public static void main(String args[])
        {
              Scannner sc=new Scanner(System.in);
              double x=sc.nextDouble();
              System.out.println(x);
        }
}

Primitive Type Variables

The eight primitives defined in Java are int, byte, short, long, float, double, boolean, and char those aren't considered objects and represent raw values.

byte

byte is a primitive data type it only takes up 8 bits of memory.

class HelloWorld
{ 
       public static void main(String args[])
      {
            byte age=18;
            System.out.println(age);
      } 
}

long

long is another primitive data type related to integers. long takes up 64 bits of memory.

class HelloWorld
{ 
       public static void main(String args[])
      {
            long var=900.0;
            System.out.println(age);
      } 
}

float

We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. Which means if we get past six decimal points, this number becomes less precise and more of an estimate.

class HelloWorld
{ 
       public static void main(String args[])
      {
            float price=100.05;
            System.out.println(price);
      } 
}

char

Char is a 16-bit integer representing a Unicode-encoded character.

class HelloWorld
{ 
       public static void main(String args[])
      {
            char letter='A';
            System.out.println(letter);
      } 
}

int

int holds a wide range of non-fractional number values.

class HelloWorld
{ 
   public static void main(String args[])
   { 
        int var1=256;
        System.out.println(var1); 
    } 
}

short

If we want to save memory and byte is too small, we can use short.

class HelloWorld
{ 
   public static void main(String args[])
   { 
        short var2=5666;
        System.out.println(var2); 
   } 
}

Comments

A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.

Single line comment

// It's a single line comment

Multi-line comment

/* It's a 
multi-line
comment
*/

Constants

Constants are like a variable, except that their value never changes during program execution.

public class Declaration {

  final double PI = 3.14;

  public static void main(String[] args) {
    System.out.println("Value of PI:  " + PI);
  }
}

Arithmetic Expressions

These are the collection of literals and arithmetic operators.

Addition

It can be used to add two numbers

public class HelloWorld
{
    public static void main(String args[])
    {
          int x=10+3;
          System.out.println(x);
    }
}

Subtraction

It can be used to subtract two numbers

public class HelloWorld
{
    public static void main(String args[])
    {
          int x=10-3;
          System.out.println(x);
    }
}

Multiplication

It can be used to multiply add two numbers

public class HelloWorld
{
    public static void main(String args[])
    {
          int x=10*3;
          System.out.println(x);
    }
}

Division

It can be used to divide two numbers

public class HelloWorld
{
    public static void main(String args[])
    {
          int x=10/3;
          System.out.println(x);
    }
}

Modulo Remainder

It returns the remainder of the two numbers after division

public class HelloWorld
{
    public static void main(String args[])
    {
          int x=10%3;
          System.out.println(x);
    }
}

Augmented Operators

Addition assignment

public class HelloWorld
{
    public static void main(String args[])
    {
          var=1;
          var+=10;
          System.out.println(var);
    }
}

Subtraction assignment

public class HelloWorld
{
    public static void main(String args[])
    {
          var=1;
          var-=10;
          System.out.println(var);
    }
}

Multiplication assignment

public class HelloWorld
{
    public static void main(String args[])
    {
          var=1;
          var*=10;
          System.out.println(var);
    }
}

Division assignment

public class HelloWorld
{
    public static void main(String args[])
    {
          var=1;
          var/=10;
          System.out.println(var);
    }
}

Modulus assignment

public class HelloWorld
{
    public static void main(String args[])
    {
          var=1;
          var%=10;
          System.out.println(var);
    }
}

Escape Sequences

It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.

Tab

It gives a tab space

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\t");
   }
}

Backslash

It adds a backslash

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\\");
   }
}

Single quote

It adds a single quotation mark

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\'");
   }
}

Question mark

It adds a question mark

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\?");
   }
}

Carriage return

Inserts a carriage return in the text at this point.

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\r");
   }
}

Double quote

It adds a double quotation mark

public class HelloWorld
{
   public static void main(String args[])
   {
        System.out.print("\"");
   }
}

Type Casting

Type Casting is a process of converting one data type into another

Widening Type Casting

It means converting a lower data type into a higher

class HelloWorld
{
   public static void main(String args[])
   {
        int x = 45;
        double var_name = x;
        System.out.println(var_name);
   }
    
}

Narrowing Type Casting

It means converting a higher data type into a lower

class HelloWorld
{
   public static void main(String args[])
   {
        double x = 40005;
        int var_name = x;
        System.out.println(var_name);
   }
    
}

Decision Control Statements

Conditional statements are used to perform operations based on some condition.

if Statement

if (condition) {
// block of code to be executed if the condition is true
}

if-else Statement

if (condition) {
// If condition is True then this block will get executed
} else {
// If condition is False then this block will get executed
}

if else-if Statement

if (condition1) {
// Codes
}
else if(condition2) {
// Codes
}
else if (condition3) {
// Codes
}
else {
// Codes
}

Ternary Operator

It is shorthand of an if-else statement.

Syntax

variable = (condition) ? expressionTrue : expressionFalse;

Example

public class TernaryOperatorExample  
{  
  public static void main(String args[])   
  {  
    int x, y;  
    x = 20;  
    y = (x == 1) ? 61: 90;  
    System.out.println("Value of y is: " +  y);  
    y = (x == 20) ? 61: 90;  
    System.out.println("Value of y is: " + y);  
  }  
}

Switch Statements

It allows a variable to be tested for equality against a list of values (cases).

class SwitchExample
{
  public static void main(String args[])
  {
  int day = 4;
  switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
  }
}

Iterative Statements

Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the coder.

while Loop

It iterates the block of code as long as a specified condition is True

public class WhileExample
{  
  public static void main(String[] args) 
  {  
    int i=1;  
    while(i<=10)
    {  
        System.out.println(i);  
        i++;  
    }  
  }  
}  

for Loop

for loop is used to run a block of code several times

class HelloWorld
{
   public static void main(String args[])
   {
        int i;
        for(i=1;i<100;i++)
         {
              System.out.println(i);
         }
   }
    
}

for-each Loop

public class HelloWorld
{
   public static void main(String args[])
   {
     int