|
The top software developers are more productive than average software developers not by a factor of 10X or 100X or even 1000X but by 10,000X. Nathan MyhrvoldA Little Taste of Java
Question: If we compile a Java source program in MS Windows
to binary code,
can we ftp the binary code to Linux and execute it?
How about C++?
//Java program
public class Csusb {
public static void main( String [] args ) {
System.out.println( "CSUSB the Beautiful!" );
}
}
|
Platform independent Java source code.. |
|||||
| ↓ | ↓ | ↓ | ||||
|
|
|
...yields... | |||
| ↓ | ↓ | ↓ | ||||
...............
|
Platform independent byte code.. |
|||||
Byte code can be run on any computer that supports a Java Virtual Machine ( JVM ) , which is a special piece of software that knows how to interpret and execute Java byte code.
An applet is a 'mini' Java program that can be embedded in a Hyper Text Markup Language ( HTML ) page.
<html>
<head>
<title>My Home Page</title>
</head>
<body>
<applet code=MyApplet width=80 height=100></applet>
</body>
</html>
|
//Csusb.java
/*
A simple Java Program
*/
public class Csusb {
public static void main( String [] args ) {
System.out.println( "CSUSB The Beautiful!" );
}
}
|
Execute: java Csusb
Examples:
String s = "CSUSB the Beautiful!";
String s1 = "Java the Tasteful";
String s2 = s + s1;
String name = null;
char c = 'A'; //single quote
boolean error = false;
....
if ( error )
...
boolena done = false;
....
if ( done )
...
if ( done == true )
....
while ( !done ) {
....
}
//Csusb.java
import java.applet.Applet;
import java.awt.Graphics;
public class Csusb extends Applet {
public void paint(Graphics g) {
g.drawString("CSUSB the Beautiful!", 50, 25);
}
}
|
create a file called csusb.html
<HTML> <HEAD> <TITLE>A Simple Program</TITLE> </HEAD> <BODY> Here is the output of my program: <APPLET CODE="Csusb.class" WIDTH=250 HEIGHT=30> </APPLET> </BODY> </HTML> |
autoincrement ++
autodecrement --
int i, j, k; i = 1; j = i++; //j ← 1, i ← 2 i = 1; j = ++i; //j ← 2, i ← 2 |
int x; double y; x = 1; y = x; //O.K. x = y; //NO, won't compile; O.K. in C++ x = ( int ) y; //O.K. explicit casting float z = 3.5; //Error! Why? float z = ( float ) 3.5; //O.K. float z = 3.5F //O.K. |
// Print a Fahrenheit to Celsius table
class FahrToCelsius {
public static void main (String args[]) {
int fahr, celsius;
int lower, upper, step;
lower = 0; // lower limit of temperature table
upper = 300; // upper limit of temperature table
step = 20; // step size
for (fahr=lower; fahr <= upper; fahr = fahr + step) {
celsius = 5 * (fahr-32) / 9;
System.out.println(fahr + " " + celsius);
} // for loop ends here
} // main ends here
}
|
Outputs:
0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148 |
//Count to ten
class CountToTen {
public static void main (String args[]) {
int i;
for (i=1; i <=10; i++) {
System.out.println(i);
}
System.out.println("All done!");
}
}
|
Outputs:
1 2 3 4 5 6 7 8 9 10 All done! |
class CountToTwentyByTwos {
public static void main (String args[]) {
int i;
for (i=0; i <=20; i += 2) {
System.out.println(i);
}
System.out.println("All done!");
} //main ends here
}
|
Outputs:
0 2 4 6 8 10 12 14 16 18 20 All done! |
class Hello {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println("Hello, world!");
i++;
}
System.out.println("Goodbye!");
}
}
|
Outputs:
Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Goodbye! |