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 '. 

Related Posts:

  • Abstract keyword in java Abstract is a keyword in java. So its a reserved word in java. general meaning of abstract is to hide the implementation, only show the outer structure. Now in computer science its meaning is that user can have their own i… Read More
  • Abstract class in java When we use an abstract modifier before the name of any user-define class then it is called as an abstract class. Abstract class in java has only method signature whose implementation must be in its sub-classes. Importan… Read More
  • Private access modifier in Java Private is the keyword in java and it is also called as the access modifier in java which defines the visibility to the members of the class like methods, constructors, variables etc. Declaring private to the members of … Read More
  • Accessibility of access modifiers in java Read More
  • Protected access modifier in Java Like public and private, protected is also a access modifier which has different visibility scope and it is also one of the keyword in java. Its scope are valid to the other packages but it should be within the sub-class… Read More

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic