aspose file tools
The moose likes Beginning Java and the fly likes Why is RunThis(char arg[]) expecting a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why is RunThis(char arg[]) expecting a String?" Watch "Why is RunThis(char arg[]) expecting a String?" New topic
Author

Why is RunThis(char arg[]) expecting a String?

Mannie J Chaihan
Greenhorn

Joined: May 10, 2005
Posts: 6
I want my function to accept "this is array of chars".



Of course I can't. compiler is telling me I need to send a String
What am I doing wrong?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Mannie -

A String is a different thing in Java than it is in C. In C, a null terminated character array is a string; in Java, String is a full-fledged object. So, you can create a char array from the String object that you create when you put text between quotes, or you can change the method parameter to take a String object.

Hope that helps...


Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Mannie J Chaihan:
I want my function to accept "this is array of chars".



Of course I can't. compiler is telling me I need to send a String
What am I doing wrong?


Actually "this IS a String, NOT an array of chars". As described above, a literal value enclosed in double-quotes behaves VERY differently in Java than it does in C/C++. They are actually String objects, not char arrays.

Layne


Java API Documentation
The Java Tutorial
Mannie J Chaihan
Greenhorn

Joined: May 10, 2005
Posts: 6
Thank you.
A java.lang.String != Char[]
Ok, so how do I write my function so that it will accept the call:
Mannie J Chaihan
Greenhorn

Joined: May 10, 2005
Posts: 6
Well, I tried my only option:

And Voila.. It still feels very unnatural.
Thank you for your help!
[JAM Edited for formatting of code....]
[ May 11, 2005: Message edited by: Joel McNary ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
That's correct: Java recognizes the "this is a string" as a literal object. This means that you can do things like:



There's no need for a primitive type, as the object type is much more flexible. Also, Literal strings are treated internally a little bit differently; only 1 copy of a string is created, so:


is true, where

is false.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why is RunThis(char arg[]) expecting a String?
 
Similar Threads
command line arguments
prog of multiple inputs
Filter Non-Standard Characters
how to obtain the name of an instance class
Doubt with notify/ notifyAll method