Computersight > Programming > Java

The Basics of Java Programming 2

It doesn't take long to learn the basics, and once you do, you can write your very own java programs.

Last Time...

Last time we covered the very basic syntax of a Java program and how to print text into a command-line box. Expanding on that this week, you will learn how to use variables which are a VERY important component to a program.

Variables

A variable is like a box. You are able to put different objects into a box, just like you can put different data into a variable. You can also change what is in the box at whatever time you want. This is just like a variable. It's data can be changed, even while the program is running.

So to be clear, a variable can hold a piece of data, and only one. If you wanted to store multiple bits of data, you would use an array. We will get to arrays later on, for now, know that variables store data and can be changed.

Now, onto the use of a variable. To declare a variable and set its value, you use the following syntax:

public class Variables {
     public static void main(String args[]){
          int thisIsMyVariable = 0;
     }
}

This is how you declare an integer variable, which if you remember from the previous tutorial, is a number. To create a string variable, you replace "int" with "String". But be careful when using strings. You must remember to place the whole string in quotation marks or you will return an error.

Now that we have our variable, lets try displaying it in your command-line box:

public class Variables {
     public static void main(String args[]){
          int thisIsMyVariable = 0;
          System.out.println(thisIsMyVariable);
     }
}

You will notice that you do not place quotation marks around the variable name when printing it, if you did, you would get a result of: "thisIsMyVariable" instead of the value the variable contains. Once you've tried this, change the value of the variable to any number you like. Don't make it too large though, try to keep it under 65,000.

As an extra excercise, try changing the datatype of the variable to a String and edit its value.

Calculations

Once you've had a bit of a play with that, we can move onto calculations. We can use variables and mathematical signs to add, subtract, multiply and divide numbers. We can also use other symbols to assign values to variables and check for certain things. Here is a list of all the symbols and what they can be used for:

+    Used for adding two values together
-     Used for subtracting two values
*    Used for multiplying two values
/     Used for dividing two values
=    Used for assigning a value
==  Used for checking if a value is equal to another value
>    Used for checking if a value is greater than another value
<    Used for checking if a value is less than another value
>=  Used for checking if a value is greater than or equal to another value
<=  Used for checking if a value is less than or equal to another value
++  Used for adding 1 to a value
--    Used for subtracting 1 from a value

In this next example, I will demonstrate how these can be used in a program. Read through the code and try to follow what is happening:

public class Variables {
     public static void main(String args[]) {
          int var1 = 0;
          int var2 = 5;
          int var3 = 10;
         
          var1 = (var2 + var3) / var2 * 2;
          System.out.println(var1);
          var1++;
          System.out.println(var1);
          var2++;
          var3--;
          var1 = var2 * var3;
         
          System.out.println(var1);
     }
}

I have used some random numbers and used a combination of symbols to do various calculations and print me the result as it goes. Have a go yourself and try changing around the numbers and calculations.

Next time...

Now that we've covered variables and performing calculations, we will move onto "if and then" statements, loops and using variables in a more complex and useful way. I hope you enjoyed learning more about Java. Check out the next tutorial for the next lesson.

4
Liked It
I Like It!
Related Articles
The Basics of Java Programming [1]  |  The Java Platform
More Articles by Jay A
The Basics of Java Programming [1]
Latest Articles in Java
How to Make an Analog Clock for Your Website  |  Collection Framework: List Interface
Comments (5)
#1 by Devin, Aug 22, 2008
Good guide. thanks for posting
#2 by Dylan, Aug 25, 2008
good java tutorial
#3 by Sid, Sep 8, 2008
nice
#4 by , Sep 19, 2008
sweet tut
#5 by , Sep 22, 2008
awesome man
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.