| Author |
Why Error Here?
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Ranchers, Code below, Why a complie time error? Regards, Jothi Shankar Kumar. S
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
William Yan
Ranch Hand
Joined: Sep 26, 2006
Posts: 69
|
|
hi, you mean this? or?
|
Java is better and better, yet bitter and bitter.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Above, I mean the other way, char c = 'H'; String s = "ello"; c = c + s; //why it gives error here? Regards, Jothi Shankar Kumar. S
|
 |
Gowher Naik
Ranch Hand
Joined: Feb 07, 2005
Posts: 643
|
|
I have made single change in your code. Try to find out yourself why there is error.
|
 |
William Yan
Ranch Hand
Joined: Sep 26, 2006
Posts: 69
|
|
Hi, char is a primitive with 16-bits, why do you think you could stored so fat a String into a char?
|
 |
srini p.
Greenhorn
Joined: Oct 13, 2006
Posts: 3
|
|
Latest exam for SCWCD is Exam cx-310-081(SCWCD using J2EE 1.4) Best book to pass SCWCD is Head First Servlet & JSP by Bryan Basham,Kethy Sierra & Bert Bates Hi Kumar, public void amethod(String s){ char c='H'; c+=s; System.out.println(c); } If U observe the code, you are tiring to concatenate a String to char. U can't use "+" operator on a char data (i.e c). replace the method with public void amethod(String s){ char c='H'; s+=c; System.out.println(s); } It will work !! Regards, Srinivas.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi all Above, So a char fits into a String but not the other way. Am I right? Regards, Jothi Shankar Kumar. S
|
 |
William Yan
Ranch Hand
Joined: Sep 26, 2006
Posts: 69
|
|
Yes, Jothi, char is a primitive, but a String is 'like' an array of char.
|
 |
Anvi Dixit
Ranch Hand
Joined: Aug 28, 2006
Posts: 45
|
|
Hi All, yes the below code is compiling fine but why i am getting the exception saying : Exception in thread "main" java.lang.NoClassDefFound error public void amethod(String s){ char c='H'; c+=s; System.out.println(c); } If U observe the code, you are tiring to concatenate a String to char. U can't use "+" operator on a char data (i.e c). replace the method with public void amethod(String s){ char c='H'; s+=c; System.out.println(s); } PLease help me out . Thanks Anvi
|
 |
 |
|
|
subject: Why Error Here?
|
|
|