Skip to content

Instantly share code, notes, and snippets.

@PSCoder10462
Created July 16, 2023 13:11
Show Gist options
  • Save PSCoder10462/8659208e7d3dff56df504a205fcdacbd to your computer and use it in GitHub Desktop.
Save PSCoder10462/8659208e7d3dff56df504a205fcdacbd to your computer and use it in GitHub Desktop.

JAVA

DATA TYPES

PRIMITIVE

  • byte
  • short
  • int
  • long
    • %d
  • float
  • double
    • %f
  • char
    • %c
  • boolean
    • %b

%s is used for String, but its not a primitive datatype.

System.out.printf() can be used or store in a string directly using String.format(<string>, <variables>).

REFERENCED (shallow copy)

  • Eg. Date, Point

TYPE CONVERSION

  • long 123L
  • float 1.3F
  • (double)10 is explicit casting

ARRAYS

SINGLE-DIMENSIONAL

  • int[] arr = new int[5]

MULTI-DIMENSIONAL

  • int[][] arr = new int[2][3]

CONSTANTS

  • use final keyword
  • uppercase convention

ORDER OF OPERATIONS

  1. ()
  2. /, *
  3. +, -

STRING TO NUMBER

  • Integer.parseInt()
  • Float.parseFloat()
  • etc.

INPUT (SCANNER)

Scanner scanner = new Scanner(System.in);
// now you can use scanner.next<datatype>
byte age = scanner.nextByte();
// single token
String firstName = scanner.next();
// with whitespaces
String fullName = scanner.nextLine();
// ...
scanner.close();

LOOPS

  • for
  • while
  • do-while
  • for-each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment