In java " + " symbol is used to concatenate two strings where as in case numerals it will add up the values.
It has wide use as an String concatenation. String concatenation means adding/ merging two or more than two string values. In java String are represented in double quote ( " " ) and the any value within this quote will be treated as a string it may a number, character or string ( Collection of characters ).

While concatenating any values if any of the value is string type then the result type will be string. And result type is int only when if both the values are integer type.

Let us discuss some cases which you may come across while reading about string concatenation operator and some of its uses.

1> Concatenating two string value :- Here we are concatenating two string values whose result is also a string type. And that string may be a character , or a numeral value or a string which is a collection of character.

for ex-
String  s  = " Java " + " World " ; // Concatenating two string values and storing it in a
                                                             string type
" s " .

String  s  = " Java " + " 10 " ;  // Concatenating two string values where one string is a
                                                        number and
storing it in a string type
" s " .

String  s  = " 20 " + " 10 " ;  // Concatenating two string values where both the strings are
                                                    number and storing it in a string type
" s " .

String  s  = " Java " + " C " ;  // Concatenating two string values where one is a character
                                                      as a strings and storing it in a string type
" s " .

String  s  = " 10 " + " C " ;  // Concatenating two string values where one is a character
                                                  as a strings and another one is number as a string and
                                                  storing it in a string type
" s ".

2> Concatenating String with a numeral value :- We can concatenate a string with a number but the result will be of string type.

for ex-
String  s  = " Java " + 10 ; // Here " 10 " append with the Java string and result will be
                                                a string i.e
 " Java10".

When we compile the above statement then compiler will convert the number into a string type by overriding toString() .  We see in the details about over riding and how it is converted internally into string by the compiler in String topic later in this tutorial.

String  s  = " " + 10 ; // Here also" 10 " is converted into a string because it is
                                       concatenating with a empty string. 


2> Concatenating String with a character value :- We can concatenate a string with a character value as well but the result will be of string type.

for ex-
String  s  = " Java " +'C' ; // Here " C " append with the "Java" string and result will be a string i.e "                                                           JavaC".

String  s  = " Java " +C ; // It gives you the compilation error because here Compiler
                                              search
C as a variable because here it is not a character .
                                              To represent a character must be defined under single quote.

Note: But we try to concatenate a character with a number then its result type will be integer or a character.
It is because every character has a ASCII value and compiler always deals with the ASCII value of a character Ans the result type will be depends up the data type we have used for storing the result. 

If want to integer value after concatenating a character and a number then store it in int type and if we want to display a character then store it in char type.

for ex-
int  s  = 'C'+10 ; // Here it will display 77 because the ASCII value of C is 67 and after
                               adding 10 it will display 77.

char s  = 'C'+10 ; // Here it will display ' M ' because the character value for 77 is ' M '. 

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic