This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between "Initialization" and "Assignment" ?  RSS feed

 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Quick question, what is the difference between initialization of a variable and assignment of a variable. I would say "the first assignment to a variable is initialization", is that correct? views?

Oh BTW this question struck to me after reading this statement "A local variable must be explicitly given a value, by either initialization or assignment, before it is used. "
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment of a variable (or its name) means giving that variable to a value as its name.

myField = ...

You can assign several variables to the same value and (if not final) you can assign the same variable to different values at different times.
Read the assignment operator as "becomes equals to" or "receives the value of".

I think most people take initialisation to mean assigning a value to a variable or field in the same statement it is declared in.

Foo myField = new Foo();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!