-5

I had been learning python and i know Java. if i were to write a simple program, can i write both programming language in one document or compiler? I got into this problem because if i cannot merge both the programming language , what is the meaning there to learn them. Another reason for this is that; I found some coding in java is simpler than python and some coding is python is simpler than java.

  • I am pretty sure you can call the python interpreter from java, you'll need to find a way how the programs will communicate. – πάντα ῥεῖ May 25 '18 at 16:00
  • See this question, there are some good answers about why different stuff is done in different languages. It also provide some examples of how the languages are being put together. As for your specific question, while it is possible to mix Java with Python, it is rarely useful for small projects. Most of the time you should pick one and stick with it. Which one, depends on the project. – Frax May 25 '18 at 16:36
  • 1
    Possible duplicate of Using of different language in same project – Frax May 25 '18 at 16:48
  • 3
    If you, like you say, 'know' (and understand) Java, you shouldn't need to ask this question .. – JoeriShoeby May 25 '18 at 17:49
  • The main reason to learn more than one language is you might change jobs and it's nice if your carreer didn't end as soon as some languages popularity did. – candied_orange May 25 '18 at 20:29
3

... what is the meaning there to learn them?

This seems to be a very common beginner question when confronted with multiple languages. Part of the answer is actually in your question, some things are easier to say in one language than another. However in general one doesn't combine languages in this way. Part of software engineering is choosing the right tool, in this case language, for the job; language choice depends on the project or goal.

In the case of a scripting language, ex. python, the interpreter is written in another language i.e. C for CPython, or Java for Jython, C# for IronPython etc. One could 'combine' these languages by integrating and/or implementing an interpreter for the scripting language. This would allow one's program to run the scripting language at run-time.

In general the cost of combining languages is greater than the benefit of doing so; but there are IPC (inter process communications), RPC (remote procedure call), Interface definition languages, like thrift, protocol buffers, MS COM, etc. that facilitate multi-language systems and/or programs.

There is also the older method with compiled languages, if they can create compatible object code, and agree on ABI (application binary interface), calling conventions, etc. they can be linked together. Windows API was originally Pascal, and to write in C/C++ one had tell the compiler to use the Pascal calling convention (ex. the WINAPI macro) in order to link a windows program. C and Assembly (ASM) are/were routinely used together.

1

It's a little complex, but the Graal project allows for exactly this case. https://www.graalvm.org/

1

Some languages are made to work well together so you can do what you describe here easily. Unfortunately that is rare these days, and in most cases you will end up with two independent programs running next to each other talking back and forth.

For Python and Java you are in luck. The Jython project is a Python 2 engine running on the JVM so your Python program can access Java classes easily.

http://www.jython.org/

1

There are complicated answers as to how to create an application that manually directs other compilers.

However, reading between the lines here, it seems you are a beginner (no offense intended) and I think your question is much more basic; boiling down to why languages are segregated in their own world to being with.


By definition.

Take a step back, and ask yourself what a (programming) language is.

In essense, it's a human readable data format that expresses the logical steps we want the computer to take. However, we must note here that languages are a compromise between man and machine, they are not perfect.

On one hand, we have the machine. Inherently logical, fast thinker, but completely unaware of the context of an instruction. If you ask it to do something, it will do it, no questions asked.
For a machine, a perfect language would effectively be machine code. This is the clearest language possible. It's a step-by-step guide. Can't get simpler than that.

However, on the other side of the compromise table, we have the human. Humans need to create the step-by-step guide. But humans are not as logical as machines, and often rely on context before understanding an instruction. Because of this, we have a tendency to omit pedantic details "when it is clear from the context", but if a machine does not observe context, then it might not understand what the human means.

As a simple example:

  1. What country is the best location to visit?
    • France
  2. Who is the president of the United States?
    • Donald Trump
  3. Has he ever been there?

Both "he" and "there" are very vague when you look at the third question by itself. A human would observe the context of the earlier questions, and notice that the conversation had already established a person and a location. But a machine tends to handle every question separately, and it would not be able to understand who you're talking about.
Note that some ambiguity is even more complex:

  1. What happened yesterday?
    • Tommy broke Bobby's nose.
  2. Was he arrested?

Even if you're aware of the context of the earlier question, "he" could still be referring to either Tommy or Bobby.
To a human, it's obvious that you're asking if Tommy was arrested, because he's the aggressor. But a machine is generally going to regard both Tommy and Bobby as equally viable options. It's even possible that "Bobby" is favored by the machine because it's the last name that was mentioned.

We are of course making progress in developing context-aware alogorithms; which I'm omitting for the sake of example here.

Machine code is simply not (easily) readable for humans. Here is a basic example. It's not impossible to see what happens, but it takes you time and effort to figure out what's going on and what's going to happen before you execute the application.

And thus we invented an intermediary step: programming languages. These languages try to keep both sides happy:

  • To please the machines, the languages are deterministic, unambiguous and never rely on implicit context.
  • To please the humans, the languages offer the use of readable names (which don't matter for the runtime but does matter to the context-reliant developer) and a syntax which makes sense, while remaining unambiguous.

Different languages focus on different things

Consider the procedure of waking someone up:

  • In a hotel, the employee gently knocks on the door and patiently waits for the guest to open the door. They generally will not enter unless the door is opened by the guest.
  • Living with your parents, your parent will knock on the door but will eventually walk in to wake you up.
  • In the army, they enter the room and yell at you to get up.

These are very different approaches to the same problem of waking someone up. They are different, but one is not necessarily always superior to the other. Each approach works in its own context:

  • The hotel is trying to make their guests feel valued and avoids inconveniencing them.
  • Your parents are teaching you a good sleeping schedule.
  • The army is expecting you to listen to orders no matter how inconvenient they are to you.

The same is true of programming languages. They are different approaches to the same problem.

  • Functional languages focus on defining calculation logic. Stored data is secondary to the operations that are executed on that data.
  • Object oriented languages focus on defining data state. The logic that changes the data state is secondary to the data structure itself.

Note: I'm oversimplifying for the sake of example. There is more of a grey area here than my explanation shows, but I'm trying to clearly contrast the two.


Okay, so we've established why we need a programming language, and why different languages exist. But we still haven't address why you can't mix these languages at will.

Context is key.

Sometimes, a certain expression can mean different things in different languages.

  • In English, "moron" is an insulting way to call you stupid. In Welsh, it means "carrot".
  • In UK English, "fag" means "cigarette". In US English, it's an insulting way to call you a (male) homosexual.

This is why you can't mix your languages. If a guide had the instruction "light a fag" or "boil a moron", the instructions would be interpreted completely differently based on the language/region you're in.

This would violate the main trait of machines: they speak in a language that is context free and unambiguous.

To extend the real world analogy: If Americans stay in America, and the British stay in the UK, then there is no confusion. Each explicitly defined location has their own meaning for the word.
But if you look at an international holiday resort, you can't rely on regional information (US/UK) to know what was meant when someone said "fag".

This is why each language has its own compiler, and its own binaries. It ensures that every used word (= programming syntax) is strictly defined by the language in which it is used.


Some remarks to finish up:

  • If we were to mix languages, most cases wouldn't end up as an unambiguous conflict. But some of them might, and that's not good. You need to ensure that everything is clear at all times.
  • There are particularly created pieces of code that work in multiple languages. This is (in part) because languages share syntactical similarities (e.g. an = will generally always be a value assignment).
  • The core of the argument isn't that mixing languages is impossible, but that it's prone to ambiguity. There are no real benefits to mixing languages, so it's better to no open the door to that possible ambiguity.
  • Arguably, you can have some files which mix two languages, for example an MVC Razor view can contain both C# and Javascript. However, these languages are not interpreted/parsed/executed at the same time. The Javascript is treated by the Razor view as pure text, not actual code.

There is much more to say on this topic, but I think I've covered your question.

0

If you are just beginning programming, I think you should try hard to make it work in one language. While it is technically possible, for some languages, to call each others functions (like C and Python using the CPython API) it has an overhead of writing some glue code to glue together the pieces of code in the two languages. For your problems, it won't be worth the effort, and in your case, it will be detrimental to your programming abilities.

If Java (or Python) is missing some programming language feature, you can try to emulate it by implementing your own version in that language. If you gave some examples for the tasks you struggled with, it'd be easier to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.