Core Java

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 an identifier.

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, 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.

Published on Java Code Geeks with permission by Annamalai Thangaraj, partner at our JCG program. See the original article here: Java Data type and Identifier

Opinions expressed by Java Code Geeks contributors are their own.

Annamalai Thangaraj

Annamalai is a Software Engineer with 2+ years experience in Java, Spring, Struts, Hibernate, IDM/IAM, and Enterprise Web Application Development.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button