Alessandro Lacava’s Blog

Google
 

September 22, 2006

How to include a .js file (a JavaScript file) within another .js file

Filed under: Computer, JavaScript — alessandrolacava @ 11:43 am

Actually, there is no function you can call to include a JavaScript file within another one. This is the bad news. The good news is that you can use a trick to accomplish this task. Suppose you have two JavaScript files, say file1.js and file2.js. You want to include file1.js within file2.js because you want to use file1's functions. Here is the code you can use:

JavaScript:
  1. //this is file2.js
  2.  
  3. // HERE IS THE TRICK!
  4.  
  5. // IMPORTANT NOTE: For security reason the src keyword is
  6. // banned and gets automatically replaced
  7. // with xsrc by the blog engine. Make sure
  8. // you use src and not xsrc in your code!
  9.  
  10. document.write("<script xsrc='file1.js' type='text/javascript'></script>");
  11.  
  12. // here you can use functions defined in file1.js

As you can see it suffices "to write" in the browser the code you generally use to include a JavaScript file within an HTML file and you're done!


September 13, 2006

Java passes EVERYTHING by value!

Filed under: Computer, Java — alessandrolacava @ 10:51 am

It's a common misconception thinking (or worse teaching!) that, in Java, primitives are passed by value and objects by reference. Actually, everything in Java is passed by value as well as object references.

When a parameter is passed by value, an actual copy of it is really passed so that any change made will have only a local effect. For example:

JAVA:
  1. public static void main(String[] args)
  2.  
  3. {
  4.  
  5. int a = 0;
  6.  
  7. increment(a);
  8.  
  9. System.out.println(a); //it prints 0 so increment didn't work as expected
  10. }
  11.  
  12. public static int increment(int a)
  13.  
  14. {
  15.  
  16. ++a;
  17.  
  18. System.out.println(a); //it prints 1
  19.  
  20. return a;
  21.  
  22. }

As you can see the change made to the parameter passed to increment, that is a, affects only the local copy of it. This proves that Java passes primitives by value.

Well, this is no surprise to anyone since you always knew that.

I'll prove now, that also object references are passed by value, that is only a copy of the reference is passed. Suppose you want to write a swap method to swap two integers. Since you know you can't use primitives you might think to write the method using the wrapper class for int values, that is Integer. Let's try that:

JAVA:
  1. public static void main(String[] args)
  2.  
  3. {
  4.  
  5. Integer a = new Integer(1);
  6.  
  7. Integer b = new Integer(5);
  8.  
  9. swap(a, b);
  10.  
  11. System.out.println(b.intValue()); //it prints 5 so swap didn't work!
  12. }
  13.  
  14. public static void swap(Integer a, Integer b)
  15.  
  16. {
  17.  
  18. Integer temp = a;
  19.  
  20. a = b;
  21.  
  22. b = temp;
  23.  
  24. System.out.println(b.intValue()); //it prints 1!
  25.  
  26. }

This means that, even the object references are passed by value, as I stated at the beginning of this post!

What really happens is that, in case of references, a copy of the reference is passed and not the reference itself. This means that, if the class is not immutable, you can change it's state but you CANNOT change the actual memory address the reference points to. Since Integer is an immutable class you cannot change its state. So if you want to swap two int values, you need to write your own class that wraps an int and provide a method to change the state of this int. In this case you can pass the COPY of the references to a swap method and call the method that lets you change the state of the object. This is left as an exercise though :-)


Next Page »