• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does it mean for a variable to be a temp?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I see a lot of code that just simply does not have initialized variables. What's the purpose of this? I know it all depends on the program, but in a general sense, why do programmers do this?

And why are variables sometimes just set to zero? what's the idea?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They'll be set or changed later.
 
Liutauras Vilda
Marshal
Posts: 4660
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There are few things needs to be known about variables declaration and initialization and assignment.

1. Before variable is used, it needs to be declared. Variable declaration means:
a) defining variable type
b) defining variable name
i.e.


2. Variable initialization needed in order to give variable a starting point, its initial value. When you open your bank account for example, there is nothing in it, 0 (zero), unless you add some money in it. So the same you need to give variable its default value.
This statement is called initialization and declaration (sometimes simply initialization), because you give him type and name and initial value.
Such statement could be on separate lines too and it is the same as above:

What is a variable assignment?

3. Variable assignment is, when you give variable value which is no an initial. From previous statements you know, that ticketNumber is 1, so, when you write somewhere further in your code:
That means assignment, you reassign variable's initial value to something else. Technically I think it is the same, just a wording which could might confuse you.


4. So let's recap.

a) Declaration:

b) Declaration and initialization:
or equally the same
c) Assignment (after variable has been declared and initialized):

Note: if you'd try to give variable an initial value without declaring it, you'd compile error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!