C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Concat: The plus-operator combines strings. We combine the data from two or more strings this way.
Length: We call the length method on each string variable reference. This returns the character count.
Java program that uses strings
public class Program {
public static void main(String[] args) {
// Create strings with literals.
String value1 = "Java";
String value2 = "Programmer";
// Concatenate strings.
String value3 = value1 + " " + value2;
// Print string.
System.out.println(value3);
// Get string lengths.
int length1 = value1.length();
int length2 = value2.length();
int length3 = value3.length();
// Print lengths.
System.out.println(length1);
System.out.println(length2);
System.out.println(length3);
}
}
Output
Java Programmer
4
10
15
Java program that uses length, literal
public class Program {
public static void main(String[] args) {
// Take the length of a literal directly.
int length = "abc".length();
System.out.println(length);
}
}
Output
3