0% found this document useful (0 votes)
20 views

Java Syntax Notes

Quick revision for java syntaxes

Uploaded by

chokkapumonisha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Syntax Notes

Quick revision for java syntaxes

Uploaded by

chokkapumonisha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

JAVA SYNTAX NOTES

Arrays
1. Syntax

Declaring an Array

In Java, you can declare an array in the following ways:

2. Traversal

You can traverse an array using loops. Here are examples using a `for` loop and an enhanced `for-each`
loop:

Using Enhanced `for-each` Loop


3. Common Methods

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()`

Converts the array to a string representation.

`Arrays.sort()`

Sorts the array in ascending order.

`Arrays.binarySearch()`

Performs a binary search on a sorted array.

`Arrays.copyOf()`
Copies the specified array, truncating or padding with zeros if necessary.

`Arrays.equals()`

Checks if two arrays are equal.

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

Declaring and Initializing Strings

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 `for` Loop

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()`

Returns the length of the string.

`charAt()`

Returns the character at the specified index.


`substring()`

Returns a new string that is a substring of the original string.

`contains()`

Checks if the string contains a specified sequence of characters.

`equals()`

Compares the string to the specified object.

`equalsIgnoreCase()`

Compares the string to another string, ignoring case considerations.

`compareTo()`

Compares two strings lexicographically.


`indexOf()`

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()`

Converts all characters in the string to lowercase.

`toUpperCase()`

Converts all characters in the string to uppercase.

StringBuilder
1. Syntax

Declaring and Initializing `StringBuilder`


2. Traversal

Traversing a `StringBuilder` can be done similarly to traversing a `String`, using loops to access each
character.

Using a `for` Loop

Using an Enhanced `for-each` Loop (requires conversion to a character array)

3. Common Methods

The `StringBuilder` class provides many useful methods for string manipulation. Here are some of the
most commonly used methods:

`append()`

Appends the specified string to this sequence.

`insert()`

Inserts the specified string at the specified position.


`replace()`

Replaces the characters in a substring of this sequence with characters in the specified string.

`delete()`

Removes the characters in a substring of this sequence.

`deleteCharAt()`

Removes the character at the specified position.

`reverse()`

Reverses the sequence of characters.

`charAt()`

Returns the `char` value at the specified index.


`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()`

Returns the length (character count).

`capacity()`

Returns the current capacity.

`setLength()`

Sets the length of the character sequence.

`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

 `StringBuilder` is used for mutable sequences of characters.


 It is not synchronized, making it suitable for single-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 `StringBuilder` is straightforward and similar to
working with strings.

String Buffer
1. Syntax

Declaring and Initializing `StringBuffer`


2. Traversal

Traversing a `StringBuffer` can be done similarly to traversing a `String`, using loops to access each
character.

Using a `for` Loop

Using an Enhanced `for-each` Loop (requires conversion to a character array)

3. Common Methods

The `StringBuffer` class provides many useful methods for string manipulation. Here are some of the most
commonly used methods:

`append()`

Appends the specified string to this sequence.

`insert()`

Inserts the specified string at the specified position.

`replace`
Replaces the characters in a substring of this sequence with characters in the specified string.

`delete()`

Removes the characters in a substring of this sequence.

`deleteCharAt()`

Removes the character at the specified position.

`reverse()`

Reverses the sequence of characters.

`charAt()`

Returns the `char` value at the specified index.

`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()`

Returns the length (character count).

`capacity()`

Returns the current capacity.

`setLength()`

Sets the length of the character sequence.

`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

 `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

Declaring and Initializing ArrayLists

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:

Using `for` Loop

Using Enhanced `for-each` Loop


3. Common Methods

The `ArrayList` class provides many useful methods for list manipulation. Here are some of the most
commonly used methods:

`add()`

Adds an element to the end of the list.

`add(index, element)`

Inserts the specified element at the specified position in the list.


`get()`

Returns the element at the specified position in the list.

`set()`

Replaces the element at the specified position in the list with the specified element.

`remove()`

Removes the element at the specified position in the list.

`size()`

Returns the number of elements in the list.

`contains()`

Returns `true` if the list contains the specified element.

`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()`

Returns `true` if the list contains no elements.

`clear()`

Removes all the elements from the list.

`toArray()`

Returns an array containing all the elements in the list.

HashMaps
1. Syntax

Declaring and Initializing HashMaps

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 `for-each` Loop to Traverse Keys

Using `for-each` Loop to Traverse Entries

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()`

Returns the number of key-value mappings in this map.

`isEmpty()`

Returns `true` if this map contains no key-value mappings.

`clear()`

Removes all of the mappings from this map.

`keySet()`

Returns a `Set` view of the keys contained in this map.

`values()`

Returns a `Collection` view of the values contained in this map.


`entrySet()`

Returns a `Set` view of the mappings contained in this map.

HashSets:
1. Syntax

Declaring and Initializing HashSets

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()`

Adds the specified element to this set if it is not already present.


`remove()`

Removes the specified element from this set if it is present.

`contains()`

Returns `true` if this set contains the specified element.

`size()`

Returns the number of elements in this set.

`isEmpty()`

Returns `true` if this set contains no elements.

`clear()`

Removes all of the elements from this set.

`iterator()`

Returns an iterator over the elements in this set.


`toArray()`

Returns an array containing all of the elements in this set.

`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.

You might also like