Docsity

Java Programming Fundamentals: Variables, Data Types, and Input/Output, Study notes of Information Technology

Partial preview of the text

Download Java Programming Fundamentals: Variables, Data Types, and Input/Output and more Study notes Information Technology in PDF only on Docsity!

L E C T U R E 0 4

New Mexico Tech: CSE/IT 111

Basic Computation (1)

Class Maintenance

New Mexico Tech: CSE/IT 111

Variables and Expressions

New Mexico Tech: CSE/IT 111

Once a person has understood

the way variables are used in programming,

he has understood the quintessence of programming.

--E.W. Dijkstra (1930-2002),

Notes on Structured Programming

(1969)

Variables

New Mexico Tech: CSE/IT 111

y

Variable

Program component used torepresent a piece of data or anobject Similar to a container^ Ù
Store data such as numbersand letters
y

Value

Number, letter, or other dataitem that a
variable
represents^ Ù
Can be changed

eggsPerBasket = 6;

y

Each variable is assignedone memory location

y

Value is encoded as a stringof zeros and ones, andplaced in the variable’smemory location

y

Variable names should behelpful and suggest thevariable’s use or kind of datait will hold

Data Types

01/28/

New Mexico Tech: CSE/IT 111 y

Data type

A specification for^ Ù

a

set of possible values

, stored in

memory in the same way, and Ù

the

operations

defined on these values

y

Class type

A data type for objects of a class^ Ù

Name of the class or interface used inthe object’s declaration

y

Primitive type

A data type for a basic value, such as asingle number or a single character, thatdoes not have smaller parts

y

Integer

A whole number, such as

5

or

Common data type:

int

y

Floating-point number

A number with a fractional part, such as 9.

or

Common data type:

double

y

Character

A single character (letters, digits, orpunctuation) that appears on thecomputer keyboard, such as

‘a’

or

‘H’

Ù

Enclose in single quotes Ù

Case sensitive

Data type:

char

y

Boolean

Either

true

or

false

Common data type:

boolean

Primitive Types

01/28/

TypeName New Mexico Tech: CSE/IT 111

Kind of Value

MemoryUsed

Range of Values

byte

Integer

1 byte

to

127

short

Integer

2 bytes

-32,

to

32,

int

Integer

4 bytes

-2,147,483,

to

2,147,483,

long

Integer

8 bytes

-9,223,372,036,854,775,

to

9,223,372,036,854,775,

float

Floating-point

4 bytes

±3.40282347×

to

±1.40239846×

double

Floating-point

8 bytes

±1.79769313486231570×

to

±4.94065645841246544×

char

Single character(Unicode)

2 bytes

All Unicode values from

0

to

65,

boolean

1 bit

true

or

false

Reserved Words

y New Mexico Tech: CSE/IT 111

Keyword

A word in a programming language that has a special predefinedmeaning Java: all keywords are entirely lowercase^ Ù
Primitive types,
if
,^
public
,^
class
,^
static
,^
void

Ù

Full list in front cover of
Java
book
Cannot be used as the names of variables, classes, or methods

y

Some words have predefined meaning but are notkeywords

main
,^
println
You can change the meaning of these words, but it is a REALLY badidea

Assignment Statements

y New Mexico Tech: CSE/IT 111

Assignment statement

A statement in a program that assigns a value to a variable

Variable

=

Expression

;

answer

=

42;

y

Assignment operator

The operator in an assignment statement that separates a variable from theexpression whose value is assigned to the variable^ Ù

Java: equal sign

=

y

Expression

A combination of operands and operators that represent a computation^ Ù

Can be another variable, a number, or a more complicated expression made using arithmetic operators

to combine variables and numbers

firstInitial

=

‘B’;

score

= numberOfCards

+

handicap;

count

= count

+

10;

Simple Keyboard Input

import New Mexico Tech: CSE/IT 111

java.util.Scanner;

public

class

EggBasket

{

public

static

void

main(String[]

args)

{

int

numberOfBaskets,

eggsPerBasket,

totalEggs;

Scanner

keyboard

= new

Scanner(System.in);

System.out.println("Enter

the

number

of

eggs

in

each

basket:");

eggsPerBasket

=

keyboard.nextInt();

System.out.println("Enter

the

number

of

baskets:");

numberOfBaskets

=

keyboard.nextInt();

… … …

}

}

Gets the

Scanner

class from the package

(library)

java.util

Sets up things so theprogram can acceptkeyboard input

Reads one whole numberfrom the keyboard

Simple Screen Output

import New Mexico Tech: CSE/IT 111

java.util.Scanner;

public

class

EggBasket

{

public

static

void

main(String[]

args)

{

int

numberOfBaskets,

eggsPerBasket,

totalEggs;

… … … System.out.println("If

you

have");

System.out.println(eggsPerBasket

+

"

eggs

per

basket

and");

System.out.println(numberOfBaskets

+

"

baskets,

then");

System.out.println("the

total

number

of

eggs

is

" +

totalEggs);

… … …

}}

JavaClass

Object

Method

Arguments

Concatenate (not addition)

Imprecision in Floating-Point Numbers

New Mexico Tech: CSE/IT 111

y

Integers are exactquantities

y

Floating-point numbersare only approximations

y

Memory only has room fora limited number of digits

0.333333 (and no more 3s)

Named Constants

y New Mexico Tech: CSE/IT 111

Named constants

Mechanism that allows you to define a variable, initialize it, and fix the value so itcannot be changed^ Ù

Gives a name to a constant Ù

public

: no restrictions on where you can use the name

Ù

final

: the program is not allowed to change the value

Syntax^ Ù

Declaration must occur within the class definition but outside of any methoddefinitions, including

main

method

public

static

final

Type Variable

=

Constant

;

Conventions^ Ù

Name of constant using all uppercase letters, with an underscore symbol _ betweenwords

Examples

public

static

final

int

MAX_STRIKES

=

3;

public

static

final

double

PI

= 3.14159;

Type Casting

y New Mexico Tech: CSE/IT 111

Type cast

An operation that temporarily changes the data type of a value from its normal typeto some other type^ Ù

Does not change the value of the source variable

Syntax

(Type_Name) Expression

Examples

double

money

=

7.83;

int

dollars

=

(int)money;

double

money

=

(double)dollars;

//same as “double money = dollars;” due to assignment compatibility

y

Truncate

To discard the part after the decimal point in a number NOT ROUNDING!

Type Casting a Character to an Integer

y New Mexico Tech: CSE/IT 111

Assignment of integers has no connection to themeaning of the characters

char symbol

System.out.println((int)symbol); Will display

, not

What could we do to get this to display the “correct” integer?

y

Characters are encoded using the Unicode numberingsystem (same as the ASCII system for English)

Each character (including numbers) corresponds to an integer