Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do multiple languages interact in one project?

I heard some people program in multiple languages in one project. I can't imagine how the languages interact with each other.

I mean there is no Java method like

myProgram.callCfunction(parameters); 

never happens or am I wrong?

like image 866
n00ki3 Avatar asked Mar 11 '09 23:03

n00ki3


People also ask

When two or more coding languages can effectively be combined in a project?

It is possible to combine different languages in one project, you just write two different programmes and let them communicate with each other. And in some cases it even makes sense. Say your project is written in Java or Python for the most part but you have a part that requires a little more computing power.

How do you communicate between two programming languages?

The easiest way to get two programs written in different languages to talk to each other is by messaging.

Can you use more than one programming language at a time?

You can learn two programming languages at once but it is not advised for beginner developers. For anyone who hasn't learned and mastered their first language avoids learning two at the same time. If you have professional experience then learning two languages side-by-side shouldn't be too difficult.

Can Java and C work together?

If you want to call native code from Java program, you should use JNI. This will require preparation in the C side of your code, but it works pretty good. If you are new to programming, again, I would recommend avoiding that. Regarding GUI - you can work with Swing.


2 Answers

Having multiple languages in one project is actually quite common, however the principles behind are not always simple.

In the simple case, different languages are compiled to the same code. For example, C and C++ code typically is compiled into machine assembler or C# and VB.Net is compiled into IL (the language understood by the .NET runtime).

It gets more difficult if the languages/compilers use a differnt type system. There can be many different ways, basic data types such as integer, float and doubles are represented internally, and there is even more ways to represent strings. When passing types around between the different languages it must be sure that both sides interpret the type the same or - if not - the types are correctly mapped. This sort of type mapping is also known as marshalling.

Classic examples of interoperability between different program languages are (mostly from the Windows world):

  • The various languages available for the .NET platfrom. This includes C#, VB.Net, J#, IronRuby, F#, XSLT and many other less popular languages.
  • Native COM components written in C++ or VB can be used with a huge variety of languages: VBScript, VB, all .NET languages, Java
  • Win32 api functions can be called from .NET or VB
  • IPC (inter process communication)
  • Corba, probably the most comprehensive (and most complex) approach
  • Web services and other service-oriented architectures, probably the most modern approach
like image 138
Dirk Vollmar Avatar answered Nov 12 '22 19:11

Dirk Vollmar


Generally, any decently sized web project will use about five languages: HTML, CSS, Javascript, some kind of server-side “getting things done” language (ASP, JSP, CGI scripts with Perl, PHP, etc.), and some variant of SQL for database connectivity.

(This is, of course, hand-waving away the argument about whether or not HTML and CSS count as programming languages – I’m the “they are, but just not Turing-complete languages” camp, but that’s a whole other thread.)

Some examples of how all those work together:

If you’re going the best-practices route, the structure of a web page is in HTML, and the instructions for how to display it are in CSS – which could be in the same file, but don’t have to be. The CSS contains a bunch of classes, which the HTML refers to, and it’s up to the browser to figure out how to click them together.

Taking all that a step further, any javascript scripts on that page can alter any of the HTML/CSS that is present (change contents of HTML entities, swap out one CSS class for another, change the behavior of the CSS, and so on.) It does this via something called the Document Object Model, which is essentially a language and platform-independent API to manipulate HTML pages in an object-like manner (at which point I’ll back away slowly and just provide a link to the relevant wiki article.)

But then, where does all the HTML / CSS / Javascript come from? That’s what the server-side language does. In the simplest form, the serer-side language is a program that returns a giant string holding an HTML page as its output. This, obviously, can get much more complex: HTML forms and query string parameters can be used as input for our server side program, and then you have the whole AJAX thing where the javascript gets to send data directly to the server language as well. You can also get fancy where the server language can customize the HTML, CSS, and Javascript that gets spit out – essentially, you have a program in one language writing a program in another language.

The Server-side language to SQL connection works much the same. There are a lot of ways to make it both more complex and safer, but the simplest way is for your server language to dynamically build a string with a SQL command in it, hand that to the database via some kind of connector, and get back a result set. (This is a case where you really do have a function that boils down to someValue = database.executeThisSQLCommand( SQLString ). )

So to wrap this up, different languages in this case either communicate by actually writing programs in each other, or by handing data around in very simple easy to parse formats that everybody can understand. (Strings, mainly.)

like image 30
Electrons_Ahoy Avatar answered Nov 12 '22 20:11

Electrons_Ahoy