JAVA DATATYPES
By : Abhishek Mondal
Variables In Java
■ //declaring a variable x of type
//integer and assigned value
■ //30 int x = 30;
■ //declaring a variable c of type
//character and assigned value //‘r’
Char b = ‘r’;
■ A variable is a named field
containing information that your
program uses.You can also think of
a variable as a name of memory
location where the variable value is
stored.
■ In Java, a variable has to be
declared before it is used.
DataTypes
DataTypes refer to the type of data you will store in a
variable. For example a variable can be of type number, string
or an object of a custom class
Primitive And Non-PrimitiveTypes
A primitive data type is pre-built or defined by the
programming language, in this case Java.The size and type of
variable values are fixed and you do not have to do anything
special to use them.
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, class or interface
DataTypes In Java
Primitive DataTypes
Integers
■ There are 4 types of integer data types in Java – byte, short, int, long.
■ //A byte type variable takes 1 byte of storage and can store values from -128 to 127
byte a = 2;
■ //The short data type is a 16-bit. It has a minimum value of -32,768 and a maximum
value of 32,767 short b = 156;
■ //The int data type is a 32-bit signed two’s complement integer, taking values from -
2^31 to 2^31-1 int c = 32769;
■ //The long data type is a 64 bit 'two's complement integer taking values from -2^63 to
2^63- long l = 24678822;
Floating Points
■ There are 2 types of floating point data types in Java – double and float.
■ Single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. float f =
56.8929929;
■ double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. double d = 4.54877;
DoubleVs Float In Java
The Boolean Datatype
■ A boolean type is declared with the boolean keyword and only takes the values true or
false
■ boolean isSaladHealthy = true
■ boolean isSaladTasty = false
■ System.out.println(isSaladHealthy); // Outputs true
■ System.out.println(isSaladTasty); // Outputs false
The Char Datatype
■ The char data type is a single 16-bit Unicode character. A char
is a single character:
■ char grade = ‘A’;
■ System.out.println(grade); // Outputs A
The Non-Primitive Datatype
Class – Defining custom data types
■ 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.
■ For example you can define a class cat or car or
savings accounts to store objects of specific types.
A class has variables defining properties and
methods defining behavior of its objects.
Class Syntax
Strings In Java
■ A String in Java, representing a collection of characters, is actually an object of type
java.lang.String.The class contains methods that can perform certain operations on
strings.
■ String s = “hello”;
■ System.out.println(“The length of s is ” + s.length());//outputsThe length of s is 5
Arrays In Java
■ Arrays are used to store multiple homogeneous values in a single variable. Note, in
Java array indexes start from zero.
■ String fruits[] = {“apple”,“banana”,”mango”};
■ System.out.println(“The third fruit is” + fruits[2]); //outputsThe third fruit is mango
Interface
■ An interface in Java is used to specify the contract or capabilities that implementing
classes must implement. An interface has abstract methods, with only signatures,
which are then implemented by some classes. For example, every corporate
department may implement the Security Interface.
■ interface Security {
boolean accessCheck(String employeeToken);
}

Java data types

  • 1.
    JAVA DATATYPES By :Abhishek Mondal
  • 2.
    Variables In Java ■//declaring a variable x of type //integer and assigned value ■ //30 int x = 30; ■ //declaring a variable c of type //character and assigned value //‘r’ Char b = ‘r’; ■ A variable is a named field containing information that your program uses.You can also think of a variable as a name of memory location where the variable value is stored. ■ In Java, a variable has to be declared before it is used.
  • 3.
    DataTypes DataTypes refer tothe type of data you will store in a variable. For example a variable can be of type number, string or an object of a custom class
  • 4.
    Primitive And Non-PrimitiveTypes Aprimitive data type is pre-built or defined by the programming language, in this case Java.The size and type of variable values are fixed and you do not have to do anything special to use them. 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, class or interface
  • 5.
  • 6.
  • 7.
    Integers ■ There are4 types of integer data types in Java – byte, short, int, long. ■ //A byte type variable takes 1 byte of storage and can store values from -128 to 127 byte a = 2; ■ //The short data type is a 16-bit. It has a minimum value of -32,768 and a maximum value of 32,767 short b = 156; ■ //The int data type is a 32-bit signed two’s complement integer, taking values from - 2^31 to 2^31-1 int c = 32769; ■ //The long data type is a 64 bit 'two's complement integer taking values from -2^63 to 2^63- long l = 24678822;
  • 8.
    Floating Points ■ Thereare 2 types of floating point data types in Java – double and float. ■ Single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. float f = 56.8929929; ■ double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. double d = 4.54877;
  • 9.
  • 10.
    The Boolean Datatype ■A boolean type is declared with the boolean keyword and only takes the values true or false ■ boolean isSaladHealthy = true ■ boolean isSaladTasty = false ■ System.out.println(isSaladHealthy); // Outputs true ■ System.out.println(isSaladTasty); // Outputs false
  • 11.
    The Char Datatype ■The char data type is a single 16-bit Unicode character. A char is a single character: ■ char grade = ‘A’; ■ System.out.println(grade); // Outputs A
  • 12.
  • 13.
    Class – Definingcustom data types ■ 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. ■ For example you can define a class cat or car or savings accounts to store objects of specific types. A class has variables defining properties and methods defining behavior of its objects.
  • 14.
  • 15.
    Strings In Java ■A String in Java, representing a collection of characters, is actually an object of type java.lang.String.The class contains methods that can perform certain operations on strings. ■ String s = “hello”; ■ System.out.println(“The length of s is ” + s.length());//outputsThe length of s is 5
  • 16.
    Arrays In Java ■Arrays are used to store multiple homogeneous values in a single variable. Note, in Java array indexes start from zero. ■ String fruits[] = {“apple”,“banana”,”mango”}; ■ System.out.println(“The third fruit is” + fruits[2]); //outputsThe third fruit is mango
  • 17.
    Interface ■ An interfacein Java is used to specify the contract or capabilities that implementing classes must implement. An interface has abstract methods, with only signatures, which are then implemented by some classes. For example, every corporate department may implement the Security Interface. ■ interface Security { boolean accessCheck(String employeeToken); }