Blog

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

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:

 
//this is file2.js
document.write("<script src='file1.js' type='text/javascript'></script>");

// 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!

Read more...

Java passes EVERYTHING by value!

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:

   
public static void main(String[] args) {
  int a = 0;
  increment(a);
  System.out.println(a); //it prints 0 so increment didn't work as expected
}

public static int increment(int a) {
  ++a;
  System.out.println(a); //it prints 1
  return a;
}

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.

Read more...

What happens if you write something like a = a++ in Java?

I often happened to see discussions about this topic. Basically, here is the question. If you have such a code:

   
int a = 0;
a = a++;
System.out.println(a);

What does it print?

More than 50% of the programmers will answer 1, some of the remaining will say “I don’t know” and the others will say 0. Well “the others” are right!

Provided that such a code MUST NEVER BE WRITTEN, let’s try to understand, for academic purposes, why it prints 0.

Read more...

Using dynamic SQL statements from PL/SQL

Sometimes you need to execute dynamic SQL statements. Starting from Oracle8i you can accomplish this task using the EXECUTE IMMEDIATE statement. Here are three examples of how you can take advantage of this great statement.

    
sql_select := 'SELECT * FROM your_table WHERE field1 = :1';
EXECUTE IMMEDIATE sql_select
INTO your_cursor
USING your_parameter_for_field1;

In this first example I showed how you can use EXECUTE IMMEDIATE to execute the query and put the result into a cursor. The USING your_parameter_for_field1 part replaces the :1 bind variable with the value contained in the your_parameter_for_field1 parameter.

Read more...