Repository navigation
Java
Ravi Eragapati edited this page Oct 12, 2026
·
6 revisions
- java.lang -> String, StringBuilder, StringBuffer (synchronized, slow), Math
- java.io -> File, BufferedInputStream, BufferedReader
- java.math -> BigDeciman, BigInteger, RoundingMode
- java.util -> Collections, ArrayList, HashMap, LinkedList, UUID, TimeZone, Objects
ASCII values:
-
a-z= 97 - 122 -
A-Z= 65 - 90 -
0-9= 48 - 57
var visited = new int[128];
for(int i=0; i<128;i++){
visited[i] = -1;
}
System.out.println("Has char r been visited?" + visited['r']);
System.out.println("ASCII value of char a is: " + (int) Character.valueOf('a'));-
Implicit (Widening) Casting:
AutomaticWhen converting a smaller primitive type to a larger primitive type (e.g., int to long). No data loss. -
Explicit (Narrowing) Casting:
ManualWhen converting a larger primitive type to a smaller type (e.g., double to int). Requires explicit casting syntax (targetType) and can result in data loss.
Order of Size (Narrow to Wide): byte > short > char > int > long > float > double
String Parses static methods:
- Integer.parseInt(String str) -> int;
- Long.parseLong(String str) -> long;
- Double.parseDouble(String str) -> double;
- Float.parseFloat(String str) -> float;
-
Reverse string: Use
StringBuilder.reverse();StringBufferis thread-safe and for that reason its slow. or you can also use pointer from length() to 0.
Useful String methods:
- str.toCharArray() -> char[]
- str.indexOf("m") -> int
- str.indexOf('m', 1) -> int
- str.indexOf('m', 1, 24) -> int
- str.lastIndexOf('m'); -> int
- str.startsWith("abc") -> boolean
- str.endsWith("xyz") -> boolean
- str.charAt(5)
- str.codePointAt(0) - returns ascii value of char at that index a-z = 97 - 122; A-Z = 65 - 90; 0-9 = 48 - 57
- str.toUpperCase(); -> String
- str.toLowerCase(); -> String
- str.length(); -> int
- str.hashcode();
- str.substring(int beginingIndex) -> String
- str.substring(int beginIndex, int endIndex) -> String
- str.concat("xyz") -> String
- str.replace("abc", "xyz") -> String
- str.replaceFirst(String regex, String replacement) -> String
- str.replaceAll(String regex, String replacement) -> String
- str.contains("abc") -> bool
- str.matches(String regex) -> bool
- str.isEmpty() -> bool " " -> false
- str.isBlank() -> " " -> true
- str.split(String regex, int limit) -> String[]
- str.split(String regex) -> String[]
- str.trim() -> removes spaces
- str.strip() -> removes spaces, even the unicode spaces like \u2000 (space)
- Math.random()
- Math.max(a,b)
- Math.min(a,b)
- Math.abs(-1) = 1
- Math.pow(a,b) = a^b
- Math.log()
- Math.log10()
- Math.sqrt(4) = 2
- Math.cbrt(9) = 3
- Math.round(10.45) = 10
- Math.floot(20.6) = 20
- Math.ceil(20.1) = 21
Read File:
Write File: