Docsity

Java Variables and Expressions: Types, Constants, and User Input in CMSC 131 - Prof. Bonni, Study notes of Computer Science

Toggle sidebar
bg1
CMSC 131 Spring 2007
Jan Plane (adapted from Bonnie Dorr)
Lecture Set #3:
Java Expressions
Last time:
1.
Basics of Java programs
Today:
1.
Variables and types
2.
Expressions in Java
3.
User input
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Java Variables and Expressions: Types, Constants, and User Input in CMSC 131 - Prof. Bonni and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Lecture Set #3:

Java Expressions

Last time: 1.^

Basics of Java programs

Today: 1.^

Variables and types

2.^

Expressions in Java

3.^

User input

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Variables … 

… are named storage locations 

Recall that memory is a sequence of bits 

Question: How much memory to allocate fora variable’s value? 

Answer: A variable must have a

type

specifying how much storage to allocate.

5

x

Value

Variable

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Primitive Data Types In Detail^ Integer Types:

byte

1 byte

Range: -128 to +

short

2 bytes

Range: -32,000 to +32,

int

4 bytes

Range: -2 billion to +2 billion

long

8 bytes

Range: -9 quintillion to +9 quintillion

Floating-Point Types

:

float

4 bytes

-3.4x

to 3.4x

38 , 7 digits of precision

double

8 bytes

-1.7x

to 1.7x

, 15 digits of prec.

Other types

:

boolean

1 byte

true, false

char

2 bytes

A single (Unicode) character

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Primitive-Type Constants ^

Constants are also called

literals

^

Integer types:

byteshort

optional sign and digits (0-9): 12

0 1234567

intlong

Same as above, but followed by ‘L’ or ‘l’: -1394382953L

^

Floating-point types

:

double

Two allowable forms:

Decimal notation

: 3.

-234.

-43.

Scientific notation

: (use E or e for base 10 exponent) 3.145E

= 3.145 x 10

5 = 314500.

1834.23e-

= 1834.23 x 10

-6^ = 0.

float

Same as double, but followed by ‘f’ or ‘F’:

3.14159F

-43.2f

Note

: By default, integer constants are

int

, unless ‘L’/‘l’ is used to indicate they are

long

. Floating constants are

double

, unless ‘F’/‘f’ is used to indicate they are

float

.

Avoid this lowercase L. It lookstoo much like the digit ‘1’

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Common Numeric Operators ^

Arithmetic operators

:

^

Unary negation:

-x

^

Addition/subtraction:

x+y

x-y

^

Multiplication/division:

x*y

x/y

^

Division between integer types

truncates

to integer:

23/

→

5

^

x%y

returns the

remainder

of

x

divided by

y

:^

23%

→

3

^

Division with real types yields a real result:

23.0/4.

→

^

Comparison operators

:

^

Equality/inequality:

x == y

x != y

^

Less than/greater than:

x < y

x > y

^

Less than or equal/greater than or equal:

x <= y

x >= y

These comparison operators return a

boolean

value:

true

or

false

.

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Common String Operators ^

String Concatenation

: The ‘+’ operator

concatenates

(joins) two strings.

^

“Go

” + “

Terps

”^ →→→→

“GoTerps

”

^

When a string is concatenated with another type, the other type is first evaluated and converted

into its string representation.

(

degrees

”^ →→→→

“32degrees

”^

(1 + 2) + “

5 ”

→→→→

“

35

”

^

String Comparison

: Strings have special comparison functions.

s.equals(t)

: returns true if

s^

and

t^

have the same characters.

s.compareTo(t)

: compares strings

lexicographically

(dictionary order)

result < 0

if^ s

precedes

t

result == 0

if^ s

is equal to

t

result > 0

if^ s

follows

t

“dilbert

”.compareTo(

“

dogbert

”^

)^

→→→→

(which is < 0)

Note: Concatenation doesnot add any space

Both functions are case-sensitive.

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

User Input in Java 

We've done output (System.out); what aboutinput? 

Java 5.0 includes the

Scanner class

feature

^

Can use Scanner to create “scanner objects” ^

Scanner objects convert user input into data



To use Scannner need to

import

a library:

import java.util.Scanner;

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Example5.java

import

java.util.Scanner;

public

class

Example

public static void

main(String[]

args) {

int

i; double

d;

String s;Scanner sc =

new

Scanner(System.

in

System.

out

.print(

"Enter

an integer: "

i =

sc.nextInt(); System.

out

.print(

"Enter

a floating point value: "

d =

sc.nextDouble(); System.

out

.print(

"Enter

a string:

s =

sc.next(); System.

out

.println(

"Here is what you entered: "

System.

out

.println(i);

System.

out

.println(d);

System.

out

.println(s);

Create new scanner object

to read from keyboard

Include the definition ofthe Scanner utility^ Input an integer^ Input a double^ Input a string (upto white space)

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Debugging Java Programs 

Types of errors^ 

“Compile time”: caught by Eclipse / Java compiler^ 

Syntax

errors: typos, etc.

^

Type

errors: misuse of variables

^

“Run time”: appear during program execution ^

Division by 0 ^

Wrong outputs (because of mistakes in programming)



Eclipse helps catch compile time errors^ 

Red

: error

^

Yellow

: warning

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Example3.java – find the errors public class

Example3 {

public static void

main(String[] args) {

int

x = 7; int

y = 12; double

d = 72.33;

boolean

b =

true

;

char

c; String s;x = y + 24;y = 17.3;d = x;b = 17;c =

"cow"

;

s =

"Here is something weird "

  • x + y;

} }