Saturday, 25 August 2012

Java programming lesson #2 - Variables and constants

Variables are places in the computer's memory where you store the data for a program. Each variable is given a unique name which you refer to it with.

Variable types

There are 3 different types of data that can be stored in variables. The first type is the numeric type which stores numbers. There are 2 types of numeric variables which are integer and real. Integers are whole numbers such as 1,2,3,... Real numbers have a decimal point in them such as 1.89 or 0.12.

Character variables store only 1 letter of the alphabet. You must always put the vlaue that is to be stored in a character variable inside single quotes. A string variable is like a chracter variable but it can store more than 1 character in it. You must use double quotes for a string instead of single quotes like with a character.

Boolean variables can store 1 of 2 values which are True or False.

Here is a table of the types of variables that can be used:

NameTypeValues
byteNumeric - Integer-128 to 127
shortNumeric - Integer-32 768 to 32 767
intNumeric - Integer-2 147 483 648 to 2 147 483 647
longNumeric - Integer-9 223 372 036 854 775 808 to 9 223 372 036 854 775 807
floatNumeric - Real-3.4 * 1038 to 3.4 * 1038
doubleNumeric - Real-1.7 * 10308 to 1.7 * 10308
charCharacterAll unicode characters
StringCharacterAll unicode characters
booleanBooleanTrue or False

Declaring variables

You can declare a variable either inside the class curly brackets or inside the main method's curly brackets. You declare a variable by first saying which type it must be and then saying what its name is. A variable name can only include any letter of the alphabet, numbers and underscores. Variable names can start with a $ but may not start with a number. Spaces are not allowed in variable names. You usually start a variable name with a lower case letter and every first letter of words that make up the name must be upper case. Here is an example of how you declare an integer variable called MyVariable in the main method:

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

If you want to declare more than 1 variable of the same type you must seperate the names with a comma.

public class Variables
{
   public static void main(String[] args)
   {
      int myVariable, myOtherVariable;
   }
}

Using variables

A = is used to store a value in a variable. You put the variable name on the left side and the value to store in the variable on the right side. Remember to use quotes when storing String values. You can also store a value in a variable when it is declared or later in the program.

public class Variables
{
   public static void main(String[] args)
   {
      int myInteger;
      myInteger = 5;
      String myString = "Hello";
   }
}

You can print variables on the screen with System.out.println.

public class Variables
{
   public static void main(String[] args)
   {
      int myInteger = 5;
      String myString = "Hello";
      System.out.println(myInteger);
      System.out.println(myString);
   }
}

You can join things printed with System.out.println with a +.

public class Variables
{
   public static void main(String[] args)
   {
      int myInteger = 5;
      System.out.println("The value of myInteger is " + myInteger);
   }
}

Variables in calculations

The important thing about variables is that they can be used in calculations. When you perform a calculation you must store the result in a variable which can also be a variable that is used in the calculation.. Here is an example of how to add 2 numbers together and store the result in a variable:

public class Variables
{
   public static void main(String[] args)
   {
      int answer;
      answer = 2 + 3;
   }
}

Once you have stored a value in a variable you can use it in a calculation just like a number.

public class Variables
{
   public static void main(String[] args)
   {
      int answer;
      int number = 2;
      answer = number + 3;
   }
}

Here is a table of operators which can be used in calculations:

OperatorOperation
+Addition
-Subtraction
*Multiplication
/Division
%Remainder of division

Constants

Constants are variables with values that can't be changed. The value is assigned to a constant when it is declared. Constants are used to give names to certain values so that their meaning is more obvious. The meaning of the number 3.14 is not obvious but if it was given the name PI then you know immediatly what it means.Use the word final in front of a normal variable declaration to make it a constant.

public class Variables
{
   public static void main(String[] args)
   {
      final double PI = 3.14;
   }
}

0 comments:

Post a Comment