Java by Example - simple methods and basic data types

back 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 next

Here you can see how to use a simple method with one parameter. The parameter is used to change the y-coodinate of the text output. Everything that is done more than once, should be put inside a method to make it reusable. Remember: elegance always pays off!

//Sourcecode

import java.awt.*;
import java.applet.*;

//Introduces methods

public class Project2 extends Applet
{
    //We need a global graphics object if we use functions which use graphics:
    public Graphics g;

    //This is our method. It's only parameter is the y-coordinate of the text string.
    public void Hallo(int y)
    {
        g.setColor(Color.black);
        g.drawString("Hallo Java!",120,y);
    }

    public void paint (Graphics gr)
    {
        g=gr;

        //We call our method several times with different parameters
        Hallo(50);
        Hallo(100);
        Hallo(150);
        Hallo(200);
        Hallo(250);
    }
}

The most common basic variable types in Java are:

Name What is it Range Standard value Example
boolean logical true or false false true
int 32 bit signed number -231 .. +231-1 0 123789
long 64 bit signed number -263 .. +263-1 0 123456789
float 32 bit floating point number ca. 7 places accuracy 0.0 3.14
double 64 bit floating point number ca. 15 places accuracy 0.0 0.00032424
String text - "" "This is a text"

To declare a variable, just write the type (int, float, String, Font etc.) followed by a blank and the variable name and a semicolon. The variable name can be almost any name you can imagine (except for the the reserved words of the Java language, no number at first place and no blanks inside the name), but it should be short and descriptive, of course. In the following examples you will quickly see what I mean. Note that Java is a case sensitive language, you have to be careful about upper/lowercase letters!

You can define variables almost anywhere you like, but you will usually declare them either on top of the class, or (if they only have to be visible inside a method) inside the method, right after the method head. You can declare several variables at the same time: int a, b, c;. You can also initialize a variable at the same time it is declared: int a=10;, String s="Hi!";and you can use a mixture: int a, b=3, c=10;. If you don't initialize a variable when you declare it, Java assigns a standard value (see above table), but don't count on it! In most cases it is neccessary to initialize a variable before using it.

In this applet you can see how to use variables of type String (for text strings) and type int (for integer numbers, without fractional part). To use numbers for text output, you can simply add the number to the string with a + sign. If you have only a number variable to put out with drawString, you can put empty "" before your number, in order to convert them to a string.

//Sourcecode

import java.awt.*;
import java.applet.*;

//Introduces variable output

public class Project3 extends Applet
{
    //We need a global graphics object if we use functions using graphics:
    public Graphics g;

    //This is our method. It's only parameter is the y-coordinate of the text string.
    public void Hallo(int y)
    {
        //str is a variable of type String
        String str="Hallo Java!";
        //our x-coordinate is of type integer
        int x=50;

        g.setColor(Color.black);
        g.drawString(str+" The y-coodinate is: "+y, x, y);
    }

    public void paint (Graphics gr)
    {
        g=gr;

        //We call our method several times with different parameters
        Hallo(50);
        Hallo(100);
        Hallo(150);
        Hallo(200);
        Hallo(250);
    }
}

back 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 next

0 - setup - getting your tools ready
1 - basic graphics functions
2 - simple methods and basic data types
3 - IF, ELSE and SWITCH: basic control structures
4 - introducing the operators
5 - methods with and without a return value
6 - using methods and basic mouse functions
7 - fonts, random numbers and timers
8 - flicker free graphics, GIF and JPEG display
9 - animation with GIF pictures, sprite animation
10 - loops, advanced color functions
11 - random colors and arrays
12 - digital clocks, HTML page parameters
13 - introducing classes and objects
14 - using the Vector class
15 - using mouseMove and mouseDrag
16 - keyboard commands and playing sound
17 - detecting collisions and intersections
18 - a Bouncing Balls applet
19 - fun with letters and words
20 - rotating lines and polygons
21 - sorting and shuffling


© 2000 by Johannes Wallroth
www.programming.de

watson@programming.de