Question

So I've written a few comprehensive scripts using bash and sh, but only a couple of small java programs. I've read a couple of java books and just feel like it isn't sticking. I find C and C++ easier to grasp for some odd reason and then scripting to just be immensely more simple.

One of the big gaps in my understanding between scripting and programming is understanding how programs are processed. For example, in a script, I can do the following:

#!/bin/sh

var="my variable"

echo "$var"

Obviously this will print my variable. I understand that this basically works as if I typed this into the shell as commands with echo being the command and $var being the argument to echo and the script just processes these commends sequentualy. But if I do this in java:

String var = "my variable"
System.out.println(var);

What is the difference in how the computer is processing the commands or are these even commands? For the first line, is String a command to the jvm and var the argument?

I understand that these are differences in syntax and I understand how that code works, but under the hood, what is the difference between how a shell script processes commands versus the jvm? Or am I looking at this totally wrong?

Was it helpful?

Solution

Every line of source code is a instruction to the interpreter or the compiler.

The result is equivalent but they work totally different. In your java example the first line is a variable declaration. Java requires it, C requires it, but PHP, Javascript, and python don't require them.

It's not a special command, it's just how you tell the program how to store that content. In bash when you do the first assignment, the interpreter allocates some memory automatically to stores its value. In other languages you must specify what you will store beforehand. It's just the way it was made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top