• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

TYPE CASTING

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Chartype{
public static void main(String argv[] )
{
Conv c=new Conv();
String s=new String("ello");
c.amethod(s);
}
public void amethod(String s)
{
char c='H';
// c = c + s;
s = c + s;
System.out.println(s);
}
}
The Above Programe works fine if Compiled and run but the output given is just "H".
But i remove the above comment (c=c+s) then it gives a compilation error.
Please reply Iam confused as according to me should give a type conversion error saying that the primitive datatype cannot be converted to String data type.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Chartype{
public static void main(String argv[] )
{
Chartype c = new Chartype();
String s = new String("ello");
c.amethod(s);
}
public void amethod(String s)
{
char c='H';
//c = c + s;
s = c + s;
System.out.println(c);
System.out.println(s);
}
}
sudhakar the code if not commented gives the error cannot
convert char to java.lang.string if commented gives output H
and Hello on executed.
 
Sadashiv Borkar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Chartype{
public static void main(String argv[] )
{
Chartype c = new Chartype();
String s = new String("ello");
c.amethod(s);
}
public void amethod(String s)
{
char c='H';
s = c + s;
System.out.println(s);
}
}
I think you didnot understand the question clearly...
Now see the above code .The above code works fine if Iam compiling it and gives me an output as H.....Ok..If you see above code clearly you will find that there is a char variable declared above. "According to me when Iam adding a char to a string it should give me an error saying that you cannot convert a char to a string"....
Now see the code Below
class Chartype{
public static void main(String argv[] )
{
Chartype c = new Chartype();
String s = new String("ello");
c.amethod(s);
}
public void amethod(String s)
{
char c='H';
c = c + s;
System.out.println(c);
}
}
If you see the above code you will find that now it gives me an
compilation error Of TypeCAsting Now I think you can clarify my doubt...
Thanks in Advance...
chao
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first set of code should give you a print out of "Hello" and the second set will receive a compile error of "incompatible types"
You can add a char to a String, but you cannot add a String to a char. That's why you don't receive a compile error with the first one. I'm not sure about the reason. Mayby it's because String is an array of chars?
 
Let's get him boys! We'll make him read this tiny ad!
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic