Monday, June 20, 2005

Day 2 - Datatypes and the Hello Billy Program



They're C programmers anyway... So datatypes were quite quick:

int - still the same, but can store -2B to +2B
char - still the same, but uses unicode
float - dont use this, as float w = 3.14; produces an error (has to be float w = 3.14f)
double - use this if you want floating-point variables

boolean - true or false

Although not really a primitive datatype, i introduced them to Strings. For C programmers, the simplicity of Java Strings is pure heaven.

String s, t, u; (no more character arrays!)
s = "bobby"; (wow! easy assignment! no need for strcpy!)
t = "andrews";
u = s + t; (they really dig how easy it is to concatenate strings!)

Had the HelloBilly program today. I call it the hello billy program because the dialog is like this:

Enter name: Billy
Hello Billy!

I mean, if Hello World is the first program, then the first program with I/O should also have a special name right? (Tell all your friends. ;)

I'd rather not use the java.io classes for user input. Pretty complicated.

Behold!


import java.io.*;
public static class HelloBillyNastyVersion {
  public static void main(String args[]) throws IOException {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter name");
    String name = stdin.readLine();
    System.out.println("Hello " + name);
  }
}


vs.


import javax.swing.*;
public static class HelloBillyBetterVersion {
  public static void main(String args[]) {
    String name = JOptionPane.showInputDialog(null, "Enter name");
    System.out.println("Hello " + name);
  }
}


I mean, really now... Exceptions on the 2nd day of classes? Long named Java classes? Scary...

1 Comments:

At 10:43 AM, Blogger Unknown said...

Hmmmm... useful, since I'm trying to learn Java myself.

 

Post a Comment

<< Home