Consolidate the basis of Java Series 5: Java and Java package file structure

table of Contents

- Java class

This series of articles will arrange to me on GitHub's "Java Interview Guide" warehouse, more exciting content please go to my warehouse View

https://github.com/h2pl/Java-Tutorial

Like it under the trouble spots Star Kazakhstan

Article first appeared in my personal blog:

www.how2playlife.com

This article is a micro-channel public number [of] Java technology quack "reinforce the Java Foundation series Bowen," in which a Part of this article from the network, to the subject of this article speaks clear and thorough, and I think a lot of good integration of technology blog content, which cited a number of good blog posts, if infringement, please contact the author.
This blog series will show you how to from entry to advanced, step by step to learn the basics of Java, and started actual combat, then understand Java knowledge to realize the principles behind each point, a more complete understanding of the entire Java technology system, to form their own intellectual framework. In order to summarize and test your learning outcomes, this series will provide every knowledge point corresponding interview questions and suggested answers.

If you have any suggestions for this series of articles, or have any questions, you can also concerned about the number of public rivers and lakes [] Java technology to contact the author, you are welcome to participate in the creation and revision of this blog series.

Package concept in Java

Java packet is encapsulated in a set of classes, interfaces and mechanisms of sub-packets. Packages for:

Prevent naming conflicts. For example, there are two names are Employee class, college.staff.cse.Employee and college.staff.ee.Employee
more easily search / locate and use classes, interfaces, enumerations, and annotation

Provide controlled access: protected and there is a default packet-level access control. Protected class members can access the same package and subclasses by. Default member (no access specifier) ​​can only be accessed by the class of the same package.

Encapsulated data packet may be considered (or hidden data).

We need to do is to related classes into the package. After that, we can simply write a class imported from an existing package, and for our program. A package is a set of related classes of containers, some of which can access the class, other classes are saved and used for internal purposes.
We can pack as much as possible to reuse the existing class in the program.

In order to better organize the class, Java provides a package mechanism for the difference between the class name of the namespace.

Role package

  • 1, the functionally similar or related tissue class or interface in the same package, a class to find and easy to use.
  • 2, as in the same folder, the package also used storage directory tree. Classes in the same package name is different, the name of the class in a different package is the same, while simultaneously calling two different classes in the same package name of class, distinction should be added to the package name. Therefore, the package name to avoid conflict.
  • 3, the package also defines access rights, have access to the package of the class can access a package of classes.

Java uses package (package) This mechanism is to prevent naming conflicts, access control, providing search and locate class (class), interfaces, enumerations (enumerations) and comments (annotation) and so on.

包语句的语法格式为:

package  pkg1[.pkg2[.pkg3…]];

例如,一个Something.java 文件它的内容

package  net.java.util; public  class  Something{ ... }

Then its path should be  net / java / util / Something.java  so saved. action package (package) is to classify the different java program saved more easily be called by other java program.

A package (package) can be defined as a set of types (classes, interfaces, enumerations, and annotation) interconnected to provide access protection and namespace management functions for these types.

The following are some of the Java package:

  • the java.lang - package basis of class
  • the java.io - function of the input and output functions comprising

Developers can own a set of classes and interfaces package, and define their own package. And to do so in the actual development is to be encouraged, and when you complete the implementation of the class, grouping related classes, so that other programmers can more easily determine which classes, interfaces, enumerations, and comments are related .

Because the package creates a new namespace (namespace), so without naming conflicts with the name of any other packages. Use package this mechanism, easier to implement access control, and let locate the relevant class easier.

package directory structure

Class on the package will have two main results:

  • Package name becomes part of the class name, as we discussed earlier.
  • The directory structure must be located under the package name corresponding byte codes match.

Here is a simple way to manage your own java in the file:

The type of class, interface source code in a text, the name of this file is the name of this type, and to .java extension. E.g:

// File Name: Car.java package vehicle; public class Car {// class implementation}

Next, the source files in a directory, the directory where the class name corresponds to the package.

....\vehicle\Car.java

Now, the correct class name and path will be look like the following:

  • Class name -> vehicle.Car
  • Pathname -> vehicle \ Car.java (windows in the system)

Typically, a company uses its reversed form of Internet domain names to its package names such as: Internet domain name is runoob.com, all the package names are beginning to com.runoob. Each portion of the packet corresponding to a subdirectory name.

For example: There is a  com.runoob.test  package, this package contains the source file called Runoob.java, then the appropriate, should be like following a series of subdirectories:

.... \ com \ runoob \ test \ Runoob.java

Compile time, the compiler creates a different output file, the output file name is the name of this type is the type of each class, interface defined in the package, plus as an extension .class suffix. E.g:

// File name: Runoob.java package com.runoob.test; public class Runoob {} class Google {}

Now, we use the -d option to compile the file as follows:

$javac -d .  Runoob.java

This will place the compiled file such as the following:

. \ Com \ runoob \ test \ Runoob.class. \ Com \ runoob \ test \ Google.class

So you can import all look like  \ com \ runoob \ test  defined in the class, interface:

import com.runoob.test.*;

.class file after compiling .java source files should be the same, they should be placed in the directory together with the corresponding name of the package. However, it does not require the same path to the .class file with the path of the corresponding .java. You can arrange separate source directory and classes.

 
 
  
  \ Sources \ com \ runoob \ test \ Runoob.java 
  
  
   
   \classes\com\runoob\test\Google.class
  
  
 
 

In this way, you can share your classes directory to other programmers without having to disclose their source. In this way manage their source code and class files can let the compiler and the java virtual machine (JVM) can find all types used in your program.

Absolute path to the directory category called  class path . Set the system variable  CLASSPATH  in. Compiler and the java virtual machine by the package name to the later path construction .class file class path.

\ Classes is the class path, package name is com.runoob.test, and the compiler and JVM will \ Classes \ com \ runoob \ test in looking for .class files.

A class path might contain several paths, multi-path should be separated by a delimiter. By default, the compiler and JVM find the current directory. JAR file contains the class by the Java platform dependent, so they default on a directory in the class path.

Set the CLASSPATH system variable

With the following command displays the current CLASSPATH variable:

  • Windows platforms (DOS command line): C:> set CLASSPATH
  • UNIX platforms (at Bourne shell): # echo $ CLASSPATH

Delete the current CLASSPATH variable content:

  • Windows platforms (DOS command line): C:> set CLASSPATH =
  • UNIX 平台(Bourne shell 下):# unset CLASSPATH; export CLASSPATH

Set CLASSPATH variable:

  • Windows platforms (DOS command line): C:> set CLASSPATH = C: \ users \ jack \ java \ classes
  • UNIX 平台(Bourne shell 下):# CLASSPATH=/home/jack/java/classes; export CLASSPATH

Java package (package) Detailed
java package in order to distinguish the role of the class name of the namespace  

1, the functionally similar or related tissue class or interface in the same package, a class to find and easy to use. ,

2, as in the same folder, the package also used storage directory tree. A package of the same name is different classes, the class names are different packages may be the same,

When two different classes at the same time calling packages in the same class name, package name should be added distinction. Therefore, the package name to avoid conflict.

3, the package also defines access rights, have access to the package of the class can access a package of classes.

Create a package
when the package is created, you need to take an appropriate name for this package. After that, if a source file contains additional classes provided by this package, interface, enum or annotation type, the package must be declared at the beginning of the source file.

Package should be declared in the first line of the source file for each source file can have only one package statement, the file for each type are applied to it.

If no source file declare a package, then one class, function, an enumeration, comments, etc. will be placed in an unnamed package (unnamed package) in.

Examples
Let's look at an example that created a package called the animals. Typically use lowercase letters to avoid naming classes, interfaces names of conflict.

Animals added to a packet interface (interface):

package animals;
  
interface Animal {
   public void eat();
   public void travel();
}

  Subsequently, the interface is added to achieve the same package:

package animals;
  
/* 文件名 : MammalInt.java */
public class MammalInt implements Animal{
  
   public void eat(){
      System.out.println("Mammal eats");
   }
  
   public void travel(){
      System.out.println("Mammal travels");
   }
  
   public int noOfLegs(){
      return 0;
   }
  
   public static void main(String args[]){
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

import keyword
To be able to use a member of a package, we need to explicitly import the package in Java programs. Using the "import" statement can perform this function.

In java source file import statement after statement should be in the package, all previously defined classes, you can not, you can also have multiple, its syntax is:

. 1
Import Package1 [.package2 ...] (className | *);.
  If a package, a class that you want to use another class of this package, then the package name can be omitted.

Typically, a company uses its reversed form of Internet domain names to its package names such as: Internet domain name is runoob.com, all the package names are beginning to com.runoob. Each portion of the packet corresponding to a subdirectory name.

For example: There is a com.runoob.test package, this package contains the source file called Runoob.java, then the appropriate, should be like following a series of subdirectories:

1
.... \ com \ runoob \ test \ Runoob.java

Common jar package

Type java package

Type packages have built-in and user-defined packages of software packages built package
these packages by a large number of classes, these classes are part of the Java API. Some common built-in packages are:

1) java.lang: contains language support classes (e.g. classified, for defining basic data types, mathematical operation). The package will be automatically imported.

2) java.io: categorical to support input / output operations.

3) java.util: image category comprising a list of links to practical use, data dictionaries and support structure; a date / time of the operation.

4) java.applet: contains the classes used to create Applets.

5) java.awt: contains classes used to implement the graphical user interface components (such as buttons, menus, etc.).

6) java.net: comprising class supports network operation.

dt.jar

SUN对于dt.jar的定义:Also includes dt.jar, the DesignTime archive of BeanInfo files that tell interactive development environments (IDE's) how to display the Java components and how to let the developer customize them for the application。

Chinese translation is: dt.jar is DesignTime BeanInfo archive file, BeanInfo file is used to tell the Integrated Development Environment (IDE) how to display the Java components and how to let developers customize them according to the application. This text is mentioned in a few keywords: DesignTime, BeanInfo, IDE, Java components. In fact dt.jar is DesignTime abbreviation of the Archive. So what is DesignTime.

    What is DesignTime? Translates design. In fact, people know to understand JavaBean design time and runtime (runtime) the meaning of these terms. Design (DesignTIme) is the time by adding controls, set the control or form properties such as ways to build applications in a development environment.
  
    This operation should be relatively time (Runtime) refers to as a user can interact with the application as time. So now and then understand how the above translation, in fact, contains dt.jar swing control of BeanInfo, and IDE's GUI Designer requires this information. So let's look at what in the end there dt.jar in? Below is a screenshot dt.jar the following contents:

image

    As it can be seen from the above screenshot, dt.jar BeanInfo Swing in all components. So in the end what is BeanInfo it?

    What is BeanInfo? JavaBean and BeanInfo have a great relationship. JavaBean specification developed by Sun, is largely prepared for the IDE - IDE allows it to set JavaBean properties in a visual way. If a visual application development in the IDE, we need a variety of components to customize the application by way of property settings, IDE through the property editor lets developers use visual way to set the properties of the component.

    General support IDE property editor JavaBean specification defined when the component developer to release a component, it is often a component corresponding property editor bundled release, so developers can easily use the property editor in the IDE environment components for custom work. JavaBean specification defined by java.beans.PropertyEditor JavaBean property set methods described by BeanInfo JavaBean which attributes are customizable, also describes a corresponding relationship between custom attributes of PropertyEditor.

    And a correspondence relationship between BeanInfo JavaBean, by naming specification established therebetween: BeanInfo corresponding to a JavaBean naming convention is as follows: BeanInfo. When JavaBean its properties together with the same editor component registered to the IDE, when JavaBean be customized development interface, IDE will find the corresponding BeanInfo according JavaBean specification, to find JavaBean properties described in the description information BeanInfo ( It is open, which use the property editor), and then generate a specific development editor interface JavaBean.

    dt.jar which is mainly BeanInfo swing components. Based on these IDE BeanInfo show how these components as well as developers to customize them.

rt.jar

rt.jar is the runtime archive. Java Foundation Classes, which is the Java doc file inside to see the class of all classes.

image

 rt.jar default in the loading path Root Classloader the inside, arranged in Claspath the variable is not required; while in the other jar jre / lib directory: jce.jar, jsse.jar, charsets.jar, resources.jar Root Classloader are in.

* .Java files mysteries

* .Java file Profile

.java file you can think of just a text file, the file that is written in java program, or code block task.

Is the .class file is essentially a binary file, which is generally the .java files generated by this command javac (jdk tool itself provides a) a file, the file can be loaded by the JVM (Java Virtual Machine) (class loader) and then interpreted into java, which is to run your program.

You can also compare this:
.java and .c, .cpp, .asm file and so on, essentially the same, but with a language to describe how you want to accomplish something (a task), and this computer language itself is no way to know what is the meaning of it oriented itself just programmers, programmers can (grammar) to describe the task or organization through the language itself, which is called programming.

Finally, of course, you need a computer in accordance with your intentions to run your program, this time it has come a translator (compiler, assembler, linker, and so complicated process) turn it into a machine-understandable instructions (which is to say everyone machine language, machine language itself is a programming language, but the program is hard to write, hard to read, basically there is no way maintenance).

Here .class file architecture in the corresponding calculation is essentially a machine language (machine and here called JVM), it is possible to run the JVM itself here .class files directly. So you can think further, .java and .class and other programming syntax, as they are used by programmers to describe a language of their own tasks, but they are not the same as an object-oriented, and the computer itself can only recognize it those instructions own definition of what (re-emphasized, the computer itself here not so strict definition)

In short:

.java Java source file extension is stored inside the function code written by the programmer.

.class file is a byte code file generated by the file javac command to compile the source .java files. Binaries can run on any Java virtual machine hardware platforms and operating systems.

.class files are not local executable program. Java virtual machine is to run in order to achieve .class file to run the program.

Why a java source file can have only one public class?

  In the java programming ideas (fourth edition) a book such words section 3 (Access 6.4 class):

  1. Each compilation unit (file) can have only one public class, which means that each compilation unit has a single common interface with a public class performance. The interface can contain a number of required support package access class. If more than one public class within a compilation unit, the compiler will give an error message.

  2.public class name must contain the file name of the same compilation unit, comprising a case. If not, the same will get a compiler error.

  3. Although not very common, but completely within the compilation unit with no public class is also possible. In this case, you are free to name the file.

Summarizes several questions related to:

1, a ".java" source files may include a plurality of whether the class (not an internal class)? What are the limits?

  A: You can have more than one class, but only one public class, and the public class name must be consistent with the file name.

2, why a file can have only one public class

  A: The compiler at compile time for a java source code files (also known as "compilation unit") only accept a public class. Otherwise error.

3, if you can not java file class public

  A: public class is not necessary, java file can no public classes.

4, why the class name of the class and the public must be the same as the file name

  A: In order to facilitate a virtual machine is to find the corresponding class bytecode files corresponding to the respective paths.

Main method

Main function: is a special function, as the program entry, may be invoked JVM

The main function is defined:

public: This function represents the largest access

static: As the representative of the main function to load classes already exist

void: no specific main function return value

main: not a keyword, but a special word, it can be JVM identity

(String [] args): parameter function of the parameter type is an array, the array element of division of the string, the string array. temporal length of this array main (String [] args) string array is 0, but may be passed to which parameters at run time.

The main function of a fixed format, JVM identification

The main function can be overloaded, but the JVM only recognizes main (String [] args), the other is as a general function. This inside knowledge args array variable can change, can not change the other.

Java file may contain a number of classes, each class has only one main function, but each java file can contain multiple primary functions, in operation, need to specify which entry JVM. The main function, for example, a class can call the main function of another class. We will not necessarily use the primary function of public classes.

Access outside the class

External class can only be modified by the public and default.

Why do modifications to the external class or type it?

1. The present package concept: public default and can distinguish the external division of the class can be a different packet (a modified default classes in other packages not introduced into this class, the class can be modified public Import)

2.protected is visible within the package and sub-category visible, but when an external class want to inherit a protected modified unusual bags, are they not find this class, not to mention the several layers

3.private modified outside of class, or any other external classes can not import it.

//Java中的文件名要和public修饰的类名相同,否则会报错
//如果没有public修饰的类,则文件可以随意命名
public class Java中的类文件 {

}

//非公共开类的访问权限默认是包访问权限,不能用private和protected
//一个外部类的访问权限只有两种,一种是包内可见,一种是包外可见。
//如果用private修饰,其他类根本无法看到这个类,也就没有意义了。
//如果用protected,虽然也是包内可见,但是如果有子类想要继承该类但是不同包时,
//压根找不到这个类,也不可能继承它了,所以干脆用default代替。
class A{

}

Naming Java packages

. At the beginning of the java * Java core package, all programs will use these classes in a package;

To javax. * The beginning of the expansion pack, x is the extension of meaning is extended. Although javax. * Is the java. * Optimization and expansion, but because javax. * The increasing use of many programs rely on javax. , So. Javax also part of the core, and also together with the JDK release.

To org. * At the beginning of each institution or organization is publishing packages, because these organizations is very influential, high quality of their code, so it will be part of the development of their common class is distributed with JDK.

In naming the package, in order to prevent duplicate names, there is a convention: we all form to write down your own domain name for himself as a beginning to the development of the package, such as Baidu released the package will com.baidu * beginning, w3c organization. the package will be posted. * begin with org.w3c. * beginning, micro Academy package will be released net.weixueyuan ......

Domain name suffix organizations generally org, the company's domain name suffix is ​​generally com, can be considered org. * Packages starting as a non-profit organization released a package, they are generally open-source, free to use in their own products, without consider infringement, and to com. * packages are often published by the beginning of the profitability of the company, there may be copyright issues, should be used with caution.

Reference article

https://www.cnblogs.com/ryanzheng/p/8465701.html
https://blog.csdn.net/fuhanghang/article/details/84102404
https://www.runoob.com/java/java-package.html
https://www.breakyizhan.com/java/4260.html
https://blog.csdn.net/qq_36626914/article/details/80627454

Micro-channel public number

Java technology rivers and lakes

If you want to focus on my real time updated articles and dry sharing, you can focus on my public number of rivers and lakes] [Java technology Java, a technical engineer Ali station, the author Huang oblique, focused Java related technologies: SSM, SpringBoot , MySQL, distributed, middleware, cluster, Linux, network, multi-threaded, occasionally speaking point Docker, ELK, as well as dry goods and technology to share the learning experience, committed to the full Java stack development!

Java engineers required Learning Resources: Some Java engineers common learning resources, the number of public attention, background replies keyword "Java" to get free no routine.

My public number

Personal Public Number: Huang oblique

The author is Master 985, ants gold dress JAVA engineer, specializing in JAVA backend technology stack: SpringBoot, MySQL, distributed, middleware, services, but also understand the point of investment banking, occasionally speaking point algorithms and theoretical basis of computer, keep learning and writing, believe in the power of lifelong learning!

Programmers 3T Technology Learning Resources: Some programmers learning resources spree technology, the number of public attention, background replies keyword "data" can get free no routine.

Guess you like

Origin www.cnblogs.com/AliCoder/p/11595349.html