CodeGym /Courses /New Java Syntax /Introducing ints and Strings

Introducing ints and Strings

New Java Syntax
Level 2 , Lesson 4
Available

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.
Comments (219)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Tangent Level 2, Firozabad, India
4 May 2025
guys one word is wrong in this where they indicate that integers are whole no. which is totally wrong, because intergers are also -ve, but whole no. are those integers which are non -ve
Jimmy Level 29, Lausanne, Switzerland
7 November 2024
stay hard, keep going
One Eyed Level 3, CodeGym University in India, India
1 September 2024
the only reason why i picked this code gym was because it has this coding practice. and that isnt free. so i guess it sucks
Anonymous #11498770 Level 2, Nigeria
5 April 2024
i'm beginning to hate code gym
FREEDOM Level 2, United States
8 May 2024
Why?
Anonymous #11425342 Level 7, United States of America, United States
20 December 2023
Codegym is helping me a lot thanks CodeGym
Anonymous #11413956 Level 3, Mesa, United States
24 October 2023
I just got to memorize System.out.print(); Nevermind I got it!
Lishon Level 3, Nottingham , United Kingdom
5 July 2023
Really getting the hang of this. Still making silly mistakes. but less of them. "Don't over complicate things". Do exactly as it asks and you'll be find.
rags 3 Level 5, Wichita, United States Expert
8 June 2023
i went with the month cause why not i waste more on toilet papper will see ifffffff ittttsss worth it
Ankita Gaikwad Level 2, CodeGym University in India, India
30 April 2023
I was starting to like this site a lot but...subscription made me wanna leave this site...sadly I cannot afford this subscription. It was nice getting to know about this site
Anonymous #11225728 Level 2, Saudi Arabia
24 December 2022
I was glad I found a site where I could learn, sadly saying goodbye