Data Types in Java Programming

l ecture c ontents n.w
1 / 13
Embed
Share

Explore the two groups of data types in Java - Primitive Data Types and Reference/Non-Primitive Data Types. Learn about integral, floating-point, character, and boolean data types with examples and default values.

  • Java Programming
  • Data Types
  • Integral
  • Floating-Point
  • Character

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. L ecture C ontents Data Types in Java

  2. Data Types in Java There are two groups of data types available in Java: Primitive Data Types Reference/Object Data Types/Non-Primitive Data Types Primitive Data Types There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. All primitive types in Java have a defined size (in bits). Six of the types are numeric (byte, short, int, long, float, double) The char type holds characters The boolean type holds truth values

  3. Primitive Data Types Primitive DataTypes Floating Point Boolean Integer Character boolean char double float long byte short int (1byte) (2 bytes) (8 bytes) (4 bytes) (8 bytes) ( 1 byte) ( 2 bytes) (4 bytes)

  4. Integral Data Types 4 types based on integral values: byte, short, int, long All numeric types are signed. There are NO unsigned types in Java. Type Range Size Example Default Value 0 -128 ( 2^7) to +127 (2^7 -1) byte 8 bits byte b= 10; short 16 bits -32768( 2^15)to +32767(2^15-1) 0 short s = 1000; int 32 bits -2147483648 ( 2^31) to +2147483647 (2^31 1) 0 int a = 10000; -9223372036854775808 ( 2^63) to long a = 100000L; long 64 bits 0L +9223372036854775807 (2^63 1)

  5. Floating point Data Types 2 types based on floating point values: float and double These two data types represent numbers in fractional parts. float data type stores 7 significant digits. double data type stores 15 significant digits. Example Default Value Type Size Range 38 38 -3.4 * 10 to +3.4 * 10 float 32 bits 0.0f float f1 = 24.5f; 308 308 -1.7 * 10 to +1.7 * 10 double 64 bits 0.0d double d1 = 123.4;

  6. Character data type The char type defines a single character. In Java, character types are 16-bits. Java character data type stores characters in unicode format. Unicode is an international character set which defines characters and symbols from several languages different world. (Unicode includes ASCII at its low range (0-255)) A character literal represented in a single character enclosed in the single quotes. Characters can be converted to integers to perform mathematical functions on them. Example Default Value Type Size Range char 16 bits '\u0000' char ch='A ; '\u0000' (or 0) to '\uffff' (or 65,535 inclusive)

  7. Boolean data type The boolean type defines a truth value: true or false. It is logical type having only values: true or false booleans are often used in control structures to represent a condition or state. booleans CANNOT be converted to an integer type. Example Default Value Type Values boolean false boolean result=true; true or false

  8. Reference/Object Data Types/Non-Primitive Data Types Non-primitive, or reference data types, don't store the value, but store a reference to that value. Reference data types can be String , array, or class. Default value for reference variables is null. Non-Primitive Datatypes Array String Class

  9. String Data type Strings are not a primitive data type in Java. They are what s called an Object of type java.lang.String class. Strings are sequences of characters or a String in Java, representing a collection of characters surrounded by double quotations . Strings are constants and cannot be changed after they are created. Strings have a special append operator + that concatenates strings. The String class contains methods that can perform certain operations on strings. The default value for string variable is null. E.g. String s = Java ;

  10. Class Data type A class is a data type in Java that defines a template or blueprint for an object. In other words a class refers to the category or type of objects. A class allows you to declare custom data types. A class has variables defining properties and methods defining behavior of its objects.

  11. Example class animal { String bread, color; int maxAge; animal(){ } void displayanimalinfo() { } public static void main(String args[]){ animal cat = new animal(); } }

  12. Array Data type Arrays are used to store multiple homogeneous values in a single variable. Note, in Java array indexes start from zero. E.g. int num[]= new num[10]; or String fruits[] = { apple , banana , mango };

  13. Thank You

More Related Content