Simple question | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Simple question

What is the output of this code? public class Program { public static void main(String[] args) { String a = ā€œoneā€, b = ā€œtwoā€; String c = String.join(a, b); System.out.print(c); } } Answer: two - please explain me

4th Mar 2020, 7:27 PM
Š¢ŠµŃ€ŠµŃ‰ŠµŠ½ŠŗŠ¾ Š”Š¼ŠøтрŠøŠ¹
Š¢ŠµŃ€ŠµŃ‰ŠµŠ½ŠŗŠ¾ Š”Š¼ŠøтрŠøŠ¹ - avatar
3 Respostas
+ 5
The join method takes at first a char sequence delimeter and then another char sequence for the elements String.join("/", "a","b","c") would be: "a/b/c" String.join("+", "1","2","3","4") would be "1+2+3+4" In your case: a is the delimeter and b the element. One element don't need an delimeter, so the output is just "two". You can see what happens if you add another String. String c = String.join(a,b,"three"); For more details you can read this article: https://www.javapoint.com/java-string-join
4th Mar 2020, 7:42 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 3
In this case you are passing two strings to the join() method, so the following method signature is invoked: public staticĀ StringĀ join(CharSequenceĀ delimiter, CharSequence...Ā elements) a is the delimiter b is the first element But because there are no more elements to combine, the delimiter is not used. That's why the result is two. https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.CharSequence...-
4th Mar 2020, 7:39 PM
Tibor Santa
Tibor Santa - avatar
+ 2
For join method, the first argument is delimiter..., next arguments are string to be joined.. For ex: S=String.join("@", "abc", "cdf"," ef"); Now S=abc@cdf@ef; Delimiter is added in between string not at end, not at start so, Since in your example, have only one string, one delimiter, output is two..
4th Mar 2020, 7:42 PM
Jayakrishna šŸ‡®šŸ‡³