Given the following declarations String s1=new String("Hello")String s2=new String("there");String s3=new String(); Which of the following are legal operations?
-
s3=s1 + s2;
-
s3=s1-s2;
-
s3=s1 & s2;
-
s3=s1 && s2
String concatenation in Java is performed with the + operator. The - (subtraction), & (bitwise AND), and && (logical AND) operators cannot be applied to String objects. Only s3 = s1 + s2 is valid.
To answer this question, we need to understand the different operations that can be performed on strings in Java.
Option A) s3=s1 + s2 - This option is legal. The + operator can be used to concatenate two strings in Java. Therefore, s1 + s2 will concatenate the strings "Hello" and "there", resulting in the string "Hellothere". The concatenated string can then be assigned to s3.
Option B) s3=s1 - s2 - This option is not legal. The - operator is not defined for strings in Java. Therefore, we cannot subtract one string from another.
Option C) s3=s1 & s2 - This option is not legal. The & operator is a bitwise operator and is not defined for strings in Java. Therefore, we cannot perform a bitwise operation on strings.
Option D) s3=s1 && s2 - This option is not legal. The && operator is a logical operator and is not defined for strings in Java. Therefore, we cannot perform a logical operation on strings.
The correct answer is A) s3=s1 + s2. This option is legal because the + operator can be used to concatenate strings in Java.