Docsity

Last Minute Java Cheat Sheet: Key Concepts to Ace Java, Cheat Sheet of Java Programming

Toggle sidebar
bg1
Java Cheatsheet
Basics
Boilerplate
public → Access modifier (needed for JVM to call main ).
static → Allows JVM to call without creating an object.
void → Method returns nothing.
String[] args → Command-line arguments.
Showing Output
Taking Input (Scanner)
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
•
•
•
•
System.out.print("Hello"); // prints without newline
System.out.println(" World"); // prints with newline
System.out.printf("Age: %d",20); // formatted output
import java.util.Scanner;
class InputExample {
pf3
pf4
pf5
pf8

Partial preview of the text

Download Last Minute Java Cheat Sheet: Key Concepts to Ace Java and more Cheat Sheet Java Programming in PDF only on Docsity!

Java Cheatsheet

Basics

Boilerplate

public → Access modifier (needed for JVM to call main ).

static → Allows JVM to call without creating an object.

void → Method returns nothing.

String[] args → Command-line arguments.

Showing Output

Taking Input (Scanner)

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

System.out.print("Hello"); // prints without newline System.out.println(" World"); // prints with newline System.out.printf("Age: %d", 20); // formatted output

import java.util.Scanner;

class InputExample {

Primitive Types

Eight primitives: byte , short , int , long , float , double , boolean , char.

byte (8-bit, -128 to 127)

short (16-bit, -32,768 to 32,767)

int (32-bit, default for integers)

long (64-bit, append L for literals)

float (32-bit, append f )

double (64-bit, default for decimals)

boolean ( true or false )

char (16-bit Unicode)

Comments

public static void main(String[] args) { Scanner sc = new Scanner(System.in);

String name = sc.nextLine(); // String int age = sc.nextInt(); // int float f = sc.nextFloat(); // float double d = sc.nextDouble(); // double boolean b = sc.nextBoolean(); // boolean char c = sc.next().charAt(0); // char (first char of input)

System.out.println("Name: " + name);

// Single line /* Multi-line / /* Javadoc comment */

Escape Sequences

\n → newline

\t → tab

\ → backslash

' → single quote

" → double quote

\r → carriage return

\b → backspace

( ? is not valid in Java; use "?" literally.)

Type Casting

Widening (automatic)

Narrowing (manual)

  • • • • • • •

int x = 45; double y = x; // OK

double d = 45.9; int n = (int) d; // truncates decimal

Control Flow

if / else if / else

Ternary

switch (Java 7+ supports Strings)

Loops

while

if (x > 0) { ... } else if (x == 0) { ... } else { ... }

String result = (x > 0)? "Positive" : "Negative";

switch(day) { case 1: System.out.println("Mon"); break; ... default: System.out.println("Invalid"); }

while (i < 5) { i++; }

Methods

Method Overloading

Recursion

Strings

Strings are objects of java.lang.String.

static int add(int a, int b) { return a + b; }

public static void main(String[] args) { System.out.println(add(5, 10)); }

void sum(int a, int b) { ... } void sum(double a, double b) { ... }

int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); }

String s = "Hello"; int len = s.length(); s.toUpperCase(); s.toLowerCase(); s.indexOf("l"); s.contains("He");

Math Class

s.equals("Hello"); s.equalsIgnoreCase("hello"); s.substring(0, 3); // "Hel" s.replace("H", "J");

Math.max(5, 10); Math.min(5, 10); Math.sqrt(16); Math.pow(2, 3); Math.abs(-10); Math.random(); // [0.0, 1.0)