Sie sind auf Seite 1von 5

RANDOM NUMBERS IN JAVA

CSE 110: Introduction to Computer Science


Stony Brook University

Computers
They

cannot generate truly random numbers

are deterministic

given sequence of input always generates the


exact same result

True

randomness only results from natural sources

THE RANDOM CLASS


We

can cheat to get a different sequence of


random values each time we need one

These

pseudorandom values are the best we can


get without a true source of randomness

Javas

Random class lets us generate pseudorandom


sequences of numbers...

The

Random class is found in the java.util package

When

we create a new Random object, it is automatically


seeded with the system time
Separate

Random instances have different creation times, so


they generate different random sequences

Creation: Random

r = new Random();

RETRIEVING RANDOM VALUES

RETRIEVING VALUES (II)


To

To

get a random integer, use the nextInt() method:

int r = myRandom.nextInt(n);
This

get a random double, use the nextDouble() method:

double r = myRandom.nextDouble();
This

returns a random double value between 0 and 1.0

Multiply

returns a random integer value in the range 0(n-1)

this number by n to get a double between 0 and n:

double myValue = myRandom.nextDouble() * 5;


This generates a random double between 0.0 and 5.0

A string is just a sequence of characters


Strings can have 0 or more characters
(an empty string is still a sequence)

The String class is probably the most


frequently used class in Java programming

Strings in Java

Its automatically provided to all Java


programs (it lives in the java.lang
package)

CSE 110

Lets take a quick look at the methods


provided by the String class...

Intro to Computer Science


Stony Brook University

String Methods
String() creates a new String
String s = new String(Hello!);

Shorthand: String s = Hello!;


This shorthand only works for String!

length() returns the total number of


characters in the String

Extracting Data
From Strings
The positions in a String are
numbered from 0 to (length - 1)
charAt() returns the character at
a given position (index)
indexOf(str) returns the first
index at which str occurs in the string
(or -1 if it isnt there)

11

String Adjustments
trim() returns a new String
with no leading or trailing whitespace
toLowerCase()/toUpperCase()
return a new copy of the String in
lower/uppercase
All three methods leave the original
String unchanged
Java strings are immutable

10

Extracting Data
From Strings, contd
substring(start, end) returns a
new String containing the characters
from position start up to (but not
including) end
You can also call substring() with
exactly one argument
In this case, it returns everything from
the specified index through the end

12

String Equality

Objects & Primitives

What makes two Strings equal?


Do they have the same length? The
same case? The same characters?

There's a difference between primitive


(built-in) types and objects

According to Java,

Primitive variables hold an actual value

Object variables (references) only hold the


address of an object!

abcdef == abcdef

is false (they are not the same).


What's going on?

13

This causes problems when we try to


compare two objects

14

Java is Shallow
Java performs shallow comparisons by
default (using the == operator)
A shallow comparison looks at the value
immediately associated with a variable
This is okay for primitive types

For objects, this means that we compare


their memory addresses, not their contents!
Objects are only equal if they live at the
same memory address

15

String Equality
String has methods to test equality
based on content, not memory location
equals() returns true if two
Strings have the same sequence of
characters
Usage: firstString.equals(secondString)
equals() requires both strings to have
identical capitalization

16

Comparing Strings
compareTo() compares two strings
for their relative lexicographical ordering
case, then alphabetical, then by length

Usage: firstString.compareTo(secondString)
This method returns an integer value
Result

Positive

Negative

Meaning

first > second

first == second

first < second

Dont Be So
Sensitive!
Problem: equals() and compareTo()
are case-sensitive
Sometimes, we only want to compare two
Strings by length and/or characters

Java has two more methods for this


situation:
equalsIgnoreCase()
compareToIgnoreCase()

17

18

Das könnte Ihnen auch gefallen