Question? Leave a message!




Built-in Types of Data

Built-in Types of Data
Dr.AlexanderTyler Profile Pic
Dr.AlexanderTyler,India,Teacher
Published Date:21-07-2017
Website URL
Comment
1.2 Built-in 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 Built-in 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 floating-point 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); command-line 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 Floating-Point Numbers Floating-Point 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 command-line 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 command-line 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