The Artima Developer Community
Sponsored Link

Java Answers Forum
procedure

7 replies on 1 page. Most recent reply: Mar 17, 2004 10:19 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
anamika

Posts: 11
Nickname: anamika
Registered: Feb, 2004

procedure Posted: Mar 14, 2004 8:07 AM
Reply to this message Reply
Advertisement
hi ,

Can we return more than one value in a procedure. if so how ?


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: procedure Posted: Mar 14, 2004 8:08 PM
Reply to this message Reply
Yes and No.

A method (as they are called in Java) can only return one thing. However, if that thing is an object, then, in a sense, it can return multiple items of data.

For example, a MouseEvent object is created in response to any of several mouse activities (i.e. clicking, moving, entering or exiting the space above a component). It has a method that can return a Point object that corresponds to the point where the event took place. A Point object has two fields, an x coordinate for the horizontal position, and a y coordinate for the vertical position.

So the getPoint() method is only returning one thing - a Point object. But that object contains both the x and y coordinates where the Mouse event occurred.

I hope this helps.
twc

Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: procedure Posted: Mar 15, 2004 6:35 AM
Reply to this message Reply
we can always return multiple values out of a procedure by returning a collection object as simple as that
dhrubo@indiainfo.com

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: procedure Posted: Mar 15, 2004 12:04 PM
Reply to this message Reply
> we can always return multiple values out of a procedure by
> returning a collection object as simple as that
> dhrubo@indiainfo.com


Either way you are still returning an object.

Coolest Head

Posts: 5
Nickname: stkovrflow
Registered: Mar, 2004

Re: procedure Posted: Mar 15, 2004 8:52 PM
Reply to this message Reply
Or since all the variables are references(except the primitives) you can have out param in the calling parameter list.

example:

void foo(Object inParam, Object outParam1, Object outParam2)
{
outParam1 = new Integer(1);
outParam2 = new Integer(inparam.hasCode());
}

void main(String [] args)
{
Obj o1=new Object(),o2=null,o3=null;
foo(o1, o2, o3);
System.out.println(o2+","+o3);
}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: procedure Posted: Mar 16, 2004 2:59 AM
Reply to this message Reply
> Or since all the variables are references(except the
> primitives) you can have out param in the calling
> parameter list.

You could do that but it would be an extraordinarily Bad Thing. I can't think of any reason I would allow such code to be delivered.

Vince.

Coolest Head

Posts: 5
Nickname: stkovrflow
Registered: Mar, 2004

Re: procedure Posted: Mar 17, 2004 3:42 PM
Reply to this message Reply
> hi ,
>
> Can we return more than one value in a procedure. if so
> how ?

Actually I think I am wrong....

I cannot create a new object and expect it to come as an OutParam. I can only change an object. So just disregard my prev. comment.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: procedure Posted: Mar 17, 2004 10:19 PM
Reply to this message Reply
Yeah, I didn't think that example would work.

Nevertheless, you can "return" data with parameters, but as mentioned above, it's usually a bad idea:

public class AntiIdiom
{
   static void getOutaHere( StringBuffer name )
   {
      name.append("Outa here!");
   }
   public static void main( String [] args )
   {
      StringBuffer buffy = new StringBuffer();
      getOutaHere( buffy );
      System.out.println( "Got this: " + buffy.toString() );
   }
}


You could also create your own mutable object and mutate it like all get-out with every call. This is a good way to create code that is very "interesting" for others to read (just be sure that you don't sign your code when you do this).

Flat View: This topic has 7 replies on 1 page
Topic: please help get compiler error Previous Topic   Next Topic Topic: Compilation Error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use