In-depth understanding of Java package example tutorials
Java引入包(package)机制,提供了类的多层命名空间,用于解决类的命名冲突、类文件管理等问题。
Java允许将一组功能相关的类放在同一个package下,从而组成逻辑上的类库单元。如果希望把一个类放在指定的包结构下,我应该在Java源程序的第一个非注释行放置如下格式的代码:
package packagename;
一旦在Java源文件中使用了这个package语句,就意味着该源文件里定义的所有类都属于这个包。位于包中的每个类的完整类名都应该是包名和类名的组合,如果其他人需要使用该包下的类,也应该使用包名加类名的组合。
javac -d . Hello.java 与 javac Hello.java 的区别? package lee;public class Hello{ public static void main(String[] args) { System.out.println("Hello World!"); } }
-d选项本来用于设置编译生成的class文件的保存位置,这里指定将生成的class文件放在当前路径下。使用该命令编译后,当前路径下生成一个名为lee的文件夹,该文件夹下则有一个Hello.class文件。
如果直接使用javac Hello.java命令编译,将会在当前路径下生成一个Hello.class文件,而不会生成lee文件夹。
同一个包中的类必须位于同一个文件夹吗?
可以不在同一个文件夹。例如lee.Person和lee.PersonTest两个类可一个在C盘一个在D盘,只要让CLASSPATH环境变量包含这两个路径即可。
包名如何起?
包名一般全是小写字母,由一个或多个有意义的单词连缀而成。为了在开发中避免包名和类名同时重复的情况,Oracle建议使用公司Internet域名倒写来作为包名,例如Internet域名是crazyit.org,则该公司的所有类都放在org.crazyit包及其子包下。
域名倒写.项目名.模块名.组件名
同一个包下的类互相访问时可以不加包前缀吗?
可以。
父包和子包互相访问时可以不加包前缀吗?
不可以,必须加包前缀。
import关键字
正如上面看到的,如果需要使用不同包中的其他类时,总是需要使用该类的全名,这是一件繁琐的事情。为了简化编程,Java引入了import关键字,import关键字可以向某个java文件只能给导入指定包层次下的某个类或全部类,import语句应该出现在package语句之后、类定义之前。一个Java源文件只能包含一个package语句,但可以包含多个import语句,多个import语句用于导入多个包层次下的类。
导入单个类: import package.subpackage...ClassName; 导入全部类: import package.subpackage...*;
上面的(*)只能代表类,不能代表包。
Java的常用包
Java的核心类都放在java这个包及其子包下,Java扩展的许多类都放在javax包及其子包之下。这些实用类也就是前面所说的API(应用程序接口),Oracle按这些类的功能分别放在不同的包下。下面几个包是Java中的常用包。
java.lang:这个包下包含了Java语言的核心类,如Sting、Math、System、和Tread类等,使用这个包下的类无需使用import语句导入,系统会自动导入这个包下的所有类。
java.util:这个包下包含了Java的大量工具类/接口和集合框架类/接口,例如Arrays和List、Set等。
java.net:这个包下包含了一些Java网络编程相关的类/接口。
java.io:这个包下包含了一些Java输入/输出相关的类/接口。
java.text:这个包下包含了一些Java格式化相关的类。
java.sql:这个包下包含了一些Java进行JDBC数据库编程相关的类/接口。
java.awt:这个包下包含了抽象窗口工具集(Abstract Window Toolkits)的相关类/接口,这些类主要用于构建图形用户界面(GUI)程序。
java.swing:这个包下包含了Swing图形用户界面编程的相关类/接口,这些类可用于构建平台无关的CUI程序。
【相关推荐】
2. java软件工程师学php -15 php中的namespace 与java中的package
3. Sublime text 2/3 中 Package Control 的安装与使用方法
4. 图解idea不能新建JavaClass和Package解决办法
The above is the detailed content of In-depth understanding of Java package example tutorials. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
