Data Types In Java With Examples | Java Tutoring
Beginners Programs :

Data Types In Java With Examples | Java Tutoring

Data types in Java, primitive types, Java environment variables, Types of variables in java with examples are listed here.The following post was written by an Expert Java Author who had high command on Java technologies.

If you have any doubts related to this subject filed about Java datatypes and Java variables, do comment at the end of the post or contact us. We are glad to help you out.

data types in java

What Are Data Types In Java?

Data Types In Java: Before we using a variable, we should specify what type (datatype) of variable it is.

Because, when we specify the datatype, the system can understand the memory requirements and the operations allowed on the corresponding variables.

Suppose we want to store age of a person, then we specify the variable as an integer. Guess we want to store temperature of a city, then we define the it as double. 

In Java, an integer occupies 4 bytes of memory where as a double occupy 8 bytes of memory.

We can apply almost all operators on integer type data, but we can not use some operators (like bitwise operators) to double type data. Below re the different types of primitive data types in java with examples listed.

Different Primitive Data Types In Java

In Java, we have eight basic  (primitive datatypes).

Each primitive type has its significance. Out of this eight primitive data types in Java , we have four datatypes to work with integer numbers, two datatypes to work with real numbers (numbers with fraction part), one datatype for character data, and one datatype for logical (boolean) data.

1) BYTE DATA TYPE IN JAVA

As it occupies 1 byte of memory, we can store a value between -128 to 127. If we try to store a value bigger than that we will get a compilation error.

Eg:         

  • byte b=25; is valid ✔
  •  Byte c=225; is not valid 

To work with single characters (ASCII values up to 127), we can use byte type as it uses only 1 byte of memory (against char which takes 2 bytes of memory).

2) SHORT DATATYPE IN JAVA:

It is used to store integers in the range -32768 to 32767. Any value out of this range cannot be kept as short. In that case, we should use int. (In C, short is a data modifier but in Java short is a datatype).

3) INT DATA TYPE IN JAVA:

This is the default integer type. Most of the times we use int type only to work with whole numbers even though they can be managed with byte or short.

We can use up to a 10 digit number (up to 200 crores nearly) with int type.

4) LONG DATATYPE IN JAVA:

When we want to store a value bigger than int range, we should use long type. With long, we can store up to a 19 digit number. (in C, long is a data modifier but in Java long is a data type).

When using a constant bigger than int range, we should suffix it with ‘l’ or ‘L’ to indicate it to be a long value.

Eg:          long a=131009; is fine

  • long b=123456789012345; is not fine 
  • long c=123456789012345L; is fine ✔

5) CHAR DATA TYPE IN JAVA:

In Java, char type takes 2 bytes of memory to support UniCode characters ( In C, char type is 1 byte as it supports only ASCII characters). As per UniCode, we have ” 65536 (216) “ characters numbered from 0 to 65535. The char type cannot take negative values.

Numeric type can take both positive and negative values. The first 256 (numbered from 0 to 255) characters of UniCode are the ASCII set of characters only. So UniCode is compatible with ASCII. So both in ASCII and UniCode ‘A’ is 65, ‘B’ is 66, ‘C’ is 67, ‘ a’ is 97, ‘b’ is 98, ‘0’ is 48, ‘1’ is 49, ‘\n’ is 10, ‘\t’ is 9.

Eg:          char a=55; is valid ✔

  • char b=’A’; is valid ✔
  • char c=-35; is not valid 

6) FLOAT DATATYPE IN JAVA 

To work with numbers with a fractional part, we can use float type. Even though we have float and double types to work with a fractional (decimal) type of data, double is preferred over the float.

Because accuracy with double is better than float. Allowed range of values is more in double than in float. The float cannot take a decimal value directly.

It should be converted to float before assignment. So, an almost float is not used in regular programming.

Eg:          float a=3.6; is not valid 

  • float b=3.6f; is valid ✔
  • float c=(float)3.6; is valid ✔

7) DOUBLE DATA TYPE IN JAVA:

This is the default datatype to store decimal (real numbers) values. By default, any value with a fractional part is treated as double by the system.

A bigger type (double) value can’t be given to a smaller type (float, int, byte, etc) variable. So the following assignments will result in compilation error.

Eg:          float a=5.8;

  • int b=5.8;
  • short c=5.8;

A smaller type value can be given to bigger type variable. So the following statements are valid.

Eg:          double a=3.9;

  • double b=834;
  • double c=’A’;
8) Boolean Datatype In Java : 

This is the type used to store logical values. In Java, ‘true’ and ‘false’ are reserved words to represent logical (boolean) values.

These are not compatible with other datatypes. So boolean values cannot be assigned to other types and another type of values cannot be assigned to boolean variables.

In java a logical expression results in a logical value (true or false).

Eg:         

  • boolean a=true; is valid ✔
  • boolean b=’true’; is not valid 
  • boolean c=”false”; is not valid 
  • boolean d=false; is valid ✔
  • boolean e=1; is not valid 
  • boolean f=34; is not valid 
  • boolean g=10<20; is valid ✔

Why Most Of The Programmers Prefer ” Double ” And ” Int ” ?

Generally, when an operation is performed on bytes or shorts they are internally upcasted to int before the actual operation takes place. Similarly, when we try to add two characters, their ASCII values (integers) are added.  ” Java is more strict in terms of type checking “.

It does not allow bigger type data to be assigned to smaller type. So it is advised to use integer type of even to store small values.

Similarly, when we perform an operation on 2 float type values, they are upcasted to double internally, and the actual operation will be conducted.

By default, the system considers a fractional value as double.

If we try to assign a fraction value to a float variable we may get a compilation error. So it is preferred to use double variables rather than float.

Reference Environment Variables

Along with the primitive types, we have a special group of variables known as reference variables. These reference variables can refer objects in the program.

Learn More Here: