Memory and I/O
In
Java
Output
The simplest way to output to the screen is to use System.out.print(
) and println( );
Whatever is in the parentheses will be printed on the
screen
It is called the argument of the method call print( )
If you are printing a string, it must be enclosed by
double quotes.
Escape sequences
Escape sequences are used to format strings (i.e.
inside double quotes)
The backslash (\) is
the escape character
It indicates that the next character in the string has
special meaning
\n means start a new line
\t means move the cursor to the next tab stop
\\ means print the backslash
\ means print the double quote
Formatted output
System.out.printf(arguemnts) allows the programmer more control over how
output looks
Sysytem.out.printf(%s\n%s\n, Hello,
World);
%s\n%s\n is the formatting string
Hello is
substituted for the first &s, World for the second
Its major advantages come with floating point numbers,
not strings and integers
These two
statements output exactly the same:
Sysytem.out.printf(%s\n%s\n,
Hello, World);
Sysytem.out.print(Hello\nWorld\n);
Input
The Scanner class in
the new version of Java makes input a lot easier than it used to be.
How do you use the Scanner class?
1. import
java.util.Scanner before the class
declaration
2. Declare a Scanner object inside the class declaration
Scanner varName = new Scanner
(System.in);
3. Call one of the methods of the Scanner class to input
int num = varName.nextInt( ) returns an int
String str = varName.next( ) returns a String
The value returned is assigned to a variable by the
assignment operator
Program on page 47 of your text
import java.util.Scanner;
public class Addition
{ public static void main( String args[
] )
{
Scanner input = new Scanner( System.in );
int number1, number2, sum;
System.out.print( "Enter first
integer: " );
number1 = input.nextInt( );
System.out.print( "Enter
second integer: " );
number2 = input.nextInt( );
sum = number1 + number2;
System.out.printf( "Sum is
" + sum );
//System.out.printf( "Sum is
%d\n, sum ); this is the same as the line above
}
}
What happens in main memory
After your program is compiled, it must be loaded into
RAM to be executed
It is loaded into a section of RAM called static RAM
(or sometimes the stack)
The executable statements are loaded in one part, and
room to store any variables declared are stored directly below
The variable names then correspond to locations (or
addresses) in RAM
Variables
Every variable has:
A name
This
is the variable name give it by the programmer
A type
So
far, the only type we have looked at are integers
The
keyword for integers is int
A size
On
most systems, an integer takes four bytes
A value
This
can either be assigned by the programmer, or by the user of the program
If
no value is assigned by the programmer, or the user, it will contain the junk
that was there from the last use
Loading a program into RAM
On the whiteboard, we will load the program Addition
and its data slots into RAM
Java Arithmetic
Arithmetic operators in Java are much like in algebra
a +
b is addition; a
- b is subtraction;
a * b is multiplication; and a/b is
division
Integer division yields an integer;
so 5/2 = 2; The fractional part is discarded
With integers, you often want to know the remainder
after division;
this is called the mod function, indicated a%b
10%4 = 2;
5%2 = 1; 15%5
= 0
Operator precedence
What is the difference between
5+3*2 and (5+3)*2
It is understood that multiplication takes precedence
over addition
But parentheses
can override the precedence
In a programming language the precedence between
operators is well defined
But sometimes
easily overlooked
My advice is to use parentheses if you are in doubt if
you need them
It
is never wrong to have them, and often wrong to leave them out.