consider two conditions:
1) we give the statement in java file
2) we save the java file in a window folder named as mypackage.
in short difference between a package and a window directory ?
a package in java is a namespace. it "assembles" related class files under a common "tree". a directory is file which holds other folders/files. the directory structure where you save your .java files should match package structure.
A package is a name space to which a class belongs. It determines the fully qualified name of a class, and which classes can interact with each other and how.
A directory is a structure on the file system that helps organize files.
The javac compiler and the JVM by default look for source and class files in a directory path that reflects the package name. This is not required though. Package names are an integral part of Java code. Directories can be used to store files in, but don't necessarily have anything to do with Java.
thanks for your reply
i agree with the answer given above
but what i have read that we use packages so that user need not to concern about the file name , same we can do by creating the directory manually.
any advantage of using package over directory
The two are not related, they serve a different purpose.
Let's assume we have two independent developers, who happen to create a class with the same name. Let's call it MainClass. What if a client used software by both these developers? How would the client's code know which of the two classes to use if you referred to MainClass? Package names provide developers a way to assign classes to a name space, so that different classes can be more easily distinguished: dev.one.MainClass is different from dev.two.MainClass.
Now, when you tell the JVM to run dev.one.MainClass, how would the class loader know where to find classes it refers to? Using a directory structure that's similar to the package name makes it very easy to locate these files.
So why don't we get rid of the package statements and just determine which class we are using from their directory structure alone? Because some classes aren't located in directories. Classes can be loaded from a network, or can even be created and loaded at runtime, without ever existing on a file system.
Hopefully you now see why we need both directory structures and package statements, because both fulfill a different role.