CSERD


  • userhome
  • catalog
  • resources
  • help

Programming in Java


Shodor > CSERD > Resources > Tutorials > Programming in Java

  Intro  •   Lesson 1  •   Lesson 2  •   Lesson 3  •   Lesson 4  •   Lesson 5  •   Final Challenge


Programming in Java - Lesson 2

Variables

OK, so a computer program that does nothing but say hello isn't really all that useful. What we want is a program that is able to do a number of things. Store text and numbers; add, subtract, and multiply; allow the user to give input; give detailed feedback; perform different tasks based on user input; take over the world; etc.

More importantly, we are writing the program to make our life easier. We want a tool that can be applied to different situations, and we need to deal with the fact that those situations will vary. We need our numbers, text, and information to be, well, variable.

You may have used variables in math classes. In math class, a variable could be one of two things, an unknown, or input to an equation. In computer programming, we are using the second interpretation. Trust me, you don't want to use unkown quantities in computer programming. (At some point you will, accidentally, and you will scratch your head wondering why you are getting a null pointer exception. We don't want to go there.)

A variable in computer programming is just a spot to store information, it is like a cubby-hole in memory that we reference by its address.

Data Types

What are the different types of data we can store in computer memory? There are certain basic types of data, like integer numbers, decimal numbers (sometimes called floating point numbers, or floats; or double precision floating point numbers, or doubles), or alphanumber charachters. We can build more complex data types from these simple ones. We can have a lot of alphanumeric characters strung together (and maybe call it a "String"). We could make a 2 dimensional array of decimal numbers, and call it a Matrix.

The four most fundamental data types that you will deal with in Java are int, double, boolean, and char. An int is an integer number. A double is a double precision (64 bit) decimal number. A boolean is a single bit representing either true or false. A char is a single ASCII character ('a', 'b', 'c', '1', '2', '3', etc.).

Example

The following program uses three types of variables: ints, doubles, and booleans.

public class Types {
    static public void main (String [] args) {

        int i=1;
        double x=1.0;
        boolean flag=true;

        System.out.println("i = "+i);
        System.out.println("x = "+x);
        System.out.println("flag = "+flag);
    }
}

Save as Types.java, compile, and run.

You should get the follwing output;

[localhost:~/Intro/Types] djoiner% !java
java Types
i = 1
x = 1.0
flag = true

Let's start performing some operations on these variables.

What are the types of operations we might want to perform? Well, we can add, subtract, multiply, and divide ints and doubles. For booleans, we can use not, and, and or.

Modify the previous program, and add the following lines.

        // basic mathematics
        int j = 2+i;
        double y = 3.0+x-0.5;
        System.out.println("int j = "+j);
        System.out.println("double y = "+y);

        // more advanced mathematics
        System.out.println("(2+3)/2 = "+((2+3)/2));
        System.out.println("(2.0+3.0)/2.0 = "+((2.0+3.0)/2.0));

        // boolean algebra
        boolean flag2 = false;
        System.out.println(""+flag+" and "+flag2+" = "+(flag&flag2));
        System.out.println(""+flag+" or "+flag2+" = "+(flag|flag2));
        System.out.println(""+flag+" and not "+flag2+" = "+(flag&!flag2));

You should get the following output:

[localhost:~/Intro/Types] djoiner% java Types
i = 1
x = 1.0
flag = true
int j = 3
double y = 3.5
(2+3)/2 = 2
(2.0+3.0)/2.0 = 2.5
true and false = false
true or false = true
true and not false = true

Notice that integer algebra, when confronted with a numerical procedure which produces a non-integer number, "chops" the decimal part of the number. Care must be taken when mixing computation with ints and doubles to avoid loss of precision.


©1994-2024 Shodor