Data types


Published on

Published in: Education
1 Comment
  • For Siebel CRM Online Training Register at
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Data types

  1. 1. Data types and Literals 1
  2. 2. ObjectivesOn completion of this period, you would be able to know• Data types in Java• Literals in Java 2
  3. 3. RecapIn the previous class we have discussed • White spaces, separators, literals and keywords • Comments in Java 3
  4. 4. Data types in Java• Data types in Java are classified into • Primitive types • Non-primitive types• Primitive types are also known as simple data types• A primitive type is predefined by the language• Primitive types are further classified into • Numeric types • Non-numeric types 4
  5. 5. Data types in Java• Non-primitive types are also known as reference types• Non-primitive types are • Classes • Interfaces • Arrays 5
  6. 6. The classification of data types in Java Data types in Java Primitive Non-primitive Numeric Non-numeric classe Arrays s integer char InterfaceFloating point Boolea n Fig. 6.1 Data types in Java 6
  7. 7. Data types• Primitive Java data types can be grouped into • integers • floating point numbers • characters • boolean 7
  8. 8. Integer types• Java defines four integer types: byte, short, int and long• All these are signed, positive and negative values• Java does not support unsigned types• byte is 8 bit width , short is 16 bit width int is 32 bit width and long is 64 bit width 8
  9. 9. byte type• The smallest type is byte• Range from -128 to +128• Byte variables are declared by use of keyword ‘byte ‘ eg. byte x, y; 9
  10. 10. short type• short is a signed 16 bit type.• Its range is from -32,768 to +32,767• It is probably the least used data type in Java• Keyword used is ‘short’eg. short x; 10
  11. 11. int type• The most commonly used integer data type is int• It has a range of -2,147,483,648 to 2,147,483,647• Keyword used is ‘int’eg. int x; 11
  12. 12. long type• long is a signed 64 bit type• Useful where an int type is not large enough to hold the desired value• The range of large is quite large• This makes it useful when large, whole numbers are needed• Keyword used is ‘long’eg. : long seconds; 12
  13. 13. floating point types• floating point numbers are also known as real numbers• Are used when fractional components are required• float and double are two kinds of floating point types• Keywords used are ‘float’ and ‘double’ 13
  14. 14. Float type• float specify a single precision value that uses 32 bits of storage• float type is useful when you require fractional component, but does not require a large degree of precisioneg. float area; 14
  15. 15. double type• double uses 64 bits of storage• When you need to maintain accuracy over many iterative calculations or manipulating large valued numbers double is the best choiceeg. : double area; 15
  16. 16. character type• In Java the data type used to store character is char• char in Java is different from char in C and C++• Java used Unicode to represent characters• Unicode was selected because it would help in internationalization of Java as a programming language• Java character is 16 bit type. The range of the characters is 0 to 65,536 16
  17. 17. boolean type• boolean data type is used to represent logical values that can be either true or false• All relational, conditional and logical operators return boolean values• It used only one bit of storage• Keyword used is ‘boolean’• eg;. • boolean flag; 17
  18. 18. The simple data types in Java• In summary Java defines eight simple types of data. They are • byte • short • int • long • char • float • double • boolean 18
  19. 19. Summary• In this class we have discussed • Data types in Java • Literals in Java 19
  20. 20. Quiz1.Which of the following data type can store only two valuesa) intb) floatc) booleand) All of the above 20
  21. 21. Quiz2.String Literal Enclosed witha) ‘‘b) ““c) **d) All the Above 21
  22. 22. Frequently Asked Questions1. List the different data types available in Java2. Explain the various data types available in Java3. List the Literal in Java4. Explain the Literals in Java 22