Java Data type and Identifier

In this tutorial we are going to see about Data types and Identifiers in Java.

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in memory.

Java data types are classified into two categories :

  1. Primitive Data type
  2. Non-Primitive Data type

The Primitive Types

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. The primitive types are also commonly referred to as simple types.

These can be put in four groups:

Integers : This group includes byte, short, int, and long, which are for whole-valued signed numbers.
Floating-point numbers : This group includes float and double, which represent numbers with fractional precision.
Characters : This group includes char, which represents symbols in a character set, like letters and numbers.
Boolean : This group includes Boolean, which is a special type for representing true/false values.

We will see the each types in details with example program in upcoming chapters.

Integers

Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, supports positive-only integers. Many other computer languages support both signed and unsigned integers. However, Java’s designers felt that unsigned integers were unnecessary.

byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;

short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default value zero. example: short s=11;

int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647. Default value zero. example: int i=10;

long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

Floating-Point Types

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or transcendental

such as sine and cosine, result in a value whose precision requires a floating-point type.

float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;

double : It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double db=11.123;

Characters

In Java, the data type used to store characters is char. However, C/C++ programmers beware: char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide.

char : It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535. example: char c=’a’;

Booleans

Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, as in the case of a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

Example: boolean b=true;

Non-Primitive(Reference) Data type

Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Student, etc.

Class objects and various type of array variables come under reference datatype.

Default value of any reference variable is null.

A reference variable can be used to refer any object of the declared type or any compatible type.

Example: Employee employee= new Employee(“Arun”);

String :

String is a special data type in Java. We will see more about String in upcoming chapter.

Identifiers in Java

All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:

  1. All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.
  2. After the first character, an identifier can have any combination of characters.
  3. A Java keyword cannot be used as an identifier.
  4. Identifiers in Java are case sensitive, foo and Foo are two different identifiers.

Read more about Java Best practices in Naming Conventions here.


Posts You may Like

  1. Java 8 Stream Collectors groupingBy with count and sum Example
  2. Java Best Practice – Statements
  3. How to Convert JSON String to Object using GSON
  4. Java 7 Switch statement on String
  5. First Java Application in Eclipse
  6. Java 8 Stream Filter and Collect Example
  7. Log4j Basic Configurator Example
  8. How to create XML file using DOM Parser
  9. How to convert Array into List in Java
  10. How to shuffle the element of collection (List)
  11. How to validate IPv4 Address using Java Regular Expression
  12. How to read XML file using SAX Parser
  13. How to find parent directory of file using Java
  14. Java 7 Re-throw of an Exception
  15. Log4j Example using Property File

Be the first to comment

Leave a Reply

Your email address will not be published.


*