IT 114: Introduction to Java
Class 20
Topics
Review
Homework 7
Homework 7 is not due this Sunday.
Instead it is due the Sunday after the Mid-term Exam.
This will give you time to study for the exam.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
Mid-term
The mid-term exam will be given on Monday, March 23rd.
It will consist of questions like those on the quizzes along with questions
asking you to write short segments of Java code.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
The other 40% will come from four questions that ask you to write a short segment of code.
Today's class will be a review session.
You will only be responsible for the material in the Class Notes for today's class on the exam.
The Mid-term is a closed book exam.
Due Date for Homework Assignments 2 - 6
The due date for homework assignments 2 - 6
is Friday, March 20th at 11:59 PM.
It is also the due date for Class Exercises 2 - 17
That is the Friday of the Spring Break.
Any of these assignments submitted after that date
will receive a score of 0.
Today's Class
There will be no Class Quiz or Class Exercise for today's class.
Review
Objects
- Java is an object oriented language
- But what are objects
- An object
is a section of a computer's RAM
- An object can hold many values
- Along with code that can work with these values
- There is no limit to the number of values an object can contain
- Objects only exist when the program that created them is running
Classes
- Objects have to be defines before you can create them
- The definition are a type of software called a
class
- Classes are software written in Java
- Objects are the contents of RAM created from a class when a Java program is run
- There is only one class for each type of object
- A single class can create many objects
Statements
- A Java program consists of a number of
statements
- A statement is a line or lines of code that represent a complete action
- There are two kinds of statement
- A simple statement can be written on a single line and contains no other statements
- A compound statement contains other statements within it
- All statements must end with a semicolon, ;
- If you don't you will get an error when you compile it
Printing a Line in Java
Data Types
- The basic job of any program is to create and manipulate data
- And data comes in many forms
- Each of these forms must be represented in RAM in the most efficient way possible
- To do this, each computer language has different
data types
- Each of these data types specifies how a value is to be represented in RAM
- Some languages, like Python and Perl, are very easy going with respect to data types
- But Java is not one of them
- Java is a
type-safe language
- Another term for this type of language is
strongly typed
- In a type-safe language, you have to assign a data type to each value
- The data type of a value determines what you can do with it
The Java Data Types
- There are 8 data types in Java
- But we will only use 4 of them
int | integer |
double | decimal |
bool | boolean |
char | character |
Variables
- A variable is a place in RAM that holds one value
- And has a name
- Variables are much simpler than
objects
- Objects can store many values, not just one
- And they can contain code
- Objects do not have names
- They have locations in RAM
Declaring Variables
Assignment Statements
- You give a variable a value with an
assignment statement
- It has the following format
VARIABLE_NAME = VALUE
- Here are some examples
number = 5;
interest = .065;
ch = 'x';
finished = false;
- When we give a variable its first value we are said to
initialize
the variable
- Declaring a variable does not give it a value
- If you try to use a variable before you initialize it you will get an error
- Java lets you combine declaring and initializing a variable in
single statement with the following format
DATA_TYPE VARIABLE_NAME = VALUE ;
- Here are some examples
int number = 5;
double interest = .065;
char ch = 'x';
bool finished = false;;
Literals
- In the statements above we wrote the values of the variables directly inside
the the source code file
- These values are examples of
literals
- Programmers sometimes refer to such values as "hard coded"
= Does Not Mean Equals
- = is an example of an
operator
- Operators are special characters that perform an action using one or more values
- = is the
assignment operator
- It takes the value on the right hand sign and gives it to the variable on its left
- In arithmetic means equals
- But that is not what is really going on with = in Java
- Because the value of a variable can change
- When you read a line of code like this
number = 5
You should not say "number equals 5"
- You should say "number gets 5"
Java Is Case Sensitive
Printing Variables
Operators
- The characters + and -
are operators
- Operators are special characters that perform an action
- Usually the operator creates a new value
- Operators work on one or more values
- These values are call
operands
- There are many different kinds of operators in Java
- But we will start out with the operators used in arithmetic
Arithmetic Operators
- Java has five arithmetic operators
Operator |
Meaning |
Example |
Result |
+ |
addition |
2 + 2 |
4 |
– |
subtraction |
53 – 18 |
35 |
* |
multiplication |
3 * 8 |
24 |
/ |
division |
4 / 2 |
2.4 |
% |
remainder or modulo |
19 % 5 |
4 |
The Mod Operator
- Division with integers returns two values
- The quotient
- The remainder
- Let's look at an example
17 / 5
- In integer division, the quotient is always an integer
- In the expression above, the quotient is 3
- But there is also a remainder which is also an integer
- In this case, the remainder is 2
- In Java % is the
remainder operator
- This operator is something called the
modulus operator
Two Groups of Data Types
- There are two groups of data types
- Primitive data types can only hold one value
- Classes define objects
which can hold many values
Strings
- The
char
data type can only hold one character
- But
String
can hold as many as you want
- Strings are a sequence of characters, one right after the other
- Strings can include spaces and tabs
- Whenever a program contains a string written out inside the code
string literal
- In Java, a string literal must be enclosed with double quotation marks
- You must use double quotes, not single quotes
- A string literal must be on a single line
- It cannot continue onto the next line
- We will only be using strings in this course
Concatenation
- You can make a new string by joining two existing strings
- This is called concatenation
- We join two strings together using the + operator
Concatenating Strings and Numbers
- You can use concatenation to join a string and a number
- Java knows the data type of each variable and literal
- If the value in front of + is a number
- It knows that + means addition
- If the value is a string, it knows + means
concatenation
- Java can't concatenate a string with a number
- So it turns the number into a string
Expressions
- There are many different ways that we can get a value inside our code
- The value can be a literal
- The value can also be held inside a
variable
- Or we can calculate the value using
operators
- Each of these is an
expression
- An expression is anything in Java that can be turned into a value
- At the moment you only know three types of expressions in Java
- Literals
- Variables
- Calculations using operators
- There is a fourth expression type that we will meet in a future class
Evaluating an Expression
- The Java compiler processes the code statement by statement
- Each expression in a statement must be turned into a value
- This has to be done before the compiler can produce the bytecodes for the statement
- Turning an expression into a value is called
evaluation
- Consider the following calculation
(4 + x) / (4 - y)
- The operations inside the two parentheses must be evaluated first
- Then we can divide the two values
- So we have three separate evaluations of operators
- Evaluation of
(4 + x)
- Evaluation of
(4 - y)
- Division of the results
- Using this technique you can build up very complicated expressions
- This poses no problems to the compiler
- It just finds the lowest level expressions and evaluates them
- Then it moves up to the next level
- And performs the operations specified there
Precedence
- Consider the following expression
3 + 2 * 4
- This expression has two operators
- Which operation should be done first?
- If we do addition first we get
3 + 2 = 5
5 * 4 = 20
- But if we do multiplication first we get
2 * 4 = 8
3 + 8 = 11
- Java uses rules of
precedence
in situations like this
- The rules of precedence tell the compiler which operator to apply first
- Operators with higher precedence are used before operators of lower precedence
- For arithmetic operators there are two orders of precedence
- Operators that deal with multiplication and division
have the higher order of precedence
- Operators that deal with addition and subtraction
have a lower order of precedence
- Parentheses always overrule the order of precedence
- If you are ever in doubt about how the compiler will evaluate an expression ...
- use parentheses
Escape Sequences
- There are a few unusual characters that require special treatment
- Because they cannot be written inside a string in the normal way
- There is a character called
newline
that makes the output go down to the next line
- This is the character you get when you hit Enter on a PC
- Or Return on a Mac
- But this won't work inside a
string literal
- If you want to use these or other troublesome characters
you have to use an
escape sequence
- An escape sequence is a set of two characters
- The first of which is a backslash, \
- Here is a list
Sequence | Represents |
\t | tab character |
\n | new line character |
\" | double quote |
\\ | backslash |
Control Structures
- When the interpreter run a script it goes through the code statement by statement
- The order in which it executes the statements is call the
flow of control
- Or the
path of execution
- Control structures allow the interpreter to take different paths through the code
- There are two types of control structures in Java
- Loops
- Branches or Conditionals
Loops
- There are two kinds of loops in Java
- Loops controlled by counting
- Loops controlled by some condition
- Loops controlled by counting are sometimes called
definite loops
- Loops controlled by a condition are sometimes called
indefinite loops
- All loops are composed of two parts
- A loop header
- A loop body
- The loop header
is a single line at the top of the loop
- It controls how many times the loop runs
- The loop body
are the statements that are executed with each pass through the loop
- Each pass through the loop body is an
iteration
Code Blocks
- The loop body is an example of a
code block
- Code blocks are a group of statements that are only executed under
certain conditions
- In Java code blocks are contained between
left curly brace, {
- And a right curly brace, }
- Every { must be followed by a matching
}
- Or you will get an error
- You should indent the statements inside a code block
- The compiler won't care
- But it will make your code more readable
- And readable code is easier to fix when you get an error
The ++ and -- Operators
- ++ adds 1 the value of the variable it is placed
next too
- -- subtracts 1 from its variable
- Adding 1 to the value of a variable is called
incrementing
- Subtracting 1 from the value of a variable is called
decrementing
Order of Precedence for ++ and --
- Here is how the ++ and -- fit into the precedence hierarchy
Description | Operators |
unary operators |
––
–
|
multiplicative operators |
/
%
|
additive operators |
– |
assignment operators |
+=
*=
%=
|
The for
loop
Initializing the Loop Variable
- The first statement in the loop header
initializes
a variable that controls the loop
- It is called the
loop variable
- This variable must be an integer
- The initialization gives the loop variable it's first value
- Which is often 0 or 1
- This statement is only executed once
- And that happens before entering the loop
The Loop Test
- The second statement in the loop header determines how many
times the loop runs
- It is a
boolean expression
which test the loop variable
- As long as this expression is true the loop will run
- A
for
loop stops when the test becomes false
- Usually the expression is true as long as the loop variable
is less than some value
The Loop Update Statement
- The update statement changes the value of the control variable
- This happens after the statements in the loop body have been run
- But before the loop test is run
- The most common update is to add 1 to the loop variable
- Usually this is done using the ++
operator
The for
Loop in Action
- Here is the flow chart for a
for
loop
- First we give the control variable its starting value
- This happens only once
- Then the loop test is run
- If the loop returns true all the statement in the loop body are run
- After the last statement in the loop body, the update statement is run
- Then we run the loop test again
- If the result is true, we run the statements in the loop body again
- If not, we jump out of the loop
- And run the first statement after the loop
for
Loop Patterns
Keywords
Mixing Data Types
- What happens when you mix values of two different
data types in a calculation?
- For example, how would the compiler handle the following
4 * 3.3
- Java has a simple rule for handling situations like this
- Convert the result to a data type that gives the most information
- In the example above an
int
multiplies a double
- If the result were converted into an
int
the value would be 13
4 * 3.3 -> 13
- But if the result is converted to
double
we get
4 * 3.3 -> 13.2
- The first example is less precise than the second
- Java always uses the data type that will not lose precision
Integer Division
- Integer division works differently than decimal division
5 / 2 -> 2
5.0 / 2.0 -> 2.5
- If you want the result of integer division to be a decimal,
multiply the result by 1.0
public class IntDivision {
public static void main(String[ ] args) {
int n1 = 5;
int n2 = 2;
System.out.println("5/2 -> " + n1/n2);
System.out.println("1.0 * 5/2 -> " + 1.0 * n1/n2);
}
}
$ java IntDivision
5/2 -> 2
1.0 * 5/2 -> 2.5
Defining Methods
- You define a method with a special kind of Java statement
called a method declaration
- A method declaration has two parts
- The method body is a code block containing the Java statements that are executed each
time the method is run
- The method header specifies
- What Java programs can use the method
- What kind of value it creates
- The method name
- What values it needs when it is run
- The method header has the following format
MODIFIER [static
] RETURN_DATA_TYPE METHOD_NAME(PARAMETER_LIST)
- The MODIFIER determines which other Java programs can use the method
- In this class we will only use
public
as the modifier
static
is enclosed in square brackets, [ ], in the format above
- That means the
keyword
static
is optional
- For now will use
static
in the header for the methods we define
- The method name comes next
- Followed the the PARAMETER_LIST inside parentheses
- The parameter list has the following format
DATA_TYPE PARAMETER_NAME[, DATA_TYPE PARAMETER_NAME ...]
- The [, DATA_TYPE PARAMETER_NAME ...] mean you can have multiple parameters
- Parameters are special variables inside the method code
Running a Method
Arguments and Parameters
Returning Values
- When a method creates a value and sends it back to the the
method call, we say it is
returning a value
- This is done with a
return
statement
which has the following format
return EXPRESSION
- When the
java
interpreter comes to a return statement
it does the following
- Causes the running of the method to stop
- Sends the value of EXPRESSION
back to the method call
- So a method stops running when it gets to a
return
statement
- The method header has to specify the kind of value each method returns
- It does this using the
return type
that appears in the method header right before the method name
- Even if the method does not return a value, it still needs a
return type
- The return type for a method that returns nothing is
void
Method Calls Can Be Expressions
- Methods that have a
return
statement
provide a value to the method call
- We call anything that can be turned into a value an
expression
- So a call to a method that returns a value is also
an expression
- So an expression can be
- A literal
- A variable
- A calculation with operators
- A call to a method that returns a value
The main Method
- The real work in Java programs is done by methods
- A well written program breaks a big problem down into a number of methods
- The main method then calls the methods
to do the work required
- If the method names are well chosen, simply reading the method calls
in the main method gives a good idea of what the program does
- The main method in Java is special
- All other methods have to be called to run
- Not the main method
- It is automatically called when you run the Java file
- You cannot run a Java file if it does not have a main method
- The method drives the work of the program
Converting Strings to Integers
- The args parameter in the main
lets a Java program use command line arguments
- But command line arguments are strings
- If () wanted to convert the String variable input
into an integer I would write
Integer.parseInt(input);
Conditionals or Branches
- Conditional control structures are like switches on a railroad track
- Which path the train takes depends on how the switch is set
- When the
java
interpreter comes across a conditional
statement it has to make a decision
- It has to decide what statements to execute next
- Usually it makes this decision by evaluating an expression whose only possible values are true and false
- There are four conditionals in Java
- But for the moment we will only consider three
- The
if
statement
- The
if
/else
statement
- The
if
/else if
statement
The boolean
Data Type
- A variable of type
boolean
can only have the values true or false
- The name comes from the British mathematician George Boole who invented this field of mathematics
- A boolean expression
is an expression whose only possible values are true and false
- There are two boolean literals
Relational Operators
if
Statements
if
/else
Statements
Nested if
Statements
if
statements are
compound statements
- That means they can have statements inside them
- An
if
statement is a statement
- So you can have an
if
statement inside an if
statement
- Just like you can have a
for
statement inside a for
statement
- This called a nested if statement
if
/ else if
Statements
Relational Operator Precedence
- Here is a more complete listing of operator precedence showing where the
relational operators fit n=in
Description | Operators |
Unary operators | ++ -- |
Multiplicative operators | * / % |
Additive operators | + - |
Relational operators | < > <= >= |
Equality operators | == != |
Casting
- Java is a strongly typed language
- Scripting languages like Python are flexible about data types
- But Java is not
- Python recognizes different data types
- But it has methods that will turn one data type into another
- Java uses a different approach
- You can force a variable of one data type to behave like another by
using a cast
- It uses the following format
(DATA_TYPE) EXPRESSION
- So if the variable num was a
double
but I needed an int
I would write
(int) num
Logical Operators
- The logical operators are
- The
&&
(and) operator returns true if both its
operands are true
- It also returns false if both its operands are false
- The
||
(or) operator returns true if either of its operands are true
- It only returns false if both of its operands are false
- The
!
(not) operator takes only one operand and reverses its value
- So true becomes false
- And false becomes true
Truth Tables
- To show how the logical operators work we use truth tables
- Truth tables have multiple columns
- The first one or two columns are values of boolean variables
- The last column shows the boolean result of running the operator on the values
- Here is the truth table for
&&
(and)
p | q | p && q |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
- The truth table for
||
(or)
p | q | p || q |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
- And the truth table for
!
(not)
p | ! p |
true | false |
false | true |
Precedence of Logical Operators
- When we write an expression like
a + b > c and c * d < a
there are three different kind of operators
- Arithmetic - +, *
- Relational - >, <
- Logical - and
- Operator precedence
determines which operators are used before other operators
- The logical operators are and, or and not
- They have lower precedence than arithmetic operators
- They also have lower precedence than the relational operators
- The order of precedence is
! |
Logical NOT |
* / % |
Multiplication, division and remainder |
+ - |
Addition and subtraction |
> < >= <= |
Relational Operators |
== != |
Equality Operators |
&&
|
Logical AND |
|| |
Logical OR |
The While Loop
The do
/while
Loop
Infinite Loops
- Conditional loops keep going until their boolean expression becomes false
- That means something has to happen in the loop body to make this happen
- If this does not happen the loop will go on forever
- This is called an
infinite loop
- When this happens on hold down the Control key while pressing C key
Augmented Assignment Operators
- One of the most common operations in programming is increasing the value of a variable by 1
- This is called incrementing
- Sometimes we want to decrease the value of a variable by 1
- This is called decrementing
- Java provides special assignment operators that combine shortcuts to
change the value of a variable with a simple arithmetic operation
- These operators are called
augmented assignment operators
- They are
Expression | Read As |
x += 2 | add 2 to x |
x -= 2 | subtract 2 from x |
x *= 2 | mulitply x by 2 |
x /= 2 | divide x by 2 |
x %= 2 | take the remainder when x is divided by 2 |
Updated Order of Precedence
- All assignment operators have the lowest precedence of any
operator we have studied
Description | Operators |
Unary operators | ! ++ -- |
Multiplicative operators | * / % |
Additive operators | + - |
Relational operators | < > <= >= |
Equality operators | == != |
Logical AND | && |
Logical OR | || |
Assignment operators |
= += -= *= |
Storing More Than One Value
- A variable
can only hold one value
- But objects can hold
as many values as you wish
- For situations like Java has a special kind of object called an
array
- The values in an array are stored one after another
- You can get each value in the array by using its position
in the sequence of values
Getting Values from an Array
- Every value inside an array has its own unique position in the array
- We can get that value using the [ ] operator
- You place this operator right next to the end of the array variable
- And put a number inside it to specify which value you want
- The values in an array are called
elements
- The number put inside the square brackets is called an
index
- Unfortunately, the number used to get the first element of an
array is 0
- This is called
zero based indexing
- For this reason, the index of the last element of an array is
one less than the length of the array
- As you learned earlier, the agrs
parameter
contains all the strings on the command line
- agrs is an array of strings
- So we can use the [] operator
to get each command line argument
The Length of an Array
- We can use a
for
loop to get each value in an array
but we have to know the length of the array to do it
- One of the pieces of information in an array object is its length
- You can get this value using the following format
ARRAY_VARIABLE.length
- To get the length of args we would use
args.length
Creating an Array Object
Initializing Arrays
- If you create a variable of a
primitive data type
you must give it a value before you use it
- When you do this we say you have
initialized
the variable
- Array elements are automatically initialized when they are created with
default values
- This is feature of Java is called
auto-initialization
- For an array of each data type there is a different default value
Type | Value |
int | 0 |
double | 0.0 |
char | '\0' |
boolean | false |
- You can also initialize an array with an array literal
- It has the format
{VALUE, VALUE, ...}
Changing Array Elements
What Is an Array Variable?
- Like all objects, arrays do not have a name
- They have a position in memory
- The statement that creates an array returns a memory address
- We need to store this position in a variable so we can work with the
array
- The variable does not hold the values in the array
- It holds the memory address where the array exists in RAM
- The picture in memory looks like this
Objects
- Objects
are sections of RAM that can contain more than one value
- They can also contain methods
that act upon there values
- These values are called
attributes or
fields or
properties
- Objects do not have a name, just a position in RAM
- When you create an object its position must be stored in a variable
- Otherwise you will not be able to use the object
- The values and methods an object contains are defined by
a class
- Classes serve as the template or blueprints used to create an object
- Java is an object-oriented language
- A program in Java consists of one or more files
- Each of these files contain at least one class
Strings
- Strings are objects because they contain many values
- The format for
declaring and
initializing an array is
String VARIABLE_NAME = STRING_LITERAL;
- A
is a sequence of letters written out inside a Java program
- String literals must be enclosed in double quotes, "
String greeting = "Hello";
- Two strings can be joined to form a third string using
concatenation
- This is done using the concatenation
operator,
+
System.out.println(greeting + "John");
We Have Been Defining Unusual Classes
- All the Java programs you have seen so far are unusual
- Because they cannot be used to create an object of their class
- We will talk more about this in a future class
- All other Java classes can be used to create an object of their class
Two Kinds of Methods
- A method is a collection of statements that perform a specific task
- All methods are defined inside a class
- In Java you can define two kinds of methods
- Methods that work with the values inside an object
- Methods that do not work with values inside an object
- Methods of the first type can only be used on an object that you create from the class
- These are the most common type of Java methods
- The second type are called
static methods
- When we created programs to turn scores into grades we called a method
to convert string values into integers
number = Integer.parseInt(input);
- parseInt is a method of the
Integer class
- We did not have to create an Integer object
to use this method
- Similarly when we created the rand_int
method for our dice games, we did not have to create an object
- The method declaration
looked like this
public static int rand_int(int max, int min){
double value = Math.random() * (max - min) + min;
return (int) value;
}
- The keyword
static
means an object does not have to be created
to use this method
- In a certain sense, static methods live inside a class
- While all other methods live inside their objects
Dot Notation
- When we are calling a method defined inside the current class
we just use the name of the method
- When we call a method outside the current class
we have to use dot notation
- Dot notation has the following format
CLASS_NAME_OR_OBJECT_NAME.METHOD_NAME(PARAMETER_LIST
- When we are calling a static method we use the name of the class before the dot
int score = Integer.parseInt(input);
- For all other method calls we use the name of the variable pointing to the object
String name = "John Smith"
System.out.println(name + " is " name.length() + " long");
String Methods
- Here is a list of some of the more useful String methods
Method |
Description |
Return Type |
charAt(index) |
Returns the character at a specific position |
char |
contains(text) |
Returns true if the string contains the text |
boolean |
length() |
Returns the number of characters in the string |
int |
split(text) |
Returns an array of strings. The text is used to mark off
each string from the next
|
String [] |
charAt(index)
- A string is like an array of characters
- But you can't use the [] operator with strings
- You use the charAt method instead
- As with arrays you use zero-based indexing
- So the index of the first character is 0
length()
split(text)
- split splits a string into an array of strings
- In order to spilt the string into small sub-strings we need to
know characters to use as the border between adjacent sub_strings
- The string that serves as the boundary is called a
delimiter
- For example say we had the following string
"John, Smith, 0124353"
- Here the delimiter is ", "
- The code below shows split in action
public class SplitString {
public static void main (String[] args){
String string = args[0];
String delimiter = args[1];
String [] fields = string.split(delimiter);
for (int i = 0; i fields.length; i++){
System.out.println(fields[i]);
}
}
}
$ java SplitString "John, Smith, 0124353" ", "
John
Smith
0124353
$ java SplitString "2020-01-10" "-"
2020
01
10
trim()
- trim returns a new string with
whitespace
characters from the beginning and end of the string removed
- The whitespace characters are
Space | ' ' |
Tab | '\t' |
Newline | '\n' |
The Empty String
- The empty string
is a string with nothing in it
- The empty string acts like zero for strings
- If you add a number to zero the result is the number you added
- This is also true for the empty string
Strings Cannot Be Changed
- String objects are
immutable
- That means they cannot be changed
- This was a design decision made by the the creators of the Java language
- Every time a string is created while running a Java program it is
put in a special place called a string pool
- So when we write
s1 = "dog"
- This is the picture in memory
- But if the program creates another string with the same characters
a new String object will not be created
- Instead a new variable will point to an existing string
with the same characters
s2 = "dog"
- This is done to save memory space
- What happens when you assign a new string to the
variable s1?
- In that case the a new string object is created
- But the old one remains in memory
s1 = "cat"
- What happens when you use one string to create another
by concatenation?
- Again a new string is created
s1 = s1 + "food"
Packages
- There are literally thousands of classes in Java
- There are so many that there was a need to arrange them in some order
- This is done by collecting classes into
packages
- A package is a collection of related Java classes
Using Java Packages
- There are hundreds of Java packages
- To use a class in a program its package must be
brought into the RAM of a running program
- Bringing a package into RAM is called
importing
- There are so many packages that it is impossible to import them all
- Instead the Java interpreter automatically imports a package
with many classes useful for a broad range of task
- The name of this package is java.lang
- It includes a number of basic Java classes
Importing Packages
Using a Scanner Objects
Scanner Methods
- The Scannerobject provides different methods to do this
to return data of different types
Method | Description |
next() |
Reads and returns the next
token
as a String
|
nextDouble() |
reads and returns a double value |
nextInt() |
reads and returns an int value |
nextLine() |
reads and returns the next line of input as a
String
|
- Whenever you call one of these Scannermethods, it
- Waits for the user to type in some data and hit Enter or Return
- Takes in the data entered at the keyboard as a string
- Converts that data into a value of the right data type
- Returns that data to the statement that called it
Attendance