Fundamental Data Types Declaring Base Types Base primitive

Fundamental Data Types

Declaring Base Types � Base “primitive” types:

Primitive Types Type Description Size int The integer type, with range -2, 147, 483, 648. . . 2, 147, 483, 647 4 bytes byte The type describing a single byte, with range -128. . . 127 1 byte short The short integer type, with range -32768. . . 32767 long 2 bytes The long integer type, with range -9, 223, 372, 036, 854, 775, 808. . . 8 -9, 223, 372, 036, 854, 775, 807 bytes doub The double-precision floating-point type, with a range of about le ± 10308 and about 15 significant decimal digits 8 bytes float The single-precision floating-point type, with a range of about ± 1038 and about 7 significant decimal digits 4 bytes char The character type, representing code units in the Unicode encoding scheme 2 bytes

Constants

Constants: final • 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.

Some Java Operators Precedence Higher Lower = Operator Description () Parentheses + * / % + - Positive Negative Multiplication Division Modulus (remainder) Addition Subtraction

Evaluation: Follow Rules from 9 th Grade Algebra � Sample Arithmetic Expressions: 2 2 * 3 2 + 4 * 3 - 7 4 / 2 10 % 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 • i++ • ++I increment operators:

Arithmetic Lab (ANGEL)

• A String: • String constants • String variables: • String length: • Empty string Strings

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


Dialog Boxes

static String show. Input. Dialog (object msg) number = JOption. Pane. show. Input. Dialog (“Enter a number: “); Input ? Enter an Integer: 24 OK Cancel

static int show. Confirm. Dialog (component parent, object msg) ans = JOption. Pane. Confirm. Dialog (“Try Again? “); Select an Option ? Try Again? Yes No Cancel

static void show. Message. Dialog (component parent, object msg) JOption. Pane. show. Message. Dialog (“The Number is an Integer “); Message i The Number is an Integer OK

Predefined Numeric Classes parse. Int parse. Float parse. Long, parse. Double

Example double _y 1; int _y 2; String _number; number = JOption. Pane. show. Input. Dialog (“Enter a number: “); y 1=Double. parse. Double(_number); y 2=Integer. parse. Int (_number);

• Math Class: • sqrt • pow • round • Plus others Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

Lab Completion
- Slides: 23