Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Software Engineering Module: Core of Java Topic: Data Types and Wrapper Classes TALENTSPRINT | © Copyright 2012 Data Types and Wrapper Classes The content in this presentation is aimed at teaching learners to: • Define various primitive data types and memory size • Describe the purpose and usage on wrapper classes TALENTSPRINT | © Copyright 2012 2 Data Types and Wrapper Classes Java Data Types: There are 8 primitive or built-in data types in Java: 4 integer types (byte, short, int, long). 2 floating point types (float, double). Boolean (boolean). Character (char). TALENTSPRINT | © Copyright 2012 3 Data Types and Wrapper Classes How are Java Data Types different from Data Types in other programming languages? Java Data Types are defined (almost) identically on every machine on which Java runs, unlike other programming languages. Unlike other programming languages, the size of data types in java language, is constant across all platforms. Java is a strongly typed language – Every variable must have a declared type. TALENTSPRINT | © Copyright 2012 4 Data Types and Wrapper Classes Java Data Types TALENTSPRINT | © Copyright 2012 5 Data Types and Wrapper Classes When to use byte, short and int? When working with arrays, its better to use byte instead of short and int. bytes takes 1 byte each, when compared to short and int which takes 2 and 4 bytes each. When you can take any datatype with variables as the disks are getting less expensive. Note Preferbly use, int to avoid type-miss match. While using byte instead of short and int make sure that the range of values is within the range of byte. TALENTSPRINT | © Copyright 2012 6 Data Types and Wrapper Classes What Data type would you use to store the following data? Speed of light Your grade in this course double char Your grade point average this term double/float Number of refrigerators in a room int Location of a point on a screen Float/int 2^65 BigInteger $234.77 double/int Half of $234.77 double/int Bits per second transmitted by modem TALENTSPRINT | © Copyright 2012 int/float 7 Data Types and Wrapper Classes Program showing the use of Data Types in Java: public class DataTypes { public static void main(String[] args) { boolean isReal=true; // Can be true or false only byte d= 122; // Must be less than 127 short e= -29000; // Must be less than 32767 int f= 100000; // Must be less than 2.1 long g= 999999999999L; // Must put L on end float h= 234.99F; // Must be < 3E38; F on end billion double i= 55E100; char cvalue= '4'; // char '4' is not integer 4 } } TALENTSPRINT | © Copyright 2012 8 Data Types and Wrapper Classes Program showing the use of Data Types in Java: Though Java is called an object-oriented language, primitive datatypes are not objects. Important Note Strings are objects, not primitives as String is a pre-defined class in Java. Example: String name= “TalentSprint”; Here, we are creating an object on the heap. TALENTSPRINT | © Copyright 2012 9 Data Types and Wrapper Classes Program showing the use of Data Types in Java: Important Unlike in other programming languages, where character Data Type supports ASCII, Java's character Data Type supports Unicode. Unicode is 16 bit character set. Thus, Java's character Data Type is from 0 to 65,536. TALENTSPRINT | © Copyright 2012 10 Data Types and Wrapper Classes Widening and Narrowing in Java: Consider the following statment: int a=10; int b; b=a; In the above statement b=a a is the source which holds the data b is the destination where the data is stored TALENTSPRINT | © Copyright 2012 11 Data Types and Wrapper Classes Widening in Java: Widening In an assignment, if the source datatype is smaller than the destination datatype then the assignment is called Widening. Note Widening happens implicitly in java. Example for Widening: byte byte_num = 10; int int_num = byte_num; //Implicit conversion byte_num reserves 2 bytes int_num reserves 4 bytes A value, which occupies 2 bytes can easily be stored in 4 bytes without loss of precision. TALENTSPRINT | © Copyright 2012 12 Data Types and Wrapper Classes Narrowing in Java: Narrowing In an assignment, if the destination datatype is smaller than the source datatype then the assignment is called Narrowing. Note For Narrowing, the user has to explicitly typecast it. Example for Narrowing: int int_num = 10; byte byte_num = int_num; // Compilation error The above line generates an error if we does not typecast it as below: byte byte_num = (byte)int_num; // correct TALENTSPRINT | © Copyright 2012 13 Data Types and Wrapper Classes Narrowing in Java: int int_num = 10; byte byte_num = int_num; // Compilation error byte byte_num = (byte)int_num; // correct byte_num reserves 2 bytes, int_num reserves 4 bytes. A value which occupies 4 bytes cannot be stored in 2 bytes. The above correct statement converts value with possible loss of precision. Possible implicit conversions: Possible explicit conversions: byte --> short --> int --> long float --> double long --> int --> short --> byte double --> float TALENTSPRINT | © Copyright 2012 14 Data Types and Wrapper Classes What are Wrapper Classes? Wrapper Class Provides the object representation for all the eight primitive datatypes. Note Java is said to be a purely object-oriented language, except for primitive datatypes. TALENTSPRINT | © Copyright 2012 15 Data Types and Wrapper Classes Why Wrapper Classes? Think of a scenario wherein a method returns an object as the return type. We cannot create objects for primitive datatypes. We cannot return the primitive datatypes as ours is Object Oriented. The only way we can achieve this is by creating object version(wrapper class) of the primitive datatype and returning the object version from the method. TALENTSPRINT | © Copyright 2012 16 Data Types and Wrapper Classes Purpose of using Wrapper Classes? The wrapper classes in the Java API serve two primary purposes: To provide a mechanism to “wrap” primitive values in an object, so that the primitives can be included in activities reserved for objects, like as being added to Collections. They are useful if the return type from a method is of type Object. Wrapper Classes are immutable. Important TALENTSPRINT | © Copyright 2012 17 Data Types and Wrapper Classes Primitive datatypes and their Wrapper Classes TALENTSPRINT | © Copyright 2012 18 Data Types and Wrapper Classes Creating Wrapper Objects: All of the wrapper classes except Character provide two constructors: One that takes a primitive of the type being constructed. Another takes a String representation of the type being constructed. Example : Integer i1 Integer i2 or Float f1 = Float f2 = Note = new Integer(42); = new Integer("42"); new Float(3.14f); new Float("3.14f"); Same applies for all other Wrapper Classes. TALENTSPRINT | © Copyright 2012 19 Data Types and Wrapper Classes Creating Wrapper Objects: Another way to create wrapper classes: The two static valueOf() methods provided in most of the wrapper classes gives us another approach for creating wrapper objects. /* converts 101011 to 43 and assigns the value 43 to the Integer object i2*/ Integer i2 = Integer.valueOf("101011", 2); Where 101011 is binary representation of 43 and 2 indicates the base. For example binary, octal, or Hexadecimal. // assigns 3.14 to the Float object f2 Float f2 = Float.valueOf("3.14f"); TALENTSPRINT | © Copyright 2012 20 Data Types and Wrapper Classes Wrapper Conversions: To convert the value of a wrapped numeric to a primitive, we use xxxValue() Methods. Where 'xxx' can be any of the six numeric datatypes. Example: byte, short, int etc. Each of the six numeric wrapper classes has six methods, so that any numeric wrapper can be converted to any primitive numeric type. // convert i2's value to a byte primitive Integer i2 = new Integer(42); byte b = i2.byteValue(); /* another of Integer's xxxValue methods which converts i2's value to a short primitive */ short s = i2.shortValue(); TALENTSPRINT | © Copyright 2012 21 Data Types and Wrapper Classes Wrapper Conversions: // convert f2's value to a short primitive Float f2 = new Float(3.14f); short s = f2.shortValue(); // result is 3 System.out.println(s); Note In the same way Float wrapper object can be converted to any other numeric datatype. Same applies for all other objects like Double, Long etc. Wrapper Classes belong to a package called java.lang Important TALENTSPRINT | © Copyright 2012 22 Data Types and Wrapper Classes Converting from String to primitives: We have six parseXxx() methods one for each numeric wrapper type. Example: parseInt(), parseFloat() etc. parseInt() returns integer primitive, so on parseXxx() returns the named primitive. // convert a String to a primitive double d4 = Double.parseDouble("3.14"); // result is d4 = 3.14 System.out.println("d4 = " + d4); TALENTSPRINT | © Copyright 2012 23 Data Types and Wrapper Classes Auto boxing and unboxing: Autoboxing is the process of converting a primitive type data into its corresponding wrapper class object. Example: Int i = 100; // now num refers to the object 100 Integer num = i; Unboxing is the process of converting a wrapper object into a primitive type. Example: Integer number = new Integer (100); /* Here without type casting number would be changed into primitive int type */ int num = number; TALENTSPRINT | © Copyright 2012 24 Data Types and Wrapper Classes Overview TALENTSPRINT | © Copyright 2012 25 Data Types and Wrapper Classes Overview In summary, the essential method signatures for Wrapper conversion methods are: Primitive xxxValue() To convert a Wrapper to apremitive Primitive parseXxx(String) To convert a String to apremitive Wrapper valueOf(String) TALENTSPRINT | © Copyright 2012 To convert a String to Wrapper 26 Data Types and Wrapper Classes TALENTSPRINT | © Copyright 2012 27