Java Tutorials
Difference between Comparable and Comparator in Java
What is Comparable in Java? Comparable in Java is an object to compare itself with another object....
This function is used to get the length of string in Java. The string length method returns the number of characters written in the String. This method returns the length of any string which is equal to the number of 16-bit Unicode characters in the string.
public int length()
Parameters:
NA
Return Value:
This method returns the length of a string in Java.
Java string Length Method Examples:
In this program, we will learn how to find the length of a string in Java. We have two Strings and we will find out string length in Java using length() method.
public class Sample_String {
public static void main(String[] args) {
//declare the String as an object S1 S2
String S1 = "Hello Java String Method";
String S2 = "RockStar";
//length() method of String returns the length of a String S1.
int length = S1.length();
System.out.println("Length of a String is: " + length);
//8 Length of a String RockStar
System.out.println("Length of a String is: " + S2.length());
}
}
Output:
Length of a String is: 24
Length of a String is: 8
What is Comparable in Java? Comparable in Java is an object to compare itself with another object....
What is TypeScript? TypeScript is a superset of JavaScript. TypeScript is pure object-oriented...
Variables are used to store values (name = "John") or expressions (sum = x + y). Declare Variables in...
What is = in JavaScript? Equal to (=) is an assignment operator, which sets the variable on the...
Here are Java Collections Interview Questions for fresher as well as experienced candidates to get...
What is Quick Sort? Quick Sort algorithm follows Divide and Conquer approach. It divides elements...