Podcast
Questions and Answers
Which method is used to convert a String to a float?
Which method is used to convert a String to a float?
- Float.parseFloat() (correct)
- String.toFloat()
- Float.valueOf()
- StringParser.float()
What is the result of explicitly casting the double 5.99 to an int?
What is the result of explicitly casting the double 5.99 to an int?
- 6
- 5.0 (correct)
- 5.99
- 5 (correct)
How do you convert an int to a char?
How do you convert an int to a char?
- (char) intValue (correct)
- String.valueOf(intValue)
- Character.toChar()
- Character.fromInt()
What is the output of the following code: String str = 'HELLO'; String lower = str.toLowerCase(); System.out.println(lower);
What is the output of the following code: String str = 'HELLO'; String lower = str.toLowerCase(); System.out.println(lower);
Which statement correctly converts a long to a String?
Which statement correctly converts a long to a String?
What will be the output of 'char c = str.charAt(0);' if str = 'World'?
What will be the output of 'char c = str.charAt(0);' if str = 'World'?
Which method is used to remove leading and trailing whitespace from a string?
Which method is used to remove leading and trailing whitespace from a string?
Which of the following statements correctly describes the split(String regex) method?
Which of the following statements correctly describes the split(String regex) method?
Which method should be used to check if a String can be converted to a boolean?
Which method should be used to check if a String can be converted to a boolean?
What does the startsWith(String prefix) method return when checking if the string 'Java Programming' starts with 'Java'?
What does the startsWith(String prefix) method return when checking if the string 'Java Programming' starts with 'Java'?
When converting a float to an int, what type of conversion is required?
When converting a float to an int, what type of conversion is required?
Which method is NOT used for String to Character conversion?
Which method is NOT used for String to Character conversion?
What does the endsWith(String suffix) method return when checking if 'Hello, World!' ends with 'World!'?
What does the endsWith(String suffix) method return when checking if 'Hello, World!' ends with 'World!'?
Which output is generated by the following code: String str = 'hello'; String newStr = str.replace('l', 'p'); System.out.println(newStr);
Which output is generated by the following code: String str = 'hello'; String newStr = str.replace('l', 'p'); System.out.println(newStr);
What is returned by the isEmpty() method when applied to a newly instantiated string object?
What is returned by the isEmpty() method when applied to a newly instantiated string object?
What will be the output of comparing the strings 'apple' and 'banana' using the compareTo method?
What will be the output of comparing the strings 'apple' and 'banana' using the compareTo method?
How do you convert a string representing a number to an integer?
How do you convert a string representing a number to an integer?
Which method would you use to join multiple strings with a delimiter?
Which method would you use to join multiple strings with a delimiter?
What does the toCharArray() method do to a string?
What does the toCharArray() method do to a string?
When using compareToIgnoreCase on 'apple' and 'Banana', what will the result be?
When using compareToIgnoreCase on 'apple' and 'Banana', what will the result be?
What type of casting is performed when converting a string to an integer using Integer.valueOf()?
What type of casting is performed when converting a string to an integer using Integer.valueOf()?
Which method can be used to convert an int to a string?
Which method can be used to convert an int to a string?
What is the correct way to convert a string '123.45' to a double?
What is the correct way to convert a string '123.45' to a double?
Flashcards
Converting double to String
Converting double to String
Using String.valueOf() to convert a double to a String.
String to float conversion
String to float conversion
Converting a string to a float using Float.parseFloat().
Implicit int to double
Implicit int to double
Java automatically converts an int to a double.
Explicit double to int
Explicit double to int
Converting a double to an int using casting.
Signup and view all the flashcards
Char to int conversion
Char to int conversion
Converting a character to its ASCII integer representation.
Signup and view all the flashcards
int to char
int to char
Converting an integer (ASCII value) to a character.
Signup and view all the flashcards
Char to String conversion
Char to String conversion
Converting a character into a string using Character.toString() or String.valueOf().
Signup and view all the flashcards
String to char
String to char
Extracting a single character from a string using charAt().
Signup and view all the flashcards
toUpperCase()
toUpperCase()
Converts all characters in a string to uppercase.
Signup and view all the flashcards
toLowerCase()
toLowerCase()
Converts all characters in a string to lowercase.
Signup and view all the flashcards
trim()
trim()
Removes leading and trailing whitespace from a string.
Signup and view all the flashcards
replace(char, char)
replace(char, char)
Replaces all occurrences of a specific character with another in a string.
Signup and view all the flashcards
replace(CharSequence, CharSequence)
replace(CharSequence, CharSequence)
Replaces all occurrences of a substring with another substring in a string.
Signup and view all the flashcards
split(String)
split(String)
Splits a string into an array of substrings based on a given delimiter.
Signup and view all the flashcards
startsWith(String)
startsWith(String)
Checks if a string begins with a specified prefix.
Signup and view all the flashcards
endsWith(String)
endsWith(String)
Checks if a string ends with a specified suffix.
Signup and view all the flashcards
isEmpty() Method
isEmpty() Method
Checks if a String is empty (has zero length).
Signup and view all the flashcards
join() Method
join() Method
Combines multiple strings into one, using a specified delimiter.
Signup and view all the flashcards
compareTo() Method
compareTo() Method
Compares two strings lexicographically (alphabetical order).
Signup and view all the flashcards
compareToIgnoreCase() Method
compareToIgnoreCase() Method
Compares two strings lexicographically, ignoring case sensitivity.
Signup and view all the flashcards
valueOf() Method (String)
valueOf() Method (String)
Converts various data types (int, long, float, boolean) to a String.
Signup and view all the flashcards
toCharArray() Method
toCharArray() Method
Converts a String into a character array, where each element is a single character.
Signup and view all the flashcards
String to Integer
String to Integer
Converts a String representing a number to an int.
Signup and view all the flashcards
Integer to String
Integer to String
Converts an int to a String.
Signup and view all the flashcardsStudy Notes
Java String Methods
-
length()
: Returns the length of the string.- Example:
"Hello, World!"
.length() returns 13
- Example:
-
charAt(int index)
: Returns the character at the specified index.- Example:
"Hello"
.charAt(1) returns 'e'
- Example:
-
substring(int beginIndex)
: Returns a substring starting from the specified index.- Example:
"Hello, World!"
.substring(7) returns "World!"
- Example:
-
substring(int beginIndex, int endIndex)
: Returns a substring frombeginIndex
toendIndex
- 1.- Example:
"Hello, World!"
.substring(0, 5) returns "Hello"
- Example:
-
contains(CharSequence sequence)
: Checks if the string contains the specified sequence of characters.- Example:
"Hello, World!"
.contains("World") returns true
- Example:
-
equals(Object another)
: Compares the string to the specified object for equality (case sensitive).- Example:
"Hello"
.equals("Hello") returns true
- Example:
-
equalsIgnoreCase(String another)
: Compares two strings, ignoring case differences.- Example:
"HELLO"
.equalsIgnoreCase("hello") returns true
- Example:
-
indexOf(String str)
: Returns the index of the first occurrence of the substring.- Example:
"Hello, World!"
.indexOf("World") returns 7
- Example:
-
indexOf(String str, int fromIndex)
: Returns the index of the first occurrence of the substring starting from the specified index.- Example:
"Hello, World! World!"
.indexOf("World", 8) returns 14
- Example:
-
lastIndexOf(String str)
: Returns the index of the last occurrence of the substring.- Example:
"Hello, World! World!"
.lastIndexOf("World") returns 14
- Example:
-
toUpperCase()
: Converts all characters in the string to uppercase.- Example:
"hello"
.toUpperCase() returns "HELLO"
- Example:
-
toLowerCase()
: Converts all characters in the string to lowercase.- Example:
"HELLO"
.toLowerCase() returns "hello"
- Example:
-
trim()
: Removes leading and trailing whitespace.- Example:
" Hello, World! "
.trim() returns "Hello, World!"
- Example:
-
replace(char oldChar, char newChar)
: Replaces all occurrences of a specified character with a new character.- Example:
"hello"
.replace('l', 'p') returns "heppo"
- Example:
-
replace(CharSequence target, CharSequence replacement)
: Replaces all occurrences of a substring with another substring.- Example:
"Hello, World!"
.replace("World", "Java") returns "Hello, Java!"
- Example:
-
split(String regex)
: Splits the string into an array based on the given regular expression.- Example:
"apple,orange,banana"
.split(",") returns an array containing "apple", "orange", and "banana"
- Example:
-
split(String regex, int limit)
: Splits the string into an array with a maximum number of substrings.- Example:
"apple,orange,banana"
.split(",",2) returns an array containing "apple" and "orange,banana".
- Example:
-
startsWith(String prefix)
: Checks if the string starts with the specified prefix.- Example:
"Hello, World!"
.startsWith("Hello") returns true
- Example:
-
endsWith(String suffix)
: Checks if the string ends with the specified suffix.- Example:
"Hello, World!"
.endsWith("World!") returns true
- Example:
-
isEmpty()
: Checks if the string is empty (length is 0).- Example:
"".isEmpty()
returns true
- Example:
-
join(CharSequence delimiter, CharSequence... elements)
: Joins multiple strings with the specified delimiter.- Example:
String.join("-", "2024", "10", "20")
returns "2024-10-20"
- Example:
-
compareTo(String anotherString)
: Compares two strings lexicographically.- Example:
"apple"
.compareTo("banana") returns a negative value
- Example:
-
compareToIgnoreCase(String anotherString)
: Compares two strings lexicographically, ignoring case differences.- Example:
"apple"
.compareToIgnoreCase("Banana") returns a negative value (because apple < Banana)
- Example:
Java Type Casting
-
String to Integer (int):
Integer.parseInt(str)
Integer.valueOf(str)
-
Integer to String:
String.valueOf(int)
Integer.toString(int)
-
String to double/float/long:
Double.parseDouble(str)
Float.parseFloat(str)
Long.parseLong(str)
-
Other Type Conversions: String to boolean, boolean to string, char to int, int to char, Other conversions are implicit or explicit, depending on the context and data types' sizes.
-
Using Concatenation: You can convert an int to a String by concatenating it with an empty string (e.g.,
123 + ""
).
Additional Notes
- Methods like
toCharArray
,valueOf
handle various data types, converting them to Strings. - Java has implicit and explicit type conversion rules; implicitly, in some cases, conversion might occur based on data type compatibility. Explicitly, conversion might be required through casting to change data type.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.