base “primitive” types: typedescriptionsize intthe integer type, with range -2,147,483,648......

23
Fundamental Data Types

Upload: alden-burnison

Post on 14-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

Fundamental Data Types

Declaring Base Types

Base “primitive” types:

Type Description Size

int The integer type, with range -2,147,483,648 . . . 2,147,483,6474 bytes

byte The type describing a single byte, with range -128 . . . 127 1 byte

short The short integer type, with range -32768 . . . 32767 2 bytes

longThe long integer type, with range -9,223,372,036,854,775,808 . . . -9,223,372,036,854,775,807

8 bytes

double

The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits

8 bytes

Primitive Types

floatThe single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits

4 bytes

charThe character type, representing code units in the Unicode encoding scheme

2 bytes

boolean The type with the two truth values false and true 1 bit

Constants

• Create Examples

1. Create a constant for number of inches in an foot (FOOT_VALUE)

2. Create a constant for number of feet in a yard (YARD_VALUE).

3. Write code to find out how many yards given n feet.

Constants: final

Some Java OperatorsPrecedence Operator Description Higher ( ) Parentheses + Positive - Negative * Multiplication / Division % Modulus

(remainder)

+ Addition Lower - Subtraction

=

Evaluation: Follow Rules from 9th Grade Algebra

Sample Arithmetic Expressions:

22 * 32 + 4 * 3 - 74 / 210 % 3(2 + 3) * (11 / 12)

rules

May mix types

Two variables only at a time!

Mod (%) operator:

We can also do this using shorthand <op>=

a += 5; a = a + 5;

a -= 5; a = a - 5;

a *= 5; a = a * 5;

a /= 5; a = a / 5;

a %= 5; a = a % 5;

a++; a = a + 1;

a--; a = a - 1;

Variable Increment operators

Variable increment operators:• i++• ++I

Arithmetic Lab(ANGEL)

• A String:

• String constants

• String variables:

• String length:

• Empty string

Strings

String sub = greeting.substring(0, 5); // sub is "Hello“

Substrings

Dialog Boxes

static String showInputDialog (object msg)

number = JOptionPane.showInputDialog (“Enter a number: “);

Enter an Integer:?

OK Cancel

24

Input

static int showConfirmDialog (component parent, object msg)

ans = JOptionPane.ConfirmDialog (“Try Again? “);

Try Again??

No Cancel

Select an Option

Yes

static void showMessageDialog (component parent,object msg)

JOptionPane.showMessageDialog (“The Number is an Integer “);

The Number is an Integeri

OK

Message

Predefined Numeric Classes

parseInt parseLong, parseFloat parseDouble

Example

double _y1;int _y2;String _number;

number = JOptionPane.showInputDialog (“Enter a number: “);

y1=Double.parseDouble(_number);y2=Integer.parseInt (_number);

Big Java by Cay HorstmannCopyright © 2008 by John Wiley &

Sons. All rights reserved.

• Math Class: •sqrt •pow •round•Plus others

Lab Completion