Dynamic linking in C/C++ (dll) vs JAVA (JAR)

匆匆过客 提交于 2019-12-12 04:11:07

问题


I am new to java programming.

Basically when we work in c/c++ programming we create dll files using .h and .c files,where .h file contains declarations and .c file contains definitions of those classes, functions. when we want to use those functionalities of created .dll file in other project, we include .h in preprocessor declaration to tell compiler about declarations of variables,functions and we provide respective dll file path during compilation so that at linker stage it communicates with dll.

Here is my question how do they manage this in java programming because it doesn't contain any header files. it has only .java files where these files are combined and created JAR file.when i want to use this jar file in another project we use "package" or "import" keyword but when it says import total file will be imported with logic and how do linker manage at compilation step??


回答1:


how do they manage this in java programming because it doesn't contain any header files.

It manages this by placing all the information it needs for compiling against the class and at runtime (and possibly debugging) in the .class file so there is no need for additional information.

Often the source and javadoc are placed in JARs as well (sometimes the same JAR)

when i want to use this jar file in another project we use "package" or "import" keyword

You don't have to. This is just a short hand. You can use full package.ClassName and there is no need for an import. Note: this doesn't import any code or data, just allow you to use a shorter name for the class.

e.g. there is no difference between

java.util.Date date = new java.util.Date();

and

import java.util.Date;

Date date = new Date(); // don't need to specify the full package name.

when it says import total file will be imported with logic

There is no way, nor no need to do this. There is nothing like #include for example, and inlining only occurs at runtime, not compile time (except for constants known at compile time)

how do linker manage at compilation step

The linking and compiling to native code is performed at runtime. All the javac compiler does is check the validity of your code and generate byte code for the JVM to read.




回答2:


  1. Modern languages, Java (& C#) do not make a distinction between declaration and definition, so the concept of .H file is gone in these languages.

  2. In many aspects (new languages) the dualism compile-time vs runtime is lost (mainly because they have strong reflection). Java Classes or JARs (or C# assemblies) have information required to compile (alike declarations). Java environment don't require special 'files for compilation'. The same JAR is 'compile file' and 'runtime binary dll'

Typical C thinking with .H, .C, .LIB files goes to niche (IMHO - I'm old C programmer and I feel good with new languages)



来源:https://stackoverflow.com/questions/36646687/dynamic-linking-in-c-c-dll-vs-java-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!