-3

I want to return two values from java method to java script..can you help me.?? would be grateful for help..

Vicky
  • 1,215
  • 6
  • 22
  • 45
  • Haveyou tried returning an array or list or map or an object containing the values e.g. a Pair? – Peter Lawrey Apr 25 '12 at 09:45
  • Your question lacks of context. Would you care to elaborate? Tell us about your environment, technologies you 're using, the code you have so far, etc... Just like that, it does not make any sense – Guillaume Polet Apr 25 '12 at 09:45
  • @Guillaume: JAVA method: A() { return a,b;//tell me right way here...both are integer values. } java script method: B() { //somewhere calling java method. i want a and b here } – Vicky Apr 25 '12 at 09:51
  • @peter: i really happy about your reply...yes i did try...i succeed too..but i am asking for simple way because i want to return only 2 values. – Vicky Apr 25 '12 at 09:54
  • 1
    I would `int[] ret = { a, b }; return ret;` from Java. – Peter Lawrey Apr 25 '12 at 09:58
  • I think I am missing something here: what is a Java *script*? – thkala Apr 25 '12 at 10:11

3 Answers3

1

I would

int[] ret = { a, b }; 
return ret; 

or

return new int[] { a, b };

from Java.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Java doesn't allow for returning more than one value from a method; you would either have to pack the values you want to return into some wrapper object (possibly one you write yourself) or - if they share a common type - into some set/list/array.

See this answer for details.

Community
  • 1
  • 1
G. Bach
  • 3,869
  • 2
  • 25
  • 46
0

To return two values from one method, you must encapsulate the result into one Object. That object can be as simple as an array of length 2 or as any Collection.

You may also write your own return type and return it from your method, such as:

public class MyReturnType {
    private Type1 returnedObject1;
    private Type2 returnedObject2;
    // getters and setters
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118