Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Tim Bray on 'What Sun Should Do'

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Learn how to store data in objects

The journey from Java wanna-be to Java developer continues

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
In the previous Java 101 column, I discussed Java as an interpreted language and explained Java byte code and the Java Virtual Machine. You learned how to set up Java on a system using the Java 2 SDK, downloadable from Sun, and created your first simple Java program. I then covered the basics of object-oriented programming, and how it is centered around types and objects. Finally, I introduced the class construct in Java, and you created a class that looks like this:

public class AlarmClock {
  public void snooze() {{column,
    System.out.println("ZZZZZ");
  }
}


You also created a program (a class with a main method) that tests the AlarmClock class:

public class AlarmClockTest {
  public static void main(String[] args) {
    AlarmClock aClock = new AlarmClock();
    aClock.snooze();
  }
}


If you didn't read the last column, you'll probably want to review it before tackling this one.

Variables and primitive types

Though the snooze button is probably the most commonly used button on an alarm clock, the simple AlarmClock class you've created so far is still missing some important requirements. For instance, you have no way of manipulating how long the alarm clock will stay in snooze mode. However, before you can do that, you must take a more detailed look at how Java controls data.

Developers use variables in Java to hold data, with all variables having a data type and a name. The data type determines the values that a variable can hold. You will see in the examples below how integral types hold whole numbers, floating point types hold real numbers, and string types hold character strings.

Called primitive types, integral and floating point are the simplest data types that Java uses. The following program illustrates the integral type, which can hold both positive and negative whole numbers. This program also illustrates comments, which document your code but don't affect the program in any way.

/*
  * This is also a comment. The compiler ignores everything from
  * the first /* until a "star slash" which ends the comment.
  *
  * Here's the "star slash" that ends the comment.
  */
public class IntegerTest {
  public static void main(String[] args) {
    // Here's the declaration of an int variable called anInteger,
    // which you give an initial value of 100.
    int anInteger = 100;      // Declare and initialize anInteger
    System.out.println(anInteger); // Outputs 100
    // You can also do arithmetic with primitive types, using the
    // standard arithmetic operators.
    anInteger = 100 + 100;
    System.out.println(anInteger); // Outputs 200
  }
}


Java also uses floating point types, which can hold real numbers (numbers that include a decimal place).

Here is an example program:

public class DoubleTest {
  public static void main(String[] args) {
    // Here's the declaration of a double variable called aDouble.
    // You also give aDouble an initial value of 5.76.
    double aDouble = 5.76;     // Declare and initialize aDouble
    System.out.println(aDouble);  // Outputs 5.76
    // You can also do arithmetic with floating point types.
    aDouble = 5.76 + 1.45;
    System.out.println(aDouble);  // Outputs 7.21
  }
}


Try running the programs above. Remember, you have to compile them before you can run them:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources