Skip to content

Instantly share code, notes, and snippets.

@Mirakurun
Last active January 16, 2023 01:09
Show Gist options
  • Save Mirakurun/dd3575feac6214a65930313082b38dd4 to your computer and use it in GitHub Desktop.
Save Mirakurun/dd3575feac6214a65930313082b38dd4 to your computer and use it in GitHub Desktop.
Java Notes

☕ Java Notes

📑 Table of Contents

  1. Variables and Data Types
    1. Type-casting
    2. Static Variables
  2. Scanner
    1. nextLine Trap
  3. Strings
    1. charAt
    2. Concat
    3. Equals
    4. isBlank
    5. isEmpty
    6. Replace
    7. Strip
    8. toLowerCase
    9. toUpperCase
  4. Conditionals
    1. If...else
    2. Else if
    3. Ternary Operator
    4. Switch
  5. Arrays
  6. Methods (Functions)
    1. Static Methods
    2. Varargs
  7. Classes
    1. Static Initializers
    2. Constructors
    3. Getters
    4. Setters
    5. StringBuilder
      1. Constructor
      2. Methods
        1. Append
        2. toString

Variables and Data Types

String name = "Kevin";
char initial = "K"; // stores a single character
byte hundred = 100; // whole numbers from -128 to 127
short thousand = 1000; // whole numbers from -32,768 to 32,767
int million = 1000000; // whole numbers from -2,147,483,647 to 2,147,483,647
long globalPopulation = 7000000000L; // whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float dollar = 1.00f; // fractional numbers with 6 to 7 decimal digits
final double PI = 3.14159265359; // fractional numbers with 15 decimal digits
boolean isJavaFun = true;

Type-casting

double decimal = 4.3;
int integer = (int)decimal;

static Variables

A static variable is shared between all instances of that class. Can be called without having an instance of the class created.

public class Main {
  public static String name = "Kevin";

  public static void main(String[] args) {
    // Note: For static variables, no need to intialize the Class Main to access the variable
    // Main main = new Main();
    System.out.println(name); // Kevin
  }
}

Scanner

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    // Initiate a Scanner class
    Scanner scanner = new Scanner(System.in);

    System.out.println("Write a message: ");

    // Read user input
    String message = scanner.nextLine();

    // Output user input
    System.out.println(message);

    scanner.close();
  }
}

nextLine Trap

int num = scanner.nextInt();

scanner.nextLine();

String message = scanner.nextLine();

Strings

charAt

String str = "Hello";

System.out.println(str.charAt(1)); // e

concat

String str1 = "Hello";
String str2 = "World";

System.out.println(str1 + str2); // HelloWorld

contains

String str = "Hello";

System.out.println(str.contains("Hello")); // true

equals

String str1 = "Hello";
String str2 = "Hello";
String str3 = "Hello World";

System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // false

isBlank

String str1 = "";
String str2 = " ";
String str3 = "a";

System.out.println(str1.isBlank()); // true
System.out.println(str2.isBlank()); // true
System.out.println(str3.isBlank()); // false

isEmpty

String str1 = "";
String str2 = " ";
String str3 = "a";

System.out.println(str1.isBlank()); // true
System.out.println(str2.isBlank()); // false
System.out.println(str3.isBlank()); // false

length

String str = "Hello";

System.out.println(str.length()); // 5

replace

String str = "Hello";

System.out.println(str.replace("Hello", "Greetings")); // Greetings

strip

String str = "   Hello   ";

System.out.println(str.strip()); // Hello

substring

String str1 = "Hello";
String str2 = str1.substring(1);
String str3 = str1.substring(1, 2);

System.out.println(str2); // ello
System.out.println(str2); // e

toLowerCase

String str = "ABC";

System.out.println(str.toLowerCase()); // "abc"

toUpperCase

String str = "abc";

System.out.println(str.toUpperCase()); // "ABC"

Conditionals

If...else

int time = 20;

if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Good evening.

else if

int time = 22;

if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 20) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Good evening.

Ternary Operator

int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";

System.out.println(result); // Good day.

switch

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;
}
// Thursday

Arrays

int[] nums = {10, 20, 30, 40, 50};

System.out.println(nums.length); // 5
System.out.println(nums[1]); // 20

Methods (Functions)

static Methods

static means that the method belongs to the Main class and not an object of the Main class.

public class Main {
  public static void helloWorld() {
    System.out.println("Hello World");
  }

  public static void main(String[] args) {
    // Note: For static methods, no need to intialize the Class Main to call the method
    // Main main = new Main();
    helloWorld(); // Hello World
  }
}

Varargs

public String formatWithVarArgs(String... values) {
  // ...
}

formatWithVarArgs("a", "b", "c", "d");

Classes

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

static Initializers

public class HelloWorld {
  public static int[] nums = initNums();

  public static int[] initNums() {
    int[] nums = {3, 3, 3, 3, 3};
    return nums;
  }
}

Constructors

public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public static void main(String[] args) {
    Person p1 = new Person("Kevin", "Chhay"); 
    System.out.println(p1.firstName); // "Kevin"
    System.out.println(p1.lastName); // "Chhay"
  }
}

Getters

public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String getFullName() {
    return firstName + " " + lastName;
  }

  public static void main(String[] args) {
    Person p1 = new Person("Kevin", "Chhay");
    System.out.println(p1.getFullName()); // "Kevin Chhay"
  }
}

Setters

public class Person {
  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String getFullName() {
    return firstName + " " + lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName.substring(0, 1).toUpperCase() + lastName.subString(1);
  }

  public static void main(String[] args) {
    Person p1 = new Person("Kevin", "chhay");
    System.out.println(p1.getFullName()); // "Kevin chhay"
    p1.setLastName("chhay");
    System.out.println(p1.getFullName()); // "Kevin Chhay"
  }
}

StringBuilder

Constructor

// Default capacity is 16
String text = new StringBuilder();

Methods

append

String str = "Hello";
StringBuilder builder = new StringBuilder().append(str);

toString

String str = "Hello";
String text = new StringBuilder().append(str).toString();

System.out.println(text); // Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment