Bifalgorithm post · Computer science one · Program Structure

How do packages and imports organize Java code?

How do packages and imports organize Java code? (Computer science one · Program Structure) — "Java code is organized into modules called packages. Packages are…

Ideas in this flowchart

  • Los paquetes organizan el código Java y sus clases.
  • Un paquete es esencialmente un directorio dentro de una estructura de árbol.
  • La estructura permite subdirectorios y directorios separados al mismo nivel.
  • El paquete raíz se denomina paquete predeterminado.
  • Una declaración package indica a qué paquete pertenece un archivo fuente.
  • La declaración usa nombres de directorios separados por puntos y termina con punto y coma.
  • Por ejemplo, package unl.cse; ubica el archivo en cse, subdirectorio de unl.
  • Sin declaración package, el archivo pertenece al paquete predeterminado.
  • Los paquetes agrupan archivos fuente y funcionalidades relacionadas.
  • También proporcionan espacios de nombres separados para las clases.
  • Varias clases con el mismo nombre pueden coexistir en paquetes diferentes sin conflicto.
  • Una instrucción import permite usar una clase mediante su nombre de paquete completamente calificado.
  • Por ejemplo, import java.util.Scanner; incorpora Scanner para leer la entrada estándar.
  • Las clases de java.lang, como String y Math, se importan automáticamente.
  • El comodín import java.util.*; importa todas las clases del paquete.
  • Usar comodines generalmente se considera una mala práctica porque no es intencional ni específico.
  • Los nombres de paquetes deben seguir las reglas generales de los identificadores.
  • La convención utiliza minúsculas con guiones bajos, como here_is_an_example.
  • Los directorios superiores son más generales y los subdirectorios son más específicos.
  • unl.cse ejemplifica una organización de lo general a lo específico.
Feed/

How do packages and imports organize Java code?

No cover

Channel

Computer science one

1 page

"Java code is organized into modules called packages. Packages are essentially directories (or folders) which follow a directory tree structure which allows subdirectories and separate directories at the same level. It all starts at the root directory called the “default” package. Within a source file, we declare which package the file belongs to using the keyword package followed by a fully qualified package declaration which is essentially just the names of the directories that the file is located in, separated by a period. The declaration is terminated by a semicolon. For example, the package declaration, package unl.cse; would indicate that the file belongs in the directory cse which is a subdirectory of the directory unl. The absence of a package declaration will mean that the file is associated with the default directory. Packages allow you to organize source files and code functionality. Mathematics related classes can be placed in one package while image related classes can be placed in another, etc. It also provides separate name spaces for classes. There could be 3 or 4 different classes named List for example. They could not be located in the same directory; if you wanted to use one which one would you be referring to? By separating them into packages, they can all exist without conflict. When we want to use a particular one, we import that class with its fully qualified package name. An import statement essentially “brings in” another class so that its methods and functionality can be used. For example, there is a class named Scanner, located in the package java.util, that makes it easy to read input from the standard input. To include it in our program so that we can use its functionality, we would need to import it: import java.util.Scanner; Classes in the package java.lang, such as String and Math, are considered standard and are imported by default without an explicit import statement. You may see some code that uses a wildcard like import java.util.*; which ends up importing every class in that package. This is generally considered bad practice. In general, code should be intentional and specific, importing every class even if they are not used goes against this principle. When naming packages, you must follow the general naming rules for identifiers. Package names cannot begin with a number, no whitespace, etc. Moreover, the general convention for package names is to use lowercase underscore casing, here_is_an_example. Moreover, packages and subpackages follow the same convention as directories: the top most directory is the most general and subdirectories are more and more specific. In many of our examples we’ll use unl.cse, which illustrates this general-to-specific organization."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.