Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Learn how to store data in objects

The journey from Java wanna-be to Java developer continues

  • Print
  • Feedback
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:

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources