Variables, literals and constants

I have a little experience in C++ (Arduino) and am on day one with Java.
Am I right in thinking that in Java, a literal is a variable with a fixed value? ie similar to a const in C++ ?

Hey there and welcome to the community!! :slight_smile:

In Java a constant is actually declared with the final keyword similar to C/C++:

final int a = 5;

A literal however refers to, well, a literal value. For example 0, which represents the value 0:

int a = 0

or a boolean value of true or false

boolean b = true;

Literals are all of the values that are hardcoded into a program.

Thanks … errr …
Do you mean that neither a (irrespective of whether it is a constant) nor b are literals?
Please can you explain what you mean by “the values that are hardcoded into a program”?

Correct!! Both of these are variables (or constants). The literal is the actual value assigned to it, eg 0, true, false.

//     variable
//   /
int a = 5;
//       \
//        literal

My terminology might be a little crude so let me hope I can explain this well. Hardcoded as in you actually type the value out, or the compiler automatically knows what it means. For example, the compiler doesn’t have a clue what the name a means until you tell it, so trying to use it without at least initializing it leads to an error:

//                    What is "b"?
//                  /

A literal however the compiler already has an understanding of. For example 0, you can use it without telling the compiler what it is: