Base “primitive” types:
TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single byte, with range byte shortThe short integer type, with range bytes long The long integer type, with range -9,223,372,036,854,775, ,223,372,036,854,775,807 8 bytes doub le The double-precision floating-point type, with a range of about ± and about 15 significant decimal digits 8 bytes Primitive Types float The single-precision floating-point type, with a range of about ±10 38 and about 7 significant decimal digits 4 bytes char The character type, representing code units in the Unicode encoding scheme 2 bytes bool ean The type with the two truth values false and true1 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
Precedence OperatorDescription Higher ( )Parentheses +Positive - Negative * Multiplication / Division % Modulus (remainder) +Addition Lower - Subtraction =
Sample Arithmetic Expressions: 2 2 * * / 2 10 % 3 (2 + 3) * (11 / 12)
May mix types Two variables only at a time! Mod (%) operator:
We can also do this using shorthand = 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: i++ ++I
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? ? NoCancel Select an Option Yes
static void showMessageDialog (component parent,object msg) JOptionPane.showMessageDialog (“The Number is an Integer “); The Number is an Integer i OK Message
parseInt parseLong, parseFloat parseDouble
double _y1; int _y2; String _number; number = JOptionPane.showInputDialog (“Enter a number: “); y1=Double.parseDouble(_number); y2= Integer.parseInt (_number);
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Math Class: sqrt pow round Plus others