Computer Science Canada

[Tip] Packages

Author:  wtd [ Sat May 27, 2006 5:20 pm ]
Post subject:  [Tip] Packages

Learn to use packages well, and learn it early in your Java education.

Author:  cool dude [ Sat May 27, 2006 5:24 pm ]
Post subject: 

by packages do u mean the different things you can import i.e.
code:

import java.io.*;


i think there are a lot of those where can i find some of them? any links?

Author:  wtd [ Sat May 27, 2006 5:28 pm ]
Post subject: 

http://java.sun.com.

Look at the API references.

You should also know how to create your own packages.

Author:  cool dude [ Sat May 27, 2006 5:45 pm ]
Post subject: 

1) how do u create packages?

2)there are quite a lot of packages there! what are usually the main ones people use?

Author:  MysticVegeta [ Mon May 29, 2006 1:30 pm ]
Post subject: 

cool dude wrote:
by packages do u mean the different things you can import i.e.
code:

import java.io.*;


i think there are a lot of those where can i find some of them? any links?


Packages are actually bundles of classes, so if you import an entire package, you can use any class inside of that package but if you just want to use 1 or more specific classses from a package, then people usually import those classes specifically and not th eentire package... eg:

if you import java.io package, you import classes:

Quote:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
ByteArrayInputStream
ByteArrayOutputStream
CharArrayReader
CharArrayWriter
DataInputStream
DataOutputStream
File
FileDescriptor
FileInputStream
FileOutputStream
FileReader
FileWriter
FilterInputStream
FilterOutputStream
FilterReader
FilterWriter
InputStream
InputStreamReader
LineNumberInputStream
LineNumberReader
ObjectInputStream
ObjectOutputStream
ObjectStreamClass
OutputStream
OutputStreamWriter
PipedInputStream
PipedOutputStream
PipedReader
PipedWriter
PrintStream
PrintWriter
PushbackInputStream
PushbackReader
RandomAccessFile
Reader
SequenceInputStream
StreamTokenizer
StringBufferInputStream
StringReader
StringWriter
Writer


if you just need 1 of the class say, BufferedInputStream and not hte entire package, then you would go

code:
import java.io.BufferedInputStream;


I may not be the best explainer because I am pretty new to java myself, hopefully wtd will correct me anywhere I made a mistake...

Author:  cool dude [ Mon May 29, 2006 4:29 pm ]
Post subject: 

wat difference would it make if i just import the whole package?

Author:  wtd [ Mon May 29, 2006 4:42 pm ]
Post subject: 

Well, let's say you have package foo with class Bar, and package baz with classes Bar, Ninja and Wooble. What happens if you import all of both packages, then try to use the Bar class from package foo?

Author:  rizzix [ Mon May 29, 2006 7:59 pm ]
Post subject: 

MysticVegeta wrote:


Packages are actually bundles of classes, so if you import an entire package, you can use any class inside of that package but if you just want to use 1 or more specific classses from a package, then people usually import those classes specifically and not th eentire package... eg:
(A common mistake, but it needs to be corrected. Java's import (packages) does not behave like C++'s using (namespaces), although they both finally bring a "name" into scope.)

You Do Not (and Can Not) import packages in Java. You can only import classes

Java:
import java.io.*;
Read: "Import all classes in the java.io package".
The asterisk (*) represents all classes. If you wish you may import just one particular class from a package.
Java:
import java.io.Reader;
Here we import the Reader class from the java.io package.

Author:  rizzix [ Mon May 29, 2006 8:16 pm ]
Post subject: 

Another tip: Following the java naming conventions, all package names should be in lowercase, while Class names should be in word case.
(note: package names are not wordcase)

As a general rule: avoid underscores.

Method and variable names on the other hand are also word case, but they should begin with a lowercase letter.


: