A
Alan
I was wondering why Java behaves this way. . . . Below are a few
lines of code with nearly duplicate methods called for a file and then
a directory. One method (methodWithoutNew) just assigns a File
variable to the input File object (no "new" operator). The other
method (methodWithNew) uses the "new" operator to create its file
object with the same name as the input File object.
When checking whether or not the various files exist at different
points, the only difference in results is that the directory is not
observed to exist in the method that uses the "new" operator.
However, the non-directory file does exist in that same method.
What is happening here? Why the difference?
Also, why don`t I need the "new" operator to create a File
object?
Both the execution results and the code are shown below.
Thanks, Alan
Results:
----jGRASP exec: java CallWithFile
. . . in methodWithoutNew . . .
File CallWithFile.java exists.
. . . in methodWithNew . . .
File CallWithFile.java exists.
. . . in Main . . .
File CallWithFile.java exists.
. . . in methodWithoutNew . . .
File My Java Code exists.
. . . in methodWithNew . . .
File My Java Code does not exist!!!
. . . in Main . . .
File My Java Code exists.
----jGRASP: operation complete.
Code:
<sscce>
import java.io.*;
import java.util.*;
public class CallWithFile
{
public static void main(String[] args)
{
File afile = new File("CallWithFile.java");
methodWithoutNew( afile );
methodWithNew ( afile );
System.out.println(" . . . in Main . . . ");
if ( afile.exists() )
{ System.out.println( afile.getName() + " exists." );}
else
{ System.out.println( afile.getName() + " does not exist!!!" ); }
File directory = new File(System.getProperty("user.dir"));
methodWithoutNew( directory );
methodWithNew ( directory );
System.out.println(" . . . in Main . . . ");
if ( directory.exists() )
{ System.out.println( directory.getName() + " exists." );}
else
{ System.out.println( directory.getName() + " does not
exist!!!" ); }
}
public static File methodWithoutNew(File infile)
{
File file = infile;
System.out.println(" . . . in methodWithoutNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }
return file;
}
public static File methodWithNew(File infile)
{
File file = new File(infile.getName());
System.out.println(" . . . in methodWithNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }
return file;
}
}
<\sscce>
lines of code with nearly duplicate methods called for a file and then
a directory. One method (methodWithoutNew) just assigns a File
variable to the input File object (no "new" operator). The other
method (methodWithNew) uses the "new" operator to create its file
object with the same name as the input File object.
When checking whether or not the various files exist at different
points, the only difference in results is that the directory is not
observed to exist in the method that uses the "new" operator.
However, the non-directory file does exist in that same method.
What is happening here? Why the difference?
Also, why don`t I need the "new" operator to create a File
object?
Both the execution results and the code are shown below.
Thanks, Alan
Results:
----jGRASP exec: java CallWithFile
. . . in methodWithoutNew . . .
File CallWithFile.java exists.
. . . in methodWithNew . . .
File CallWithFile.java exists.
. . . in Main . . .
File CallWithFile.java exists.
. . . in methodWithoutNew . . .
File My Java Code exists.
. . . in methodWithNew . . .
File My Java Code does not exist!!!
. . . in Main . . .
File My Java Code exists.
----jGRASP: operation complete.
Code:
<sscce>
import java.io.*;
import java.util.*;
public class CallWithFile
{
public static void main(String[] args)
{
File afile = new File("CallWithFile.java");
methodWithoutNew( afile );
methodWithNew ( afile );
System.out.println(" . . . in Main . . . ");
if ( afile.exists() )
{ System.out.println( afile.getName() + " exists." );}
else
{ System.out.println( afile.getName() + " does not exist!!!" ); }
File directory = new File(System.getProperty("user.dir"));
methodWithoutNew( directory );
methodWithNew ( directory );
System.out.println(" . . . in Main . . . ");
if ( directory.exists() )
{ System.out.println( directory.getName() + " exists." );}
else
{ System.out.println( directory.getName() + " does not
exist!!!" ); }
}
public static File methodWithoutNew(File infile)
{
File file = infile;
System.out.println(" . . . in methodWithoutNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }
return file;
}
public static File methodWithNew(File infile)
{
File file = new File(infile.getName());
System.out.println(" . . . in methodWithNew . . . ");
if ( file.exists() )
{ System.out.println( file.getName() + " exists." );}
else
{ System.out.println( file.getName() + " does not exist!!!" ); }
return file;
}
}
<\sscce>