Contents
- ./First.java
- ./Float.java
- ./Conversion.java
./First.java 1/3
[top][prev][next]
/**
* This class demonstrates some basic Java syntax
*
* @author Sara Sprenkle
*
* All code in a Java program must belong to a class.
* There is typically one class per file, and the file name is ClassName.java
* Compile this code using
* javac First.java
* Run the compiled bytecode First.class using
* java First
*/
public class First { //File must be saved as First.java
/**
* Called when user runs
* java First
*/
public static void main(String[] args) { //Blocks start/end with {}
System.out.println("This is my first Java program!"); //Lines end with ;
System.out.println("To print a \\, you must use \"\\\\\"");
}
}
./Float.java 2/3
[top][prev][next]
/**
* This class demonstrates how to specify floats vs doubles in Java.
*
* @author Sara Sprenkle
*/
public class Float {
/**
* Called when user runs
* java Float
*/
public static void main(String[] args) {
float f = 3.14f;
}
}
./Conversion.java 3/3
[top][prev][next]
/**
* This class converts from inches to centimeters.
*
* This class demonstrates variable declarations and class constants.
*
* @author Sara Sprenkle
*
*/
public class Conversion {
static final double CM_PER_INCH = 2.540;
/**
* Called when user runs
* java Conversion
*/
public static void main(String[] args) {
int numInches = 1;
double numCM = numInches*CM_PER_INCH;
// need to put + in between string literals and variables
// need to put explicit spaces into string literals
// Note that Java will automatically convert the ints and doubles
// to strings
System.out.println("There are " + numCM + " cm in " + numInches + " inches.");
}
}
Generated by GNU enscript 1.6.4.