Java 中的“导入”与 C/C++ 中的“#include”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13517618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'import' in Java vs. '#include' in C/C++
提问by Eamon Moloney
Does 'import' in Java behave in the same way as '#include' in C/C++? Specifically, will it include the entire library that it is importing or will it just include the classes and methods that are called in the subsequent code?
Java 中的“import”与 C/C++ 中的“#include”的行为方式相同吗?具体来说,它会包含它正在导入的整个库还是只包含在后续代码中调用的类和方法?
回答by alk
#include
does none of both, neither "importing" libraries, nor classes or modules.
#include
两者都不做,既不“导入”库,也不做类或模块。
The #include
directive just tells the pre-processor to include the contents of another text file (source). That's all.
该#include
指令只是告诉预处理器包含另一个文本文件(源)的内容。就这样。
The result of pre-processing file A #include
ing file B is passed to the compiler as if they were one file, with file B pasted into file A at the position where the #include
directive was placed.
预处理文件 A 和#include
文件 B的结果就像它们是一个文件一样传递给编译器,文件 B 粘贴到文件 A 中#include
放置指令的位置。
To expliclity state this: This all happens priorto any compilation, code generation.
明确地说:这一切都发生在任何编译、代码生成之前。
As a side effect the C/C++ pre-processor could be used independently from the compiler to process anykind of text file input.
作为副作用,C/C++ 预处理器可以独立于编译器使用来处理任何类型的文本文件输入。
One could argue that pre-processor statements like #include
"are not really part of the C/C++ languages", as they are not essentially needed to write any programs in C/C++, as they are never passed to the compiler.
有人可能会争辩说,像#include
“实际上并不是 C/C++ 语言的一部分”这样的预处理器语句,因为它们本质上并不是用 C/C++ 编写任何程序所必需的,因为它们永远不会传递给编译器。
The expression import
is not used in the context of (standard) C/C++ programming, as there is nothing to be imported.
该表达式import
不用于(标准)C/C++ 编程的上下文中,因为没有要导入的内容。
C/C++ modules are put together either on source level priorto compilation or by the linker aftercompilation.
C/C++ 模块在编译前在源代码级别或在编译后由链接器放在一起。
回答by Ajay Sharma
#include<stdio.h>
means copy the contents of stdio.h in this file,
while import java.util.ArrayList
means if you cann't find a class in this file look into above mentioned import location. Java imports do not increase size of your file it just saves few keystrokes.
#include<stdio.h>
表示将 stdio.h 的内容复制到此文件中,同时import java.util.ArrayList
表示如果在此文件中找不到类,请查看上述导入位置。Java 导入不会增加文件的大小,它只会节省很少的击键次数。
回答by Jerome
In Java, when you use import
, you import either :
在 Java 中,当您使用 时import
,您可以导入:
- a single class :
import foo.bar.Baz
- a full package :
import foo.bar.*
- a single static member of a class :
import static foo.bar.Baz.GRUT
- every static member of a class :
import static foo.bar.Baz.*
- 一个班级:
import foo.bar.Baz
- 一个完整的包:
import foo.bar.*
- 类的单个静态成员:
import static foo.bar.Baz.GRUT
- 类的每个静态成员:
import static foo.bar.Baz.*
回答by DejanLekic
C/C++ #include
directive happens at pre-processing phase and in plain English it means "at this place paste the whole header file(or any text file if you like) that is given as parameter". The new C++ standard will have modules (finally), and may or may not be similar to Java's import (depends how it is going to be implemented). More about this C++ proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf
C/C++#include
指令发生在预处理阶段,用简单的英语来说,它的意思是“在这个地方粘贴作为参数给出的整个头文件(或任何文本文件,如果你喜欢)”。新的 C++ 标准将具有模块(最终),并且可能与 Java 的导入相似或不同(取决于它将如何实现)。有关此 C++ 提案的更多信息:http: //www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf
Java 8 will also have improved support for modular programming. See: http://openjdk.java.net/projects/jigsaw/
Java 8 还将改进对模块化编程的支持。见:http: //openjdk.java.net/projects/jigsaw/
回答by fkl
I think one aspect that other answers might have not cleared regarding c is,
我认为关于 c 的其他答案可能尚未明确的一个方面是,
include in c only copies the header file in the preprocessor phase which contains the function prototype, nothing more. The actual function definition is still located at link time, after basic code compilation.
include in c 只复制包含函数原型的预处理器阶段的头文件,仅此而已。在基本代码编译之后,实际的函数定义仍然位于链接时。
A simple verification can be done by including a header file, but not the source file and calling the function from your code. There will be no compilation error and only at link time it would complain not being able to locate the definition.
可以通过包含头文件而不是源文件并从代码中调用函数来完成简单的验证。不会有编译错误,只有在链接时它才会抱怨无法定位定义。
回答by Narendra N
import (in Java) is similar to using (in C++) - As you understand they are not exactly same, but very similar.
import(在 Java 中)类似于 using(在 C++ 中)——正如你所知,它们并不完全相同,但非常相似。
import is to indicate the compiler where it can find the class (or sub package) used in the current class.
import 是指示编译器在哪里可以找到当前类中使用的类(或子包)。
include - Sven explained it better here - https://stackoverflow.com/a/3739563/135553
包括 - 斯文在这里解释得更好 - https://stackoverflow.com/a/3739563/135553
回答by Germann Arlington
Depends on what you actually import. The smallest importable entity is class, the largest is the package.
So if you need a single class and import complete package it will bring in the complete package.
取决于您实际导入的内容。最小的可导入实体是类,最大的是包。
因此,如果您需要单个类并导入完整的包,它将引入完整的包。
回答by Abhishek Luthra
In general terms :
笼统 :
In c language, when compiler encounters #include statement , all the specified header files will be loaded at the time of include statement only irrespective of whether we are using those header files or not. Hence it is called static binding .
在c 语言中,当编译器遇到#include 语句时,所有指定的头文件只会在include 语句时加载,而不管我们是否使用这些头文件。因此它被称为静态绑定。
But in case of java language, when compiler encounters import statement no .class file will be loaded at the time of import statement. In the next lines of code whenever we are using a class at that time only the corresponding .class file will be loaded. This type of loading is called dynamic loading or load on demand or load on fly.
但是在java语言的情况下,当编译器遇到import语句时,import语句时不会加载.class文件。在接下来的代码行中,每当我们使用一个类时,只会加载相应的 .class 文件。这种类型的加载称为动态加载或按需加载或动态加载。
回答by Saikiran
include usually refers to C,C++ lang,where they are of platform dependent languages and need to be compiled,linked and loaded on directly to the machine instruction set of architecture, but where as import refers to java lang,it is of platform independent ,where it can generate byte code and then it can interpreted to binary code format.
include通常是指C、C++ lang,它们是平台相关的语言,需要直接编译、链接和加载到架构的机器指令集上,但是import是指java lang,它是平台无关的,它可以生成字节码,然后可以解释为二进制代码格式。