#include in C/C++ :在 C 语言的情况下, #include是程序中的一个标准或用户定义的文件,它告诉预处理器将另一个文件的内部内容插入到程序的源代码中。
句法:
#include
方案一:
下面是一个 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的import :在Java, import语句用于加载整个包或包中的某些类。它写在类定义之前和包语句之后(如果存在)。
句法:
import java.util.*;
方案二:
下面是一个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++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。