C / C++中的#include :对于C语言, #include是程序中的标准文件或用户定义文件,它告诉预处理器将另一个文件的内部内容插入程序的源代码中。
句法:
#include
程序1:
下面是一个C程序,演示#include的用法:
C
// C Program to demonstrate use of #include
#include
// Header file loads all the
// necessary Input output
// file at beginning only
// Driver Code
int main()
{
printf("GeeksforGeeks");
return 0;
}
Java
// Java program to demonstrate use of import
import java.io.*;
// import statement doesn't load
// all the necessary files at
// beginning rather it loads
// only those files which it
// needs at the runtime
class GFG {
public static void main(String[] args)
{
System.out.println("GeeksforGeeks");
}
}
输出:
GeeksforGeeks
用Java导入:在Java, import语句用于加载整个包或包中的某些类。它是在类定义之前和package语句(如果存在)之后编写的。
句法:
import java.util.*;
程式2:
下面是一个Java程序,演示了import语句的用法:
Java
// Java program to demonstrate use of import
import java.io.*;
// import statement doesn't load
// all the necessary files at
// beginning rather it loads
// only those files which it
// needs at the runtime
class GFG {
public static void main(String[] args)
{
System.out.println("GeeksforGeeks");
}
}
输出:
GeeksforGeeks
C / C++中的#include和Java的import都用于加载预定义的头文件或程序包,但下面列出了某些区别:
S No. | #include in C/C++ | import in Java |
1 | It is mandatory to use the #include statement to include standard header files. | Import statement in java is optional |
2 | It loads the file at the beginning only. | No class files will be loaded at the beginning. Whenever a particular class is used then only the corresponding class file will be loaded. |
3 | Unnecessary waste of memory and processor’s time. | No such waste of memory and processor’s time. |
4 | Size of the program increases. | No increase in the size of the program. |
5 | It is also called as static include. | It is also called as dynamic include. |
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。