Full width home advertisement

Post Page Advertisement [Top]

java data type

What is datatype?
A variable is the name that point to a specific information in computer memory(RAM).
Every information has specific datatype. And each datatype require different storage size in RAM. Therefore we declare variable with a datatype to allocate different storage size with its type. 

Why we require datatype?
  • For allocating different storage size in ram, because ram size is limited.
  • To different between its type.
    Explanation: If we declare a variable with String a = "5"  and we want to add 12 to variable a. Then the value of a become 512. To remove this problem datatype is useful too.

Basic syntax for writing a variable with a data type is:

datatype myVariableName;

There are two kind of data types in Java:
  • Primitive Data Type
  • Reference/Object type.

Primitive data type:

int: it is used for integral values.
  • Allocated size: 32 bit.
  • Minimum value:  - 2,147,483,648 (-2^31).
  • Maximum value: 2,147,483,647(inclusive) (2^31 -1).
  • Default value:  0 (zero).


byte: It is used to save space in large arrays, a byte is 4 times smaller than int.
  • Allocated size: 8 bit.
  • Minimum value: -128 (-2^7).
  • Maximum value: 127 (inclusive)(2^7 -1).
  • Default value: 0 (zero).


short: It is also same as int, but it is 2 times smaller than an int.
  • Allocated size: 16 bit.
  • Minimum value: -32,768 (-2^15).
  • Maximum value: 32,767 (inclusive) (2^15 -1).
  • Default value: 0 (zero).


long: it is used for a wider range value. 
  • Allocated size: 64 bit.
  • Minimum value: -9,223,372,036,854,775,808(-2^63).
  • Maximum value: 9,223,372,036,854,775,807 (inclusive)(2^63 -1).
  • Default value: 0L (zero). {In long datavalue, datatype should contains L as a suffix.}

float: it is used for precise values.
  • Allocated size: 32-bit IEEE 754 floating point (Single Precision).
  • Float data type is never used for precise values such as currency.
  • Float is mainly used to save memory in large arrays of floating point numbers
  • Default value: 0.0f(zero). {In double datavalue, datatype should contains f as a suffix.}

double: it is used for decimal values.
  • Allocated size: 64-bit IEEE 754 floating point (Double Precision).
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value: 0.0d (zero). {In double datavalue, datatype should contains d as a suffix.}

boolean: It is used for simple flags true/false condition.
  • Allocated size: 1 bit.
  • There is only two value true or false.
  • Default value: false.

char: it is used to store a character value.
  • Allocated size: 16 bit.
  • Minimum value: '\u0000' (or  0{zero}).
  • Maximum value: '\uffff' (or 65,535 inclusive).
  • It is used to store any character.
Reference/Object data type: It is used to allocate storage space for a object in computer memory (RAM).
  • Class objects and various type of array variables come under reference datatype.
  • Default Value: null.
  • Reference variable points to an object(which is instance of a class) in the computer memory.
  • Example:
    Car myCar = new Car();

    Here Car is a class, myCar is a variable and new is a keyword used to create a new copy of class as a object.

Deep and more detail in Java datatype.

Java Literals
A Java Literal is a source-code representation of fixed value. Without any computation, literal can be assigned to a variable.
It can assign to any primitive datatype variable.


byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.

Prefix 0(zero) is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For Example:

int decimal = 123;
int octal = 0124;
int hexa = 0x32;

String Literal in Java is specified with a back slash ("\") symbol and a character after this symbol. They can directly write in string like this: "Hello\nWorld".

These are String Literal in Java (which are same as in other language.):
NotationDescription
\bBackspace
\dddOctal Character
\fFormfeed
\nFor writing a new line.
\rCarriage return
\sSpace
\tTab
\'Single Quote
\"Double Quote
\\Back Slash
\uxxxxHexadecimal unicode character, where is x has some integer value.

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib