Java Tutorials

100+ tutorials, explanation videos, and examples to help you develop creative and technical skills.

Data Types in Java

Data Types are used to define what kind of data value a variable can take. This data value could be a digit (0-9), alphabet(a-z or A-Z) or decimal value (0.5) etc.

A variable must take a data value that corresponds to its data type. For example if a variable has int data type then it must take an integer value between 0-1 it cannot take values between a-z or A-Z

There are two types of Programming Languages:-
1. In which a variable value must correspond to its data value it cannot take other data values are called Statically Typed Languages. Ex. Java, c, c++.
2. In which a variable’s data type can be changed are called Dynamic Typed Languages. Ex. Python, Php

There are two types of Data Types:-

1. Primitive Data Types
2. Non-Primitive Data Types

1. Primitive Data Types

Primitive data types are only single values. These are not capable of doing something special. For example ‘int a = 10’, In this statement, we can’t directly determine the length of ‘a’ variable (In statement ‘a’ variable’s length is 2).

1. byte - byte can have values between -128 – 127. It is used to save memory when you are certain that the value would be in the limit between -128 – 127.
Default value is 0

2. short - short can have values between -32768 – 32767. You can use it when you are certain that the value would be in the limit between -32768 – 32767.
Default value is 0

3. int - int can have values between -2,147,483,648 – 2,147,483,647. You can use it when you are certain that the value would the be in limit between -2,147,483,648 – 2,147,483,647.
Default value is 0

4. long - long can have values between -2 63 – (2 63 – 1). You can use it when you are certain that the value would be in the limit between -2 63 – (2 63 – 1)
Default value is 0

5. float - float values lie between 0-9 decimal values ex. 5.0, 1.2 etc. float can give you 6-7 digits decimal points precision but you need to explicitly add the suffix ‘f’.
Default value is 0.0 (0.0f)

6. double - double values lie between 0-9 decimal values ex. 5.0, 1.2, etc. double can give you 15-16 digits decimal points precision.
Default value is 0.0.

7. char - char can hold 2 bytes of Unicode character
Default value is \u0000 (‘0’)

8. boolean - double has only two values either true or false. boolean is usually used in conditions, loops.
Default value is false.

2. Non-Primitive Data Types

Non Primitive Data Types has the capability of doing something special. For example ‘ String a = “10”; ’, In this statement, we can directly determine the length of ‘a’ variable by using the length() function of String data type.

1. String - tring is a pre-defined class in java that consists of some pre-defined functions like length(). These functions allow us to do some special tasks which primitive data types don’t allow. String is an array of characters. In char data type we can only assign one character to it but in String, we can assign a sequence of 2 or more characters.

Note:- We have a complete tutorial on String class. You can learn more about String class in our next Tutorials

1. Array - Array is a collection of similar types of data.

Note:- We have a complete tutorial on Arrays. You can learn more about Arrays in our next Tutorials

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.