A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"It's me again. I forgot to explain something. I'll now tell you about variables and memory addressing. We won't delve too deeply, but it’ll be good if you remember at least something."

"I like your approach to lessons: If you understand something — great. If you don't understand anything — that's okay."

"Love can't be forced. That's obvious. Isn't that so on your planet?"

"No. We have a different approach. If you want to, you study; if you don't want to, you study, though unwillingly."

"What a backward approach to learning! That wastes a lot of energy and time and produces poor results."

"We waste it all right. But let's not talk about sad stuff."

"OK. Imagine Excel. Everybody knows Excel. An Excel sheet consists of cells. Each cell has its own unique identifier (A1, A2,…B1, B2). If you know a cell's identifier, you can always write some value into it or get whatever value is stored there. Computer memory is organized in a very similar way."

Memory addressing and variables - 1

"I'm following you so far."

"The program and program data are stored in memory when the program is running. All computer memory is divided into small cells, or bytes. Each cell has a unique identifier, or number, associated with it: 0,1,2,3,… (the numbers start with 0). If you know a cell's number, you can save data in it. Or get data from it. Some cells store the program's code, i.e. the set of commands for the processor. Others store the data used by the program. Each cell's number is called its address."

"The Professor already told me about the processor and commands, but not in detail."

"The processor knows how to execute commands that have been loaded into memory. Almost all processor commands are something like 'take data from some cells, do something with them, send the result to other cells'. We combine hundreds of simple commands to get complex and useful commands."

"But why do I need all this?"

"When a variable is declared in code, a bit of memory that isn't already being used is assigned to it. This is usually a few bytes. Declaring a variable requires that you indicate the type of information the program will store in it: numbers, text or other data. For convenience, a unique name is assigned to each variable."

"Does that mean that a variable is a name, type, part of memory, and a value too?"

"It's all of those things rolled into one. Look at some examples:"

Code Explanation
1
String s;
This line creates a variable named s. We declare its type as String, because it will be used to store text.
We can't declare another variable with the same name in the same function or class.
2
String s2 = "I'm Diego";
This line creates a String variable named s2 and immediately assigns the value "I'm Diego".
3
int a;
Here we create a variable named a. Its data type matches what will be stored in it. The int data type is short for integer.
3
int b = 4;
We create a variable named b. Its data type (int) is for storing integers. The value 4 is immediately assigned to the variable.

"Here are some tasks from Diego. Work on them a little at a time."

1
Task
New Java Syntax,  level 1lesson 3
Locked
It's cool to be a programmer!
Write a program that displays: "I think being a programmer is cool". Example output: I think being a programmer is cool