English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Firstly, how do we exit from a function(not the program) expllicitly from anywhere inside the function? for e.g. in VB we simply write Exit Sub;

And secondly, if i have an array created using:
String arr[][] = new String[100][5];
and all its elements initialized to 0..if in the same program i re-initialize it like arr = new String[1][1], so this way i'll be releasing memory rite? (given that theres no "delete" keyword in java)

2007-03-21 19:34:27 · 3 answers · asked by kenshin 3 in Computers & Internet Programming & Design

3 answers

1. Use return statement to come out of a function in java.
2. Yes, it will create a new object and assign it to this reference. If you have not assigned the previous object's reference to some other variable, it will eventually be freed by the garbage collector.

2007-03-21 20:39:33 · answer #1 · answered by cantfindanewidentity 1 · 0 0

to exit a function throw an exception and catch it somewhere outside the function in the program

about the array u r right i guess, and there is no delete keyword, but there is the garbage collector, u can use it to clean memory.. ( System.gc() )

2007-03-22 02:47:46 · answer #2 · answered by abd 5 · 0 0

(1) Simply use 'return'
Example

class Demo {
public static void main( String args[] )
{
method1();
int x = method2();
String str = method3();
}
public static void method1()
{
..........
return;
..........
}
public static int method2()
{
.........
return 1;
..........
}
public static String method3()
{
..............
return "Bye";
..............
}
}
/**********************************************/
(2)
In Java, you don't have to worry at all about releasing the memory. The 'garbage collector will do it for you automatically once the memory has no reference to it.

Or (for your statement above) you simply can type the following to force the garbage collector to do its work immediately...
arr = null;
System.gc();

2007-03-26 00:51:14 · answer #3 · answered by Liviawarty J 2 · 0 0

fedest.com, questions and answers