Histroy of Robotic in art and science

Back to Physical and Embedded Computer


 

Created by Sun Microsystems in early 1990s.
Simple language

Portable programs (multiple platforms, Windows, MacOS, Linux, etc.)

 

Object-oriented language

Everything is an object (class).

 

Network friendly

Java virtual machine

Get it from http://www.java.com/

 

Development process

  • Compile (javac turns a xxx.java file into xxx.class file)
  • Execute (java runs the xxx.class file)

 

Simple hello world example using,

System.out.println("Hello World!");

 

GUI example using Swing facility

 

 

 

Anatomy of a simple Java Program

A simple HelloWorld example. The name of the program is

HelloWorld.java

 

/*
   This is a simple Java program. 
*/
class HelloWorld {

// This is the main program.

  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
Note the following:

Comment can be either

/*
Multiple lines comment. 
*/

or

// Single line comment. 

 

Capital and small letters matter.

HelloWorld does not equal to helloWorld

 

Dot syntax to denote object, properties and methods.

e.g. System.out.println

 

Everything is inside a Class.

 

The name of the file HelloWorld.java should match the major class name HelloWorld.

 

It needs a main function to let the program run.

 

Note that there are three words in front of the main function.

public - indicates that the main function can be accessed outside the class HelloWorld. The opposite of public is private.

static - allows the main function to be called before an object of the HelloWorld class is created.

void - tells us that the main function does not return anything after it is called.

 

Using Variables

Now we try another example.

UseVar01.java

 

class UseVar01 {
  public static void main(String[] args) {
    int var01, var02;
    float var03, var04;
    double var05, var06;
    var01 = 5;
    var02 = var01 / 3;
    var03 = 5;
    var04 = var03 / 3;
    var05 = 5;
    var06 = var05 / 3;
    System.out.println(var02);
    System.out.println(var04);
    System.out.println(var06);
  }
}

Note the 3 numbers printed.

1
1.6666666
1.6666666666666667

 

int, float and double are 3 different numeric data types

 

Try another example with the String data type.

UseVar02.java

 

class UseVar02 {
public static void main(String[] args) {
String s1, s2;
s1 = new String("Bryan");
s2 = "Chung";
System.out.println(s1);
System.out.println(s2);
System.out.println(s1 + " " + s2);
}
}

Note the result,

Bryan
Chung
Bryan Chung 

 

char data type is a single character

 

char ch = 'Q';

 

Condition

 

if - else statement

int a, b;
a = 3;
b = 4;
if (a==b) { 
  System.out.println("equal");
} else {
  System.out.println("not equal");
}

 

switch statement

char myChar = 'b';
switch (myChar) {
  case 'a':
    System.out.println("This is a");
    break;
  case 'b':
    System.out.println("This is b");
    break;
  case 'c':
    System.out.println("This is c");
    break;
  default:
    System.out.println("None of the above");
}
 
            

Loop - repetition

 

for statement

int i;
for (i=0;i<10;i++) {
  System.out.println("i = " + i);
}

 

for (int i=0;i<10;i++) {
  System.out.println("i = " + i);
}

 

while statement

char myChar;
myChar = 'a';
while (myChar<='z') {
  System.out.println(myChar);
  myChar++;
}

 

do - while statement

int i = 10;
do {
  System.out.println("i = " + i);
  i--;
} while (i>0);

 

break statement

for (int i=0;i<10;i++) {
  if (i==5) {
    break;
  }
}

 

continue statement

for (int i=0;i<10;i++) {
  if (i==3) {
    continue;
  } else {
    System.out.println(i);
  }
}

 

Object

Create an Object from a class.

 

Remember each Java program has a class definition.

 

A simple one first

public class SimpleOne {
  public static void main(String[] args) {
    SimpleOne so = new SimpleOne();
  }
}

 

so will be an object of the class SimpleOne.

 

Properties can be added to the class.

public class SimpleOne {
  String name;
  int id;

  public static void main(String[] args) {
    SimpleOne so = new SimpleOne();
    so.name = "bryan";
    so.id = 167;
    System.out.println(so.name);
  }
}

 

Methods can also be added to the class.

public class SimpleOne {
  String name;
  int id;
  
  public void ChangeID(int i) {
    id = i;
  }

  public static void main(String[] args) {
    SimpleOne so = new SimpleOne();
    so.name = "bryan";
    so.id = 167;
    System.out.println(so.id);
    so.ChangeID(168);
    System.out.println(so.id);
  }
}

 

Majority of the Java programs have a number of objects either pre-defined or user-defined.