-1

I was writing a piece of code to reverse a String wordwise(Input:Java is fun; Output:fun is Java), when I came across a question:How can a String variable be added to a character type variable.the code is working perfectly, but how is this possible? Someone, please explain this to me. Thanks in advance.

import java.io.*;
public class numberof {
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str1,str2="",str3="";
str1=br.readLine()+" ";
for(int i=0;i<str1.length();i++){
char x=str1.charAt(i);
if(x==' '){
    str2=str3+" "+str2;
str3="";
}
else{
    str3=str3+x;   //HERE IS THE QUESTION PART(STRING+CHAR)

   }
 }
 System.out.println(str2);  
 }

  }
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Abhinav
  • 182
  • 1
  • 3
  • 12
  • 3
    Implicit type conversion – nj-ath Jul 23 '13 at 03:58
  • 1
    [This guy][1] gives a pretty decent rundown of what's happening. [1]: http://stackoverflow.com/a/328253/2400222 – Zec Jul 23 '13 at 03:59
  • 1
    @Zec Pretty sure you can just put the link in like this: "\[link-name\](link-address)" – mattbdean Jul 23 '13 at 04:01
  • @whowantsakookie - Thanks. I actually submitted that as an answer but SO said it was "trivial" and made it a comment, making the link go whacky. Figured it's clickable and has value regardless. Leaving it as is out of spite. Trivial?! – Zec Jul 23 '13 at 04:05
  • @Zec I think you were right. That is a good answer and this question could have actually just been answered via a simple google search. – mattbdean Jul 23 '13 at 04:07
  • @Zec it's not trivial, but against the rules of SO. Refer to [What is an acceptable answer?](http://meta.stackexchange.com/q/118582/182862), section 6. – Luiggi Mendoza Jul 23 '13 at 04:09
  • 1
    @LuiggiMendoza - Thanks. I suspected my answer might not be sufficient to be called an answer, but it was to a question for which answers abound. Also, I couldn't think of a clever analogy like cans or necklaces. – Zec Jul 23 '13 at 04:16

2 Answers2

6

A char can be implicitly converted into a String. If Java didn't do this, you would have had to write it as following:

str3 = str3 + String.valueOf(x);

Heck, even the + operator is overloaded for Strings in Java. Without which, you would have had to write it like this:

str3 = str3.concat(String.valueOf(x));

So implicit type conversions make it easy for you to write code. And it's easy for you to read and understand the same code:

str3 = str3 + x;
//understanding: the string 'str3' is being joined with the character 'x'

Note, however that not everything can be implicitly converted. For example, this wouldn't work:

char y = str3 + x; //ERROR: cannot cast string to char!

Implicit conversion only works when a basic type can be converted to a more advanced type without data loss. Here, a String can be thought of as a string of characters (think of those letter-bead bracelets). So a char can be thought of as a string with just one character. So what the + operator does is to add this character to the end of the other string full of characters.

ADTC
  • 8,999
  • 5
  • 68
  • 93
2

When there is a conflict between the conversion between data types, the compiler always chooses the larger data type(in your case it is string). This is called implicit type conversion.

Say you have 2 cans 1 of 1ltr capacity and the other 2ltr. It is always safe to pour the contents of 1ltr can to the 2 ltr can.

nj-ath
  • 3,028
  • 2
  • 25
  • 41