You are on page 1of 7

Quest map Lessons

Different ways to create variables


Java Syntax
Level 2, Lesson 5

AVAILABLE

"Hi, buddy. I made a copy of your contract for you, just in case. Rishi, that cheapskate, is blissfully ignorant. You should see the figures in
my contract. Ha!"

"Good job, Diego. I think I'll learn a lot from you."

"Sure thing, Amigo. There are too many stupid people in the world who want to get rich without actually doing something. But there are
even more idiots who are ready to work for free."

"OK, let's get back to our lesson. Now I'm going to teach you several ways to create variables:"

1 String s1 = new String(); Create two identical empty strings.


2 String s2 = "";

1 int a; Create an variable;

1 int a = 5; Create an , variable named a and set its value equal to

1 int a = 5, b = 6; Create an , variable named and set its value equal to Create an ,
variable named and set its value equal to

1 int a = 5, b = a + 1; Create an variable named and set its value equal to Create an
variable named and set its value equal to

1 Date date = new Date(); Create a Date object. It is initialized to the current date and time.

1 boolean isTrue = true; Initialize a variable to

1 boolean isLess = (5 > 6); Assign to the variable. variables only accept the values true
and false.

1 02 December 2020, 13:15


"Cool, Diego! You always make everything so clear."

"LOL! Thanks, Amigo."

"By the way, I have a couple more exercises for you. How are they going so far?"

"They weren't too hard, and some were pretty funny."

×3

Task Java Syntax, level 2, lesson 5

CLOSED

The required number

Imagine that for some reason you need a specific number, and until you get it, nothing good will happen (or something bad will happen). Have you got a
picture of that in your mind? Do you have a feel for your role? Good, now let's complete an interesting task. We have some code. You need to comment
out as many lines as possible to make the program display the number 19.

Open

×1

Task Java Syntax, level 2, lesson 5

CLOSED

Crazy eights

The fact that cats were able to take over the Internet with ease shows that we are far from knowing everything about these cute balls of furs. The only
obvious thing is that people without them are worse off than people with them. Let's make the world a better place: create 10 Cat variables and 8 more
Cat objects. By the way, does anyone need a kitten?

Open

×3

2 02 December 2020, 13:15


Task Java Syntax, level 2, lesson 5

CLOSED

Pets need people

"You become responsible forever for what you've tamed," said Antoine de Saint-Exupery through the mouth of the Little Prince's fox friend. Let's clean up
our program and not leave any animal without a caregiver. We'll create a cat, a dog, and a fish. And a woman. And then we'll assign her as the owner of
the fish, dog, and cat. We think she'll like this!

Open

Previous Next

+131

Comments(104) + 14 Popular New Old

Daniel Himmelberg

Mariam

HINT:
You can call it "object" AND you can call it "variable" as well.

This variable is "initialized immediately upon creation":

Reply +3

Gigi Suchan

precedence:
1. * and /
2. + and -

Reply

Matt

It's a good thing I use JDK and Eclipse. I knew I was typing the right code in, but after 8 attempts I was getting
confused. Then I used Eclipse's built in suggestions and it hit me like a ton of bricks! Duh! I made the changes
and it worked! I just completed a course in Java through an online accredited university. It was 300 hours of hell
in 7 weeks. No guidance from an instructor. I passed, but OOP was still a mystery. Codegym jumped right into
OOP before loops etc. And, I am still cloudy on visibility. Eclipse helps with suggestions, but instruction is what
is needed. I am so glad Codegym is tackling this first!

Reply +1
Ibrahim

3 02 December 2020, 13:15


can you tell me how you used JDK and Eclipse with the Codegyme tasks?

Reply

Matt

I copy and paste the task code straight into an Eclipse that is already open. Every task has the same class
name: Solution. This allows me to keep the file name the same: Solution. Eclipse has built-in help in the
form of suggestions. Some have to be done manually while others can be selected and the IDE will do the
rest. Be sure to use [ctrl] [x] to undo changes if they don't work out. I also save all my work in notepad as a
record of my work and for reference. Eclipse saved my GPA, but in the end, the university I was attending
failed to help me understand OOP in any measurable way. Understanding is more important than guessing.
That is why I sought out Codegym.

Reply

AMOON

What's the difference between objects and variables?

Reply +4
Kọ́láwọlé Fasasi

Variable is a named storage of some data type(like int, float etc). And Objects are variables of data types
that are user defined. So we say that an object is an instance of a class. And class is nothing but a user
defined data type. (Quora)

Variables are pre-defined by Java but when you create and initialize an object e.g.

Cat pussBoot = new Cat();

you therefore define a new data type named 'Cat' and given it a variable name 'pussBoot'. Since variable
pussBoot's data type was defined by you (the user) and not by Java (such as int, double etc.), it is called an
object.

I hope you understand my explanation.

Reply +4
AMOON

Thank you! Your explanation is very clear!

Reply

Tyler Evans

Brilliant thank you.

Reply

Maryem Vickers

*reads Diego saying, "By the way, does anyone need a kitten?"* I certainly do! XD

Reply +4
Maryem Vickers

Unfair, I can't even add spaces.

Reply +1

hidden #10602322

cat.owner = woman; is we adding reference to an object am I correct please tell me. If I am wrong then correct
me then tell me what is actually is (︺︹︺)

Reply

Seb

You're correct.

Reply +2
Dinesh

As asked I have created objects of Cat Dog Fish and Woman

4 02 December 2020, 13:15


Thing that is confusing me or say I am not able to understand.

In the main method, how to set the Woman object as the owner of each animal.

Woman woman = new Woman(); // This is the object of Woman class


public Woman owner // What is this in each of the Cat Dog and Fish class

Is this right?
woman.owner=cat;
woman.owner=dog;
woman.owner=fish;

Reply

Drazen Jankovic

I would disagree...
cat.owner = woman;
dog.owner = woman;
fish.owner = woman;
In your example, it would be concluded that woman is owned by a pets, not the other way around.

Reply

Jack

just to be clear...
what do i put in the parentheses in the
IDK idk = new IDK ()

Reply +1
Brad Hawk

For now, nothing. In future tasks, I'm sure we'll be passing data to the methods within the parentheses.
For example if the Woman method had an Age variable within it, we could assign the Woman an age while
creating the Woman objec...
Woman barb = new Woman(25);
This would create the Woman object and assign the Age variable the value of 25

Reply +3
Gallad

If the constructor was defined to take in age variable! The constructor would take exact same number of the
variables and in the order it was assigned to it!

Reply

Michael Brimage

This one was quite difficult for me to wrap my head around.


pet.owner = owner

can someone explain why it's this way? i understand that this was touched on briefly but it's not sticking. for me.

Reply +2
Julia Drugova

I love to think about that as of English grammar: the pet's owner IS= [who]
I think it's because we assign the new pet owner, so what we assign it to on the left eg pet and than we
specify the value (who) we assigned on the right.
I hope it helps and not more confusing, that's my second day in CodeGym myself :)

Reply +4
MLA

They named the method and variable the same in a lot of their examples, which is why you might be getting
confused.

A better idea might have been if they went with:

You're assigning emily to the owner variable in pet.

5 02 December 2020, 13:15


Reply +8
moseka

We can do this int a, and assign it to an integer i.e 5;


a = 5;
In all of the dog, fish and cat classes we have a Woman variable, that is
Woman owner, etc.
so we can assign that variable owner to a Woman object(woman) because it compatible just as int and 5 is
compatible so does (owner and woman).

Reply +1

Gellert Varga

I quote the Example-table above


(from this Level 2 Lecture 5):

" Date date = new Date(); // Explanation: Create a Date object. It is initialized to the current date and time."

I don't understand it exactly.


I know that to initialize a variable means to give a starter value to the variable.
It's OK, we gave a value to this date variable. This value is the reference to the newly created Date object.
But this new Date-object stores "null"-values yet, doesn't it?
- I think it will be initialized to the current date and time only if i write the following code:
Date date = new Date(27, 01, 2020);
Was i wrong?...

Reply

Seb

You can try it out in your IDE or in the integrated Web-IDE of one of the tasks.
Simply type in the following code in the main method - and run it:
Date date = new Date();
System.out.println(date);
You'll then see that date has been initialized to the current date and time. :-)

Reply +1
Seb

Yes, you're right in both cases. Date is built-in - and in your example a kitty object is created, which does
not have its own name and age yet.
You'll learn a lot more about the initialization and construction of objects in level 5.
All the best and keep it up. ;-) :-)

Reply +1

Daniel Malloy

to be honest this lesson was actually pretty fun, I don't know if you agree .

Reply +2

Load more comments

LEARN COMMUNITY

Java Course Users

Help with Tasks Articles

Subscriptions Forum

Game Projects Chat

Success Stories

Activity

ABOUT VIDEO

6 02 December 2020, 13:15


About us Top 3 features of CodeGym

Contacts "Course" section

Reviews About the "Tasks" section

Press Room "Games" section

CodeGym for EDU

FAQ

Support

FOLLOW US

INTERFACE LANGUAGE

English

© 2020 CodeGym Programmers Are Made, Not Born

7 02 December 2020, 13:15

You might also like