[Go to site: main page, start]

0% found this document useful (0 votes)
12 views2 pages

Java Inbuilt Methods Overview

Uploaded by

senthil abi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Java Inbuilt Methods Overview

Uploaded by

senthil abi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Inbuilt Methods Cheat Sheet

String Class Methods

length() - Returns length of string


charAt(i) - Returns character at index i
codePointAt(i) - Unicode of char at index i
compareTo(str) - Lexicographical comparison
compareToIgnoreCase(str) - Case-insensitive comparison
concat(str) - Joins string
contains(str) - Checks if substring exists
contentEquals(str) - Checks exact content match
endsWith(str) - Checks if string ends with str
equals(str) - Compares strings
equalsIgnoreCase(str) - Case-insensitive equality check
format() - Returns formatted string
indexOf(char) - First occurrence index
intern() - Returns canonical representation
isEmpty() - Checks if string is empty
lastIndexOf(char) - Last occurrence index
replace(a, b) - Replace all a with b
replaceAll(regex, str) - Replaces all regex matches
replaceFirst(regex, str) - Replaces first regex match
split(delim) - Splits string
startsWith(str) - Checks if string starts with str
substring(i, j) - Substring from i to j
toCharArray() - Converts to char array
toLowerCase() - Converts to lowercase
toUpperCase() - Converts to uppercase
trim() - Removes leading/trailing spaces
valueOf(x) - Converts x to string

Integer Class Methods

parseInt(str) - Converts string to int


valueOf(str) - Returns Integer object
toString(i) - Converts int to string
compare(a, b) - Compares two ints
max(a, b) - Returns max of two ints
min(a, b) - Returns min of two ints
sum(a, b) - Returns sum of two ints
bitCount(i) - Number of 1 bits in binary
toBinaryString(i) - Returns binary representation
Java Inbuilt Methods Cheat Sheet

Character Class Methods

isLetter(ch) - Checks if character is a letter


isDigit(ch) - Checks if character is a digit
isUpperCase(ch) - Checks if character is uppercase
toLowerCase(ch) - Converts char to lowercase

Arrays Class Methods

sort(arr) - Sorts array


binarySearch(arr, key) - Searches key
fill(arr, val) - Fills array with value
equals(arr1, arr2) - Compares arrays
toString(arr) - Converts array to string

Common questions

Powered by AI

Java's Arrays class offers sort() and binarySearch() methods that can optimize the storage and searching of a large array of numbers. The sort() method can be applied to arrange the numbers in ascending order, which is necessary for binarySearch() to function correctly . Once sorted, binarySearch() significantly reduces search time complexity from O(n) to O(log n) by dividing the array into halves to quickly locate the desired value . This combination of methods efficiently manages large datasets by optimizing search operations.

The intern() method in the Java String class returns a canonical representation for the string object. This means it can be used to ensure that all strings with the same content share a single instance in the memory pool, thus saving memory in applications with many identical string literals . It helps solve the problem of unnecessary memory overhead by preventing the creation of multiple instances of equivalent strings.

The Java String class provides two methods to compare strings for equality: equals() and equalsIgnoreCase(). The equals() method performs a case-sensitive comparison, meaning that 'abc' and 'ABC' would not be considered equal . On the other hand, the equalsIgnoreCase() method performs a case-insensitive comparison, treating 'abc' and 'ABC' as equal .

The fill() method in Java's Arrays class offers the benefit of easily initializing all elements of an array with a given value, reducing boilerplate code and potential human error from manually setting each element . This method is particularly useful for setting default values across an array, such as initializing all elements to a specific number or ensuring a uniform initial state before further operations are performed.

The replace() method replaces all occurrences of a character sequence with another character sequence. The replaceAll() method uses regex to replace all occurrences of a pattern with a specified string. The replaceFirst() method is similar to replaceAll() but only replaces the first occurrence of the matched regex pattern with the specified string . Replace() is useful for simple character swaps; replaceAll() is ideal when working with patterns, such as sanitizing input by removing non-alphanumeric characters; and replaceFirst() can be applied when only the first match needs replacing, such as changing the initial occurrence of a misspelled word.

The isEmpty() method in the Java String class checks if a string has a length of zero and returns a boolean value, true if the string is empty and false otherwise . The length() method returns an integer representing the number of characters in the string, and it does not check for any emptiness directly . isEmpty() provides a more direct check for emptiness, while length() is useful for getting the size of the string.

The compareTo() method in the Java String class is used for lexicographical comparison between two strings, returning a positive, negative, or zero integer based on whether the invoking string is greater than, less than, or equal to the specified string . The compare() method in the Integer class compares two integer values and also returns a positive, negative, or zero integer based on their comparison . CompareTo() is typically used for sorting or ordering strings, whereas compare() can be used for comparing numerical values for sorting or other logical conditions.

The binarySearch() method in the Java Arrays class improves algorithm efficiency compared to a linear search by leveraging the sorted nature of the array to perform a divide-and-conquer approach. While a linear search scans each element sequentially, resulting in O(n) time complexity, binarySearch() divides the search interval in half repeatedly, leading to O(log n) time complexity . This allows for much faster searching, especially beneficial for large datasets.

The parseInt() method in Java's Integer class converts a string to a primitive int type, whereas the valueOf() method converts a string to an Integer object . The main difference lies in their return types: parseInt() returns int, while valueOf() returns an Integer object.

The methods toLowerCase() and toUpperCase() in Java's String and Character classes are used for converting characters or entire strings to uniform case, which is crucial for data normalization. This process helps ensure data consistency, especially in scenarios where case sensitivity might lead to discrepancies, such as database entries or user input handling . Normalizing data through consistent case conversion aids in effective data comparison, sorting, and storage across systems.

You might also like