Return different type of data from a method in java?

前端 未结 13 1516
别那么骄傲
别那么骄傲 2020-12-05 02:18
public static void main(String args[]) {
    myMethod(); // i am calling static method from main()
 }

.

public static ? myMethod(){         


        
13条回答
  •  忘掉有多难
    2020-12-05 03:11

    @ruchira ur solution it self is best.But i think if it is only about integer and a string we can do it in much easy and simple way..

     class B {   
        public String myfun() {         
            int a=2;           //Integer .. you could use scanner or pass parameters ..i have simply assigned
            String b="hi";      //String
            return Integer.toString(a)+","+b; //returnig string and int with "," in middle          
        }       
    }
    
    class A {    
        public static void main(String args[]){
    
            B obj=new B();  // obj of class B with myfun() method  
            String returned[]=obj.myfun().split(",");
                 //splitting integer and string values with "," and storing them in array   
            int b1=Integer.parseInt(returned[0]); //converting first value in array to integer.    
            System.out.println(returned[0]); //printing integer    
            System.out.println(returned[1]); //printing String
        }
    }
    

    i hope it was useful.. :)

提交回复
热议问题