I'm a beginner in Java and was wondering if there's a way I could return something like a String multiple times without having to write. return string+string+string+string; Thanks
Assuming you meant javascript and not java, since that's where you posted (They are two completely unrelated languages that unfortunately share a name thanks to a back-room deal between Sun and Nyetscape almost a decade and a half ago)... ... and .. no. There's no built in method for that. You could use a FOR loop and another variable to build the return result though, but that's an ugly solution at best in terms of speed and memory use -- though if I had a LOT of them added together like that, I'd consider it. If there were dozens used in multiple places, I'd make my own function for that. function replicateString(inString,count) { var newString=''; while (count-- > 0) newString+=inString; return newString; } Code (markup):
Its not possible to have multiple returns in Java. But its possible to return multiple items using a wrapper object. You can create a wrapper object which has the String as data members, u can then set values for each member and return the object. Here's a sample wrapper class with a argument constructor. class MyStringWrapper { String item1; String item2; String item3; public MyStringWrapper(String item1, String item2, String item3) { this.item1 = item1; this.item2 = item2; this.item3 = item3; } } Code (markup): You can use this as follows in your code: MyStringWrapper wrapperObject = new MyStringWrapper(String1, String2, String3); return wrapperObject; Code (markup): The object members can then be accessed as 'wrapperObject.item1'