Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PHP includes and Java Import Declarations

Tags:

java

import

php

In PHP, we have:

<?php include 'external_file.php'; ?>

Whereas in Java, you have imports:

import javax.servlet.http.HttpServlet;

It is to my understanding that PHP includes simply dump the contents of the external file into the file that contains the include statement.

My gut feeling is that Java handles these includes/imports differently than PHP. What are the key differences?

like image 525
Jim Johnson Avatar asked Dec 03 '25 15:12

Jim Johnson


2 Answers

PHP's include is pretty much the exact same thing as having literally cut/pasted the raw contents of the included file at the point where the include() directive is.

Java's compiled, so there's no source code to "include" - the JVM is simply loading object/class definitions and making them available for use. It's much like the #include directives in C. you're not loading literal source code, just function definitions/prototypes/fingerprints for later use.

like image 161
Marc B Avatar answered Dec 06 '25 05:12

Marc B


In php it simply dumps the contents of the file in the current file. In Java, an imported class is used:

  1. For compiling the source to byte code using the imported classes.
  2. At runtime when the JVM sees that your program references the imported class, it loads it and uses it(for method invocations and member accesses if it is the case)
like image 35
Razvan Avatar answered Dec 06 '25 03:12

Razvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!