The Best Technical Blog Ever

twitterrss

Data Types & Identifiers

The data type defines the kind of data stored in a variable. There are two types of data types,


  • primitive data type
  • non-primitive data type

(1). Primitive data type:

There are eight primitive data types. They are predefined by the java language and named by a keyword.


byte:

Byte data type is an 8-bit(1 byte) signed two's complement integer. Default value is 0. The value range is -128 to 127. It is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.


Example:
byte a = 50

short:

Short data type is a 16-bit signed two's complement integer. Default value is 0. The value range is -32,768 to 32,767. It can also be used to save memory as byte data type. A short is 2 times smaller than an integer.


Example:
short s = 2000

int:

int data type is a 32-bit signed two's complement integer. The default value is 0. The value range is - 2,147,483,648(-2^31) to 2,147,483,647(2^31 -1). It is generally used as the default data type for integral values unless there is a concern about memory.


Example:
int a = 200000

long:

long data type is a 64-bit signed two's complement integer. The default value is 0L. The value range is -9,223,372,036,854,775,808.(-2^63) to 9,223,372,036,854,775,807. (2^63 -1). This type is used when a wider range than int is needed.


Example:
long a = 150000L

float:

float data type is a single-precision 32-bit IEEE 754 floating point. The default value is 0.0f. It is mainly used to save memory in large arrays of floating point numbers. It is never used for precise values such as currency.


Example:
float f1 = 254.5f

double:

double data type is a double-precision 64-bit IEEE 754 floating point. Default value is 0.0d.This is generally used as the default data type for decimal values. Double data type should never be used for precise values such as currency.


Example:
double d1 = 153.4

boolean:

boolean data type represents one bit of information. Default value is false. There are only two possible values: true and false. It is used for simple flags that track true/false conditions.


Example:
boolean one = true

char:

char data type is a single 16-bit Unicode character. Minimum value is '\u0000' (or 0). Maximum value is '\uffff' (or 65,535 inclusive). It data type is used to store any character value.


Example:
char letterA ='C'


(2).Non-Primitive(Reference) Data type:

In non-primitive types, three categories are available. They are,


  • classes
  • arrays
  • interface

A reference data type is used to refer to an object. These variables are created using defined constructors of the classes and declared to be of a specific type that cannot be changed. Class objects, and various types of array variables come under reference data type. Null is the Default value of any reference variable. A reference variable can be used to refer to any object of the declared type or any compatible type.


Example:
Bank bank = new Bank("Loan");


Identifiers in Java:

All Java components require names for using classes, methods, interfaces and variables. These names are called Identifiers. Identifier should follow some rules. They are,


  • All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore(_).
  • After the first character, an identifier can have any combination of characters. no spaces are allowed.
  • A Java keyword cannot be used as an identifier.
  • Identifiers in Java are case sensitive, Bank and bank are two different identifiers.
Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status