Members Login
Username 
 
Password 
    Remember Me  
 

Topic: [U of NE] Lesson 2 - Variables

Post Info
Ben
Administrator
Status: Offline
Posts: 232
Date:
[U of NE] Lesson 2 - Variables
Permalink   
 

Now that you have Eclipse up and running, let's take a look at some programming basics.

The most core fundamentals of programming in any language are variables. Variables are like spaces in a parking garage; they are addresses the computer sets aside to store a value. Like parking spaces, there are different kinds of variables for different data types. If you try to store the wrong type of data in a variable, you'll get an error! Though it's a bit of an over-simplification, let's think of four types of variables for now, and how to use them.

1. String - a string variable is for storing words, letters, sentences, or anything that's not a number. You can put a number here, but it won't be the actual number, it will just be the letter of that number. Confused? Think of it as a piece of paper. If you write 5 on the paper, it doesn't mean you have five of something; it just means you have a paper with 5 written on it. Strings are going to be quite important, so get used to using them.

2. Int - an int is any number without a decimal point. You can add them, subtract them, multiply them, or do a number of other things with them. However, be careful when dividing ints! Because they can't store decimal points, often you'll get a lot of rounding errors. For dividing, it's best to use doubles.

3. Doubles - a number with a decimal point. It can also store larger numbers than ints. If you're not sure what to use, best go with a double.

4. Boolean - can be either true or false. It's also known as a "flag" variable, because it acts like a flag - it's either up or down, telling to computer to do something (or not do something.)

So, now that you get the basics of variables, let's learn how to declare them. To declare a variable means to create the space in the parking garage (your computer program) where you want to store that value. Remember, Java is case sensative, so make sure everything is lower case, unless you're typing "String." The first S in string is always capitalized. Let's try making a simple program. Open the test program we started in the last lesson, and add these lines after the line you typed.

String myString;
myString = "Hey people! This is my string! I'm gonna type in it like nobody's business!";
System.out.println(myString);

Do you see what we did thar? Using the Print command, we just created a variable, put a value in it, and printed out that value. Let's try something with numbers next.

int aNumber;
int anotherNumber;
int sumOfNumbers;
aNumber = 5;
anotherNumber = 13;
sumOfNumbers = (aNumber*anotherNumber);
System.out.println(sumOfNumbers);

Remember, if I had done anything with decimal places, I would have wanted to use a double rather than an int.

Don't worry too much about booleans for now; you'll understand them better once we get to loops in another lesson or two. Now that you know how to use variables and how to print output (remember, use the System.out.println(); command with whatever you want to print in between the parantheses. If it's not a variable, make sure to put it in quotation marks!) you can go ahead and make a few simple project! Go ahead and experiment - that's the best way to learn!

Oh, one more thing. Make sure you put a semi-colon at the end of every line of code; that's just the way Java works! If you don't add one, it'll keep looking for the end of the line and stall or break. Don't worry; you can't damage your computer if you forget one, but your program probably won't work correctly!

__________________
"It needs to be about 20% cooler." --Rainbow Dash

Of course you should listen to me. Have I ever lied to you before?
I mean, in this topic.

Administrator
Status: Offline
Posts: 993
Date:
Permalink   
 
One, thing: I don't have eclipse up and running. I download it, right click, click open, and there are a whole bunch o folders. None the programs say eclipse.exe

__________________
I don't suffer from insanity. I enjoy every minute of it.

"Life is a game, Order. And I'm winning." --Discord, "Order and Chaos".

Ben
Administrator
Status: Offline
Posts: 232
Date:
Permalink   
 
Well, then you shouldn't have said you were ready for the second lesson.

Make sure you've downloaded the correct file. This link will take you right to the correct one:
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-java-helios-SR1-win32.zip

__________________
"It needs to be about 20% cooler." --Rainbow Dash

Of course you should listen to me. Have I ever lied to you before?
I mean, in this topic.

Forum Guru
Status: Offline
Posts: 424
Date:
Permalink   
 
lol i dont understand smart language
i prefer the language of dumb butts
...........like me

__________________

i am a greatful grapefruit!

                                       -bjork

Administrator
Status: Offline
Posts: 993
Date:
Permalink   
 
Still not working. I click open, and all there is is a bunch of files and folders. None of 'em work.

__________________
I don't suffer from insanity. I enjoy every minute of it.

"Life is a game, Order. And I'm winning." --Discord, "Order and Chaos".

Ben
Administrator
Status: Offline
Posts: 232
Date:
Permalink   
 
Make sure you extract the files to a folder first; programs don't run from a zip file, ya know.

__________________
"It needs to be about 20% cooler." --Rainbow Dash

Of course you should listen to me. Have I ever lied to you before?
I mean, in this topic.

j
Super Advanced Member Plus
Status: Offline
Posts: 233
Date:
Permalink   
 
Sorry, i left my brain in Canada last winter.

__________________
The general who is most skilled in defense hides in the deepest recesses of the earth, while the general who is most skilled in offense soars to the highest part of heaven.-Sun Tzu (The Art Of War)
Administrator
Status: Offline
Posts: 993
Date:
Permalink   
 

I got it! number1.gif



__________________
I don't suffer from insanity. I enjoy every minute of it.

"Life is a game, Order. And I'm winning." --Discord, "Order and Chaos".

Full of Win
Status: Offline
Posts: 397
Date:
Permalink   
 
this is a lot of stuff i don't understand but i'm not trying to learn it

__________________

The Fun Has Been Doubled!

Administrator
Status: Offline
Posts: 993
Date:
Permalink   
 

Please do, it's much fun to program games.



__________________
I don't suffer from insanity. I enjoy every minute of it.

"Life is a game, Order. And I'm winning." --Discord, "Order and Chaos".

Ben
Administrator
Status: Offline
Posts: 232
Date:
Permalink   
 

So, do people want another lesson?



__________________
"It needs to be about 20% cooler." --Rainbow Dash

Of course you should listen to me. Have I ever lied to you before?
I mean, in this topic.

Administrator
Status: Offline
Posts: 993
Date:
Permalink   
 

biggrin



__________________
I don't suffer from insanity. I enjoy every minute of it.

"Life is a game, Order. And I'm winning." --Discord, "Order and Chaos".

Forum Guru
Status: Offline
Posts: 485
Date:
Permalink   
 
Hi.

__________________

 

 
Page 1 of 1  sorted by
Quick Reply

Please log in to post quick replies.



Create your own FREE Forum
Report Abuse
Powered by ActiveBoard