Java Syntax Notes
Java Syntax Notes
Arrays
1. Syntax
Declaring an Array
2. Traversal
You can traverse an array using loops. Here are examples using a `for` loop and an enhanced `for-each`
loop:
Java arrays do not have built-in methods like lists in Java (e.g., `ArrayList`). However, Java provides the
`java.util.Arrays` class, which includes many useful methods for array manipulation.
`Arrays.toString()`
`Arrays.sort()`
`Arrays.binarySearch()`
`Arrays.copyOf()`
Copies the specified array, truncating or padding with zeros if necessary.
`Arrays.equals()`
Strings
Sure, let's prepare comprehensive notes on Strings in Java, including syntax, traversal, common
methods, and a list of important string problems for interviews.
1. Syntax
In Java, strings are objects of the `String` class. Here are various ways to declare and initialize strings:
2. Traversal
You can traverse a string using loops. Here are examples using a `for` loop and an enhanced `for-
each` loop (though the latter is less common for strings):
Using Enhanced `for-each` Loop (not typical, requires conversion to a character array)
3. Common Methods
The `String` class in Java provides many useful methods for string manipulation. Here are some of the
most commonly used methods:
`length()`
`charAt()`
`contains()`
`equals()`
`equalsIgnoreCase()`
`compareTo()`
Returns the index within the string of the first occurrence of the specified character or substring.
`lastIndexOf()`
Returns the index within the string of the last occurrence of the specified character or substring.
`replace()`
Returns a new string resulting from replacing all occurrences of old characters or substrings with new
ones.
`split()`
Splits the string into an array of substrings based on the specified delimiter.
`toLowerCase()`
`toUpperCase()`
StringBuilder
1. Syntax
Traversing a `StringBuilder` can be done similarly to traversing a `String`, using loops to access each
character.
3. Common Methods
The `StringBuilder` class provides many useful methods for string manipulation. Here are some of the
most commonly used methods:
`append()`
`insert()`
Replaces the characters in a substring of this sequence with characters in the specified string.
`delete()`
`deleteCharAt()`
`reverse()`
`charAt()`
`substring()`
Returns a new `String` that contains a subsequence of characters currently contained in this sequence.
`length()`
`capacity()`
`setLength()`
`ensureCapacity()`
Ensures that the capacity is at least equal to the specified minimum.
`trimToSize()`
Reduces the storage used for the character sequence to the sequence's current length.
Summary
String Buffer
1. Syntax
Traversing a `StringBuffer` can be done similarly to traversing a `String`, using loops to access each
character.
3. Common Methods
The `StringBuffer` class provides many useful methods for string manipulation. Here are some of the most
commonly used methods:
`append()`
`insert()`
`replace`
Replaces the characters in a substring of this sequence with characters in the specified string.
`delete()`
`deleteCharAt()`
`reverse()`
`charAt()`
`setCharAt()`
Sets the `char` value at the specified index.
`substring()`
Returns a new `String` that contains a subsequence of characters currently contained in this sequence.
`length()`
`capacity()`
`setLength()`
`ensureCapacity()`
Reduces the storage used for the character sequence to the sequence's current length.
Summary
`StringBuffer` is used for mutable sequences of characters and is synchronized, making it thread-
safe.
It is suitable for use in multi-threaded environments.
It provides efficient methods for modifying the string content such as `append`, `insert`, `replace`,
`delete`, and more.
Traversing and manipulating the content of `StringBuffer` is straightforward and similar to
working with strings.
ArrayList
Sure, let's prepare comprehensive notes on ArrayLists in Java, covering syntax, traversal, common
methods, and important problems related to ArrayLists for interviews.
1. Syntax
ArrayLists are part of the `java.util` package. Here’s how you can declare and initialize them:
2. Traversal:
You can traverse an ArrayList using various loops. Here are examples using a `for` loop, an enhanced `for-
each` loop, and an iterator:
The `ArrayList` class provides many useful methods for list manipulation. Here are some of the most
commonly used methods:
`add()`
`add(index, element)`
`set()`
Replaces the element at the specified position in the list with the specified element.
`remove()`
`size()`
`contains()`
`indexOf()`
Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain
the element.
`isEmpty()`
`clear()`
`toArray()`
HashMaps
1. Syntax
HashMaps are part of the `java.util` package. Here’s how you can declare and initialize them:
2. Traversal
You can traverse a HashMap using various methods. Here are examples using `for` loop, enhanced `for-
each` loop, and iterator:
Using Iterator
3. Common Methods
The `HashMap` class provides many useful methods for map manipulation. Here are some of the most
commonly used methods:
`put()`
Associates the specified value with the specified key in the map.
`get()`
Returns the value to which the specified key is mapped, or `null` if this map contains no mapping for the
key.
`remove()`
Removes the mapping for the specified key from this map if present.
`containsKey()`
Returns `true` if this map contains a mapping for the specified key.
`containsValue()`
Returns `true` if this map maps one or more keys to the specified value.
`size()`
`isEmpty()`
`clear()`
`keySet()`
`values()`
HashSets:
1. Syntax
HashSets are part of the `java.util` package. Here’s how you can declare and initialize them:
2. Traversal
You can traverse a HashSet using various methods. Here are examples using an enhanced `for-each` loop
and an iterator:
Using Enhanced `for-each` Loop
Using Iterator
3. Common Methods
The `HashSet` class provides many useful methods for set manipulation. Here are some of the most
commonly used methods:
`add()`
`contains()`
`size()`
`isEmpty()`
`clear()`
`iterator()`
`addAll()`
Adds all of the elements in the specified collection to this set if they're not already present.
`removeAll()`
Removes from this set all of its elements that are contained in the specified collection.
`retainAll()`
Retains only the elements in this set that are contained in the specified collection.
`clone()`
Returns a shallow copy of this `HashSet` instance.