Declaration of Java variables

Asked

Viewed 158 times

4

Is there any difference between the two statements:

First:

int a;
int b;

Second:

int a, b;

Which is the best? Are the variables closer in memory or is that just a myth? Is there any significant difference?

  • 3

    I believe that there is no difference, only readability only, the first form will occupy one more line, but is more readable depending on the amount of variables declassified in the code.

  • The way of declaring does not interfere with performance, but the way of initializing can greatly interfere (https://www.javaworld.com/article/2076614/core-java/object-initialization-in-java.html).

3 answers

8


No, zero, this is just a syntax form. Once compiled it’s all the same. In general all these different forms of syntax change nothing. There could be when there is semantic difference, which is not the case, there is that of getting closer in memory, you have no control of it and even if it occurred it would not make a difference because the memory is accessed directly and changes nothing where it is (generally speaking, there are far more complex cases that this can change, but it is something very advanced even, not for simple things like this.

Note that there is no context in the question. It makes more difference by the context used than this in itself. Not that, but it makes a difference whether this is a local variable or a member of an object. Not that it matters so much the variable in this case.

That’s why it’s important to understand every detail of the code. I use that phrase in my lectures:

Enquanto você não souber o que cada caractere do seu código faz, até mesmo o espaço em branco, você ainda não sabe programar

  • 3

    "It is important to understand every detail of the code" - There are those who prefer to spend the time in putting together pieces of code found on the net, and then come to ask how to correct errors arising from copy/Word, instead of using it to learn the language and the basics of programming. When I wanted to do something for Android, before writing a line of code, I "spent" over a month reading the documentation and watching tutorials. This, although I already have programming experience on other platforms.

  • 4

    Yes, it should be like this, understand the concepts, check if you already have all the requirements for this new learning, start studying in a structured way, until you answer questions with other people when necessary, start training with simple things, self-contained, and go forward, until it comes to the point of understanding to work seriously. And then you’ll have almost no questions, yes answers to give ;)

0

In terms of performance it doesn’t change anything, if you’re doing the program alone it doesn’t change anything either. If you are developing software as an employee of a company or as a team use the way in which the company or team decided to standardize, as it facilitates the identation and reading of the code. Try to have general notions of how a compiler works, learn how software compiles will make you a better programmer.

-1

Which is the best?

Neither of the two changes anything, the best choice is the one you feel most comfortable wearing and that people can understand. For we write the code so that others may read later.

Variables get closer in memory or is that just myth?

I believe it’s a myth.

Is there any significant difference?

There is no difference, the two are right and compile quietly!

I hope I helped you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.