Friday, 11 May 2018

Chaudhari Karan

Java basic datatypes

    No comments:

Lecture 4  Java – Basic Datatypes

Hello friend, In this lecture, we will discuss Basic datatypes and Variable types.

Basic Datatypes:

Variable are reserved memory location to store data or value. Which means when we create the 
variable reserve some space in memory.

Data type based variable, the operating system allocates some memory and stored in reserved memory. Therefore, assigning different datatype to the variable, and it can be store decimals, integers, or character in these variables.

Two types of Datatypes in Java:

  • Primitive Datatypes
  • Object/Reference Datatypes

Primitive Datatypes:

Java has eight primitive data types. That is predefined by language and named by keyword. Now we  look eight primitive data types in detail.


  • Byte 8-bit two's complement integer data type
  • Byte minimum value is -128(-2^7)
  • Byte maximum value is 127(2^7-1)(Inclusive)
  • Default value is zero(0)
  • Byte is used for save space in large array, it is mainly place in integer, since byte is 4-times smaller than integer
  • Example: byte x = 10 , byte y = -20


  • Int is 32 two's complement integer data type
  • Int maximum value is  2,147,483,647(2^31-1)(Inclusive)
  • Int minimum value is  -2,147,483,648(-2^31)
  • Integer is used as default data type for integer values
  • Int default value is zero(0)
  • Example: int x = 200000, int y = -250000


  • Short is 16 two's complement integer data type
  • Short maximum value is 32,767(2^15-1)(Inclusive)
  • Short minimum value is -32,768(-2^15)
  • Short is used to save memory as byte data type. A short is two times smaller than an integer
  • Short default value is zero(0)
  • Example:  short x = 20000, short y = -30000


  • Double is the double-precision 64-bit IEEE 754 floating point data type
  • Double data type is used as default data type for decimal values, it is generally default choice
  • Double data type is never used for precise value like any currency($)
  • Its default value is 0.0d
  • Example: double d= 234.6


  • Float is the single-precision 32-bit IEEE 754 floating point data type
  • Float data type is save memory in arrays of floating point numbers
  • Its default value is 0.0f
  • Float data type is never store any precise value. Example, Currency($)
  • Example: float f = 123.6f


  • Long is 64-bit two complement integer  data type
  • Long maximum value is 9,223,372,036,854,775,807(2^63-1)(Inclusive)
  • Long minimum value is -9,223,372,036,854,775,808(-2^63)
  • Its default value is 0L
  • This type of data type is used for a wider range than int is needed
  • Example: long x = 200000L , long y = - 100000L


  • Char is single 16-bit Unicode character data type
  • Char is maximum value is 65,535(or ‘\uffff')
  • Char is minimum value is 0(or ‘\u0000')
  • Char is used for store any character
  • Example: char letterS = ‘S'


  • Boolean data type is represent one bit of information
  • Two value is available is Boolean:(1) True , (2)False
  • Its default value is false
  • Boolean is used for simple flags which track true/false condition
  • Example: boolean b = false

Object/Reference Datatype:

  • The reference variable is used to create the constructors of the classes. That are used to access objects. The variable is declared to the specific value which cannot be changed. Example: Student, Employee, etc.
  • Reference datatype default value is null.
  • Object class and different type of variable come under the reference datatype
  • A reference or object variable is used for refer to the object of the compatible type or declared type.
  • Example of reference datatype: Student student = new Student("James");

Java Literals

In Java, literal is the source code which represents of fixed value. It is represented directly in code without computation.

Literals assigned primitive data type variable. 


byte, long, int, and short expressed in octal(base 8), decimal(base 10), and hexadecimal(base 16) number system.

Prefix 0 is used for indicating octal, and prefix 0x is used to indicating hexadecimal number system. 


In Java, String literals are specified like in other languages enclosing a sequence character between the pair of double quotes. Here is an example of string literals :

char and String type of literals contain the Unicode character. 

In java support some special escape sequence for char and string literals.

Which is :
Character represented
Newline (0x0a)
Carriage return (0x0d)
Formfeed (0x0c)
Backspace (0x08)
Space (0x20)
Double quote
Single quote
Octal character (ddd)
Hexadecimal UNICODE character (xxxx)

Next: In next lecture, we will discuss Variable Types in Java.


Subscribe to this Blog via Email :