variable argument not working
someone tell why this not working
class Init
{
public void assign(String... args)
{
for(Object str:args)
System.out.println(str);
}
}
public class VarArg1
{
public static void main(String args[])
{
Init abc=new Init();
abc.assign("asdfasd","sdfas","asdfa","adfas");
abc.assign(2,'c',"asdfa",5.333);//can send different types as i am recieving it with an object array.
}
}
abc.assign(2,'c',"asdfa",5.333);//can send different types as i am recieving it with an object array.You are not receiving it with an object array. You are receiving it with a String array and these aren't all Strings
When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
Also, it would help if you'd provide more detail than "not working."
However, you appear confused about one thing:
//can send different types as i am recieving it with an object array.
No, you're using a Sring array, and 2, 'c', and 5.333 are not Strings.
> someone tell why this not working
>
> class Init
> {
> public void assign(String... args)
> {
> for(Object str:args)
> System.out.println(str);
> }
> }
>
> public class VarArg1
> {
> public static void main(String args[])
> {
> Init abc=new Init();
> abc.assign("asdfasd","sdfas","asdfa","adfas");
> abc.assign(2,'c',"asdfa",5.333);//can send
> nd different types as i am recieving it with an
> object array.
This is wrong. You are recieving with an String array and here you have primitives.
> }
> }
Delete this line
abc.assign(2,'c',"asdfa",5.333);
and it will work.
> someone tell why this not working
>
> class Init
> {
> public void assign(String... args)
> {
> for(Object str:args)
> System.out.println(str);
> }
> }
>
> public class VarArg1
> {
> public static void main(String args[])
> {
> Init abc=new Init();
> abc.assign("asdfasd","sdfas","asdfa","adfas");
> abc.assign(2,'c',"asdfa",5.333);//can send
> nd different types as i am recieving it with an
> object array.
> }
> }
You can *not* send it types other than Strings.. you declare the method as "public void assign(String... args)" .. the method is expecting to be passd Strings. If you changed it to "public void assign(Object... args)" then you could pass it other stuff. However, I don't think I'd recommend that approach to programming. It'd work, but you'd eventually get some weird output as every Object's toString() method was invoked that you sent to it. (and as we all know, they're not always standardized)
but i get code from teacher and teacher saying get output....?
Either your professor is mad or he/she is testing you.
Anyways i didn't notice this
public void assign(String... args)
{
for(Object str:args)
System.out.println(str);
}
}
Make this
public void assign(Object... args)
{
for(Object str:args)
System.out.println(str);
}
}
This will work.
So it looks like he has screwed the code to test you students.
> but i get code from teacher and teacher saying get> output....?Then your teacher can't code. I suggest you drop the class and find another professor.
> but i get code from teacher and teacher saying get> output....?If the compiler and your teacher disagree, your teacher is wrong.
> but i get code from teacher and teacher saying get> output....?Did you copy/paste it, download it, get it in an email, etc.?Or did you read it and (mis?)type it in?
> i write it in book
You wrote it in a book?
Do you mean you read it from a book and typed it in here? If so, either the book has a mistake or you made a mistake when copying it. As somebody pointed out, changing String... to Object... will fix it. (The primtives oculd cause a problem, but I think autoboxing will take care of that.)
> > i write it in book
>
> You wrote it in a book?
>
> Do you mean you read it from a book and typed it in
> here? If so, either the book has a mistake or you
> made a mistake when copying it. As somebody pointed
> out, changing String... to Object... will fix it.
> (The primtives oculd cause a problem, but I think
> autoboxing will take care of that.)
@OP please note that the alternative i suggested is with respect to 1.5 and not the previous versions.
You can read about autoboxing here anyways just incase you don't know
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
Good Luck
> @OP please note that the alternative i suggested is> with respect to 1.5 and not the previous versions. Not an issue. If varargs is present, so is autoboxing. They were both introduced in 1.5
> > @OP please note that the alternative i suggested
> is
> > with respect to 1.5 and not the previous versions.
>
>
> Not an issue. If varargs is present, so is
> autoboxing. They were both introduced in 1.5
Oh! Thanks. I wasn't aware of that.