Unformatted text preview: out.print("hello world.");
System.out.println(); // print a blank line
System.out.println("Add new line after this"); General Form: A Java program
// This Java code must be in a file named class-name .java
public class class-name {
public static void main(String args) { statement(s)
}
}
// Example Program stored in the file HelloWorld.java
import java.util.Scanner;
public class HelloWorld {
public static void main(String args) {
Scanner keyboard = new Scanner(System. in);
System.out.print( "Enter your name: ");
String myName = keyboard.next(); // keyboard input
System.out.println( "Hi Rick");
System.out.println( "This is " + myName);
}
} Primitive Numeric Types Type: A set of values with associated operations Java has many types, a few for storing numbers
• Stores integers in int variables
• Store floating-point numbers in double variables A few operations for numeric types
• Assignment Store a new value into a variable
• Arithmetic +, -, * (multiplication), /
• Methods
Math.sqrt(4.0) Math.max(3, -9)
See class Math for others Variables to store numbers To declare and give initial value:
type identifier = initial-value ; Examples
int creditsA = 4;
double gradeA = 3.67;
String name = "Chris";
int hours = 10;
boolean ready = hours >= 8; Assignment We change the values of variables with assignment
operations of this general form variable-name = expression; Examples:
double x;
int j; // Undefined variables
// can not be evaluated j = 1;
x = j + 0.23; Memory before and after The primitive variables x and j are undefined at first
Variable
Name Initial
Value Assigned
Value j ? 1 x ? ? means undefined 1.23 The expression to the right of = must be a value that
the variable can store assignment compatible
x = "oooooh nooooo, you can't do that"; // <-Error
j = x; // <-Error, can't assign a float to an int Assignment
double bill; What is value for bill now? _________
bill = 10.00;
bill = bill...
View
Full Document
- Fall '12
- Mercer
-
Click to edit the document details