Menu
Topics Index
...
`

Concatenation


If you need explanation Read this topic

If you need Answer Take test on this topic

Concatenation
What will be the output of the following program?
class StringExample {
    public static void main(String args[])
    {
        String s1 = "Java";
        String s2 = "Professional";
        System.out.println(s1.concat(" * ").concat(s2));
    }
}

A. Java Professional
B. Java * Professional
C. Compilation Error
D. Runtime Error
Topic: Java String concatenation - concat() Method In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is B...concat functions appends one string to the end of another string .Here " * " is appended to Java and next concat call appends Professional to Java * and finally java * Professional is returned

Posted by Shashanka Mogaliraju    2014-12-25 12:45:38


b..... Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objectsare immutable they can be shared. For example:     String str = "abc"; is equivalent to:     char data[] = {'a', 'b', 'c'};     String str = new String(data); here we have created a string obj. s1 and s2 with vakues as java $ professoinal respectivelyq.....now the concat fnctn is used in print stmt......concat()-Description:This method appends one String to the end ofanother. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method.Syntax:Here is the syntax of this method:public String concat(String s)....here * is appended to java and the next call to concat fnctn will appnd professional to java * and the final val is java*professionall

Posted by Asma Mujtaba Khan    2014-12-25 12:54:36


ANS IS B

--Here two STRINGS arr used s1=java and s2=professional.

---As strings are immutable I.e. once created they can not be manipulated so for performing manipuling operations like insertion of any characters,concatination one more class is used which is STRINGBUFFER.

---Here strings are concatinated just for display time...It will not affect the values of String Objects.  

--String concat(String): this function is used to concatinate one string with another and returns the cocatinated string.

---s1 is concatinated with * I.e. (java * )which is concatinated with s2 I.e.  "java * professional" will get displayed.

Posted by Mânïshå Mùlchåndânï    2014-12-25 14:53:53


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-26 04:12:43


ans b.

in this program initially 2 string object initialized with s1="Java",s2=Professional"

the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable.

concat() method appends one String to the end of another. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method.

in sop 1st s1 is append with "*" and again concat with s2 string objct.

here we are not storing this concat result,so sop just displays resultant string.

"Java * Professional"

Posted by Maheshwari Natarajan    2014-12-26 04:30:28


© meritcampus 2019

All Rights Reserved.

Open In App