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


"Hi, Amigo."

"Hello, Eleanor Carrey."

"Just call me Ellie. There's no need to be so formal."

"OK, Ellie."

"I believe with my help you'll soon be one of the best. I have a lot of experience training rookies. Stick with me, and everything will be just fine. Well, let's get started."

"There are two major types in Java: String and int. We store strings/text in String, and integers (whole numbers) in int. To declare a new variable, you need to specify its type and name. The name cannot be the same as the names of any other variables and/or functions."

Example 1, code: Description
String s;
A new variable s is declared. It can store text.
int i;
A new variable i, is declared. It can store integers.

"You can assign values to variables when you declare them."

Example 2, code: Description
String s = "Ellie";
Variable s stores the string "Ellie".
int i = 5;
Variable i stores the number 5.

"To assign a new value to a variable, we use the = sign. It is also called the 'assignment operator'. Assignment means to place into a variable a value from another variable or one computed from several variables."

Example 3, code: Description
int a = 5;
Variable a stores the value 5.
int b = 6;
Variable b stores the value 6.
int c = a + b;
Variable c stores the value 11.

"A variable's value can be used to compute a new value that will replace the old one."

Example 4, code: Description
int a = 2;
Now a equals 2
int b = 3;
Now b equals 3
a = a + b;
Now a equals 5
b = b + 1;
Now b equals 4

"You can merge strings with the + sign:"

Example 5, code: Description
String s1 = "Rain";
String s2 = "In";
String s3 = s1 + s2 + "Spain";
Variable s3 stores the string "RainInSpain"

"Sometimes, strings consisting of one or more spaces can come in handy:"

Example 6, code: Description
String s1 = "My favorite movie is";
String s2 = "Route";
int roadNumber = 66;
String text = s1 + " " + s2 + " " + roadNumber;
text stores "My favorite movie is Route 66"

"Let's take a look at how we display text and variables on the screen:"

Introducing ints and Strings - 1
Example 7, code:
1
System.out.println("A man's gotta do what a man's gotta do");
2
String s = "A man's gotta do what a man's gotta do";
System.out.println(s);

"By the way, Diego asked me to give you a couple of exercises:"

2
Task
New Java Syntax, level 2, lesson 4
Locked
Let's change the code
Change the program so that the variable name has the value "Amigo".
2
Task
New Java Syntax, level 2, lesson 4
Locked
A few more corrections
Change the program to display the following text: "Coding in Java". Use variables.
2
Task
New Java Syntax, level 2, lesson 4
Locked
I'm 15 again!
Change the program so that the variable age has the value 15.