Question? Leave a message!




Built-in Types of Data

Built-in Types of Data
1.2 Builtin Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/29/11 6:40 AM Builtin Data Types Data type. A set of values and operations defined on those values. type set of values literal values operations 'A' char characters compare '' sequences of "Hello World" String concatenate "126 is fun" characters 17 add, subtract, int integers 12345 multiply, divide floatingpoint add, subtract, 3.1415 double 6.022e23 numbers multiply, divide true boolean truth values and, or, not false 2 Basic Definitions Variable. A name that refers to a value. Assignment statement. Associates a value with a variable. 3 Trace Trace. Table of variable values after each statement. 4 Text Text String data type. Useful for program input and output. Caveat. Meaning of characters depends on context. 6 Subdivisions of a Ruler public class Ruler public static void main(String args) String ruler1 = "1"; "1" String ruler2 = ruler1 + " 2 " + ruler1; "1 2 1" String ruler3 = ruler2 + " 3 " + ruler2; "1 2 1 3 1 2 1" String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler4); string concatenation java Ruler 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 Integers Integers int data type. Useful for expressing algorithms. 9 Integer Operations public class IntOps public static void main(String args) int a = Integer.parseInt(args0); commandline int b = Integer.parseInt(args1); arguments int sum = a + b; int prod = a b; int quot = a / b; int rem = a b; System.out.println(a + " + " + b + " = " + sum); System.out.println(a + " " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " " + b + " = " + rem); javac IntOps.java java IntOps 1234 99 1234 + 99 = 1333 1234 99 = 122166 Java automatically converts a, b , and rem to type String 1234 / 99 = 12 1234 99 = 46 1234 = 1299 + 46 10 FloatingPoint Numbers FloatingPoint Numbers double data type. Useful in scientific applications. 12 Excerpts from Java’s Math Library http://java.sun.com/javase/6/docs/api/java/lang/Math.html 13 Quadratic Equation 2 2 Ex. Solve quadratic equation x + bx + c = 0. "b ± b " 4c roots = 2 public class Quadratic public static void main(String args) // parse coefficients from commandline double b = Double.parseDouble(args0); double c = Double.parseDouble(args1); // calculate roots double discriminant = bb 4.0c; double d = Math.sqrt(discriminant); double root1 = (b + d) / 2.0; double root2 = (b d) / 2.0; // print them out System.out.println(root1); System.out.println(root2); 14 Testing Testing. Some valid and invalid inputs. 2 x – 3x + 2 java Quadratic –3.0 2.0 2.0 commandline arguments 1.0 2 x – x 1 java Quadratic –1.0 –1.0 1.618033988749895 golden ratio 0.6180339887498949 2 x + x + 1 java Quadratic 1.0 1.0 NaN not a number NaN java Quadratic 1.0 hello java.lang.NumberFormatException: hello java Quadratic 1.0 java.lang.ArrayIndexOutOfBoundsException 15 Booleans Booleans boolean data type. Useful to control logic and flow of a program. 17 Comparisons Comparisons. Take operands of one type and produce an operand of type boolean. 18 Leap Year Q. Is a given year a leap year A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100. public class LeapYear public static void main(String args) int year = Integer.parseInt(args0); boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year 4 == 0) (year 100 = 0); // or divisible by 400 isLeapYear = isLeapYear (year 400 == 0); System.out.println(isLeapYear); java LeapYear 2004 true java LeapYear 1900 false java LeapYear 2000 true 19 Type Conversion Type Conversion Type conversion. Convert from one type of data to another.   Automatic: no loss of precision; or with strings.   Explicit: cast; or method. 21 Random Integer Ex. Generate a pseudorandom number between 0 and N1. public class RandomInt public static void main(String args) int N = Integer.parseInt(args0); double r = Math.random(); String to int (method) int n = (int) (r N); double between 0.0 and 1.0 int to double (automatic) double to int (cast) System.out.println("random integer is " + n); int to String (automatic) java RandomInt 6 random integer is 3 java RandomInt 6 random integer is 0 java RandomInt 10000 random integer is 3184 22 Summary A data type is a set of values and operations on those values.   String text processing.   double, int mathematical calculation.   boolean decision making. In Java, you must:   Declare type of values.   Convert between types when necessary. Why do we need types   Type conversion must be done at some level. example of bad type conversion   Compiler can help do it correctly.   Example: in 1996, Ariane 5 rocket exploded after takeoff because of bad type conversion. 23
Website URL
Comment