Basic Input and Output

J1.java – Java Programming Quick Start

This code includes Java Building Blocks, Primitive Data Types, basic input and output. Remember, the class name should be the file name plus .java extension. Like this code, the title of the class is J1, so the name of the file also must be J1 plus the extension of .java, that’s why you should name it “J1.java”. Java is Case-Sensitive.

You can compile this program without changing anything

.
// include the Scanner class (from java.util package)
import java.util.Scanner;

public class J1 {
public static void main (String args[ ] ) {
// declare variables of type double, float, int, and byte
double d; float f; int i; byte b; String s;
// declare and instantiate a Scanner object
Scanner input = new Scanner(System.in);

// read values using input (Scanner object)
System.out.println(“Enter a byte, int, float, and double”);
b = input.nextByte();
i = input.nextInt();
f = input.nextFloat();
d = input.nextDouble();
s = input.nextLine();

// output values using println( )
System.out.print(“byte = ” + b + ” int = ” + i);
System.out.println(” float = ” + f + ” double = ” + d + “ string = “ + s);

// output values using printf( ). (for version 1.5 and higher)
// the syntax for format specifier is similar to C programming
System.out.printf(“%d %d %.2f %.3f %s\n”, b, i, f, d, s);
}
}

// EOF: J1.java

no comments yet.



Leave a comment

Names and email addresses are required (email addresses aren't displayed), url's are optional.

Comments may contain the following xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <p> <br>