1 / 20

Fundamental data types

Fundamental data types . Horstmann Chapter 4. Constants. Variables change, constants don't. final <datatype> <NAME> = <value> ;. final double PI = 3.14159;. … areaOfCircle = radius * radius * PI;. Representing numbers.

kay
Download Presentation

Fundamental data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamental data types Horstmann Chapter 4

  2. Constants Variables change, constants don't final <datatype> <NAME> = <value> ; final double PI = 3.14159; … areaOfCircle = radius * radius * PI;

  3. Representing numbers At base, everything in a computer is stored by bits of memory, which can be either on or off (1 or 0). Given 3 bits, what numbers can you represent? -4 -3 -2 -1 0 1 2 3 111 110 101 100 000 001 010 011 7 6 5 4 0 1 2 3 22*1 + 21*1+ 20*1 = 4 + 2 + 1 = 7 22*1 + 21*1+ 20*0 = 4 + 2 + 0 = 6 22*1 + 21*0+ 20*1 = 4 + 0 + 1 = 5 signed: -22…22-1 unsigned: Range 0…23-1 (= 0…7)

  4. Numerical data types: integers Name Range Size byte -27 … 27-18 bits (signed) byte variables can fit numbers from –128 to 127 short -215 … 215-1 16 bits (signed) short variables can fit numbers from –32768 to 32767 • int -231 … 231-132 bits (signed) • int variables can fit numbers from –2147483648 to 2147483647 We can write big numbers using scientific notation: 2147483647 ~ 2.15e+9 ~ 2.15*109 long -263 … 263-1 64 bits (signed) long variables can fit ~2.15e+9 times as many numbers as ints

  5. Numerical data types: reals float and double are used to store real numbers: numbers with a (possibly unending!) sequence of decimals; for example, pi: 3.141592653589793238462643383279502884197169399375106… float and double can only store a certain number of decimal digits: they can’t store them all! float -3.4e38 … 3.4e38 32 (6..7 digits accuracy) float pi = 3.141593; \\ the last 3 is a rounding up double -1.7e308 … 1.7e308 64 (14…15 digits acc.) double pi = 3.1415926535897932\\the 2 is rounding down

  6. What do these mean? short -215 … 215-1 16 bits (signed) • int -231… 231-132 bits (signed) • 215 = (2x2x2x2x2x2x2x2x2x2x2x2x2x2x2) • = 32,768 • How much bigger is 231 than 215 ? 216 times bigger • 231 = 2,147,483,648 float -3.4e38 … 3.4e38 32 bits (signed) 3.4e38 means 3.4 x 1038(scientific notation) To compare, 2,147,483,648 ~=2.1 x 109

  7. Numerical Operators +,-,/,*,% int i1 = 34 + 1; // i1 becomes 35 double d1 = 34.0 - 0.1; // d1 becomes 33.9 double d2 = 1.0/2.0; // d1 becomes 0.5 int i2 = 1/2; // i1 is 0!!!!! byte i3 = 1/2; // same for all integer types byte i4 = 20%3; // modulus operator. i4 is 2

  8. Literals A basic data type value which appears directly is a literal int i = 34; // 34 is a literal. double d = 5.0; // 5.0 is a literal, /* floating point values are taken to be doubles until proven otherwise*/ float myFloat = 5.0f; // f means make this a float float myDouble = 5.0d; // d means make it a double!!

  9. Shortcut Operators Some operations are so common, we have a shorthand. The following are equivalent: i = i + 1; i += 1; i++; d = d - 1.0; d -= 1.0; d--; f = f / 2.0; f /= 2.0;

  10. } Are all fundamental data types. Fundamental data types always start with a small letter. Fundamental Data Types byte, short, int, long float, double char boolean Variables of fundamental data types are just boxes in memory. Java gives us simple operations (like +, -, *, / etc) for doing things with fundamentaldata types.

  11. “Reference” data types String is not a primitive data type. String is a “reference” data type (it starts with a capital letter). Reference data types refer to (contain) a number of primitive elements combined together. For example, in String myName = “fintan”; The variable myName contains 6 char objects: the chars: ‘f’ ‘i’ ‘n’ ‘t’ ‘a’ ‘n’ Reference data types are very different from primitive types. They are not just boxes: they are Objects containing both data and methods (commands) for doing things with that data.

  12. Converting Strings to numbers We often want to convert a string into a number. One common situation is if the string has been entered by the user in a JOptionPane, for example. To convert Strings to some fundamental number type, we use conversion methods in a “Reference” datatype: String s = “10.123”; double d; d = Double.parseDouble(s); After this, the variable d contains the double 10.123 Note the capital letter in the “reference” data type parseDouble is a method that converts (parses) a string into a double. It’s stored in the Double reference type.

  13. Converting numbers to Strings Similarly, to convert numbers to Strings, we also use conversion methods in a reference type. int i = 10; String anIntString = Integer.toString(i); Integer holds methods for converting ints to Strings. Note the capital letter!!! And the variable i as an argument. This converts the value 10 in the int i into the string “10” and puts in in a String variable. Primitive type Reference type holding methods int Integer Reference types always start with capital letters float Float double Double

  14. } Strings to numbers: note String variables as arguments } Numbers back to strings: note number variables as arguments String conversion summary String s1 = “2”; String s2 = “10.123”; int i; float f; double d; i = Integer.parseInt(s1); f = Float.parseFloat(s2); d = Double.parseDouble(s2); s1 = Integer.toString(i); s2 = Float.toString(f); s2 = Double.toString(d);

  15. Converting between fundamental types These conversion methods are used for converting Strings to fundamental data types such as int, double etc. For converting between fundamental types, things are simpler. Number range hierarchy: double > float > int Assigning smaller range variable to larger range variable is ok: int i = 5; float f = i; // ok double d = f; // also ok

  16. Type conversion 2 To assign a larger range value to a smaller variable, use a type cast. This tells the computer to “squeeze” the value into the variable. Caution is always required! You may lose information because you may be putting a big number into a too-small box. float fl = (float)10.1; // double to float. ok. int i = (int)fl; // i now has the value 10! int i2 = 1000; // ok byte b = (byte)i2; // very bad idea!!!!

  17. Casting to integers..... REMEMBER! <integer variable> = <floating point value> loses all information after the decimal point. int i = (int)4.1; // i is now 4 double d = -2.5; i = (int)d; // i is now -2 The Math.round() method can be used to round double numbers to the nearest integer. Look up Math.round() in the API

  18. Character data type type char holds a single character: char c1 = 'A'; char c2 = '7'; char c3 = '\u0041'; // unicode for A Characters map onto numbers: int code = (int)'A'; Write a program to investigate characters and integers....

  19. Special characters char c1 = '\n'; // new line char c2 = '\t'; // tab stop char c3 = '\"'; // double quote More often, we use strings: String s1 = "Hello world"; String s2 = "I said \"Hello world\"\n";

  20. The boolean data type A variable of type int can have 232 different values A variable of type boolean can have 2 different values: boolean b = true; ....... b = false; More on booleans when we consider "if-statements"

More Related