In this tutorial, we will be learning about Data types in Java programming, and the various data types available in the Java programming language. We will mainly focus on various data types, their memory allocation size, and their range.
Data Types in Java Programming
Data types in Java programming are one of the basic building blocks of a program. It determines what type of data can be stored in a variable. The Java compiler needs this information before compiling in order to work correctly. In Java, there exist two data types:
List of Data Types in Java Programming
- Primitive data type
- Non-primitive data type
Primitive Data Type
Data Types in Java Programming is first data types of Primitive data type. Primitive data types are those types that are already defined in the Java programming language. We don’t have to explicitly define these data types to the Java compiler. They can be used directly in a program to store values. Java is packed with several data types out of the box, they are:
byte
A byte data types in Java Programming can be considered as the smallest integer data type that can store small integer values. It is an 8-bit signed two’s complement integer data type. This data type consumes a very less amount of memory space to store an integer value. The integer value that a byte type of variable can store should be between -128 to 127 (inclusive). This data type can be used where there is a need for a counter variable for small iterations.
- Memory: 1 byte (8-bit)
- Range: -128 to 127
- Default Value: 0
class ByteProgram{ public static void main(String arg[]) { byte number = 100; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 100
*/
short
short data types in Java Programming is basically similar to an integer data type but with a smaller range and smaller size occupied in the memory. It is just a 16-bit signed two’s complement integer data type. This data type allows a variable to store an integer value that ranges from -32768 to 32767 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is typically somewhere between a few thousand.
- Memory: 2 bytes (16-bits)
- Range: -32768 to 32767
- Default Value: 0
class ShortProgram{ public static void main(String arg[]) { short number = 31876; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 31876
*/
int
int data types in Java Programming is a 32-bit signed two’s complement integer data type. This data type allows a variable to store an integer value that ranges from -2147483648 to 2147483647 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is larger or somewhere between a few million.
- Memory: 4 bytes (32-bits)
- Range: -2147483648 to 2147483647
- Default Value: 0
class IntProgram{ public static void main(String arg[]) { int number = 2147483643; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 2147483643
*/
long
long data types in Java Programming is basically also an integer data type but with a larger range and larger memory occupied in the memory. It is a 64-bit signed two’s complement integer data type. This data type allows a variable to store an integer value that ranges from 9223372036854776000 to 9223372036854775999 (inclusive). This data type can be used in situations where there is a need for storing an integer value that is very large.
- Memory: 8 bytes (64-bit)
- Range: 9223372036854776000 to 9223372036854775999
- Default Value: 0
class LongProgram{ public static void main(String arg[]) { long number = 235634567634644565L; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 235634567634644565
*/
float
In technical terms, the float is considered as a 32-bit single-precision IEEE 754 floating-point data type in Java programming. This data type allows a variable to store a numerical value that has decimal numbers after the decimal point. This data type allows the storing of a floating-point number that ranges from -3.4 E38 to 3.4 E38. Make sure that there exist only a few digits after the decimal point (around 4 or 5).
- Memory: 4 bytes (32-bits)
- Range: -3.4 E38 to 3.4 E38
- Default Value: 0
class FloatProgram{ public static void main(String arg[]) { float number = 45764.176f; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 45764.176
*/
double
double data types in Java Programming is a 64-bit single-precision IEEE 754 floating-point data type. This data type allows a variable to store a numerical value that has decimal numbers after the decimal point. This data type allows storing of a floating-point number that ranges from -1.79 E308 to 1.79 E308. Make sure that there exist only a few digits after the decimal point (around 14 or 15).
- Memory: 8 bytes (64-bit)
- Range: -1.79 E308 to 1.79 E308
- Default Value: 0
class DoubleProgram{ public static void main(String arg[]) { double number = 646437.8357654345; System.out.println("Number variable has: " + number); } }
/* Output
Number variable has: 646437.8357654345
*/
char
char data types in Java Programming is a 16-bit Unicode character data type. This data type allows a variable to store a single Unicode character. The Unicode character can be anything between ‘\u0000’ and ‘\uffff’ (inclusive).
- Memory: 2 bytes (16-bits)
- Range: ‘\u0000’ to ‘\uffff’
- Default Value: None
class CharProgram{ public static void main(String arg[]) { char ch = '&'; System.out.println("Ch variable has: " + ch); } }
/* Output
Ch variable has: &
*/
boolean
boolean data types in Java Programming is a data type that is used to store flag values. This data type doesn’t have a fixed memory allocation size but it is assumed to a 1-bit. It allows storing a single character in a variable either true or false at a time.
- Memory: 1-bit (Assumed)
- Range: Only ‘true’ and ‘false’
- Default Value: false
class BooleanProgram{ public static void main(String arg[]) { boolean flag = true; System.out.println("Flag variable has: " + flag); } }
/* Output
Flag variable has: true
*/
Non-primitive Data Type
Data Types in Java Programming is second data type of non-primitive data type. Non-Primitive data types are those types that are actually not defined in the Java programming language but a programmer needs to explicitly define these data types to the Java compiler. They can also be used directly in a program to store values (just like primitive data types). For now, we will just scratch the surface of these data types, but we will them in-depth in our future tutorials.
String
The string data types in Java Programming is a non-primitive data type that stores a bunch of characters in a sequence. Strings can be used to form words or sentences as needed by the programmer/developer. A string can just be a character, word, line, or just a blank space. If we say a word say “pencil”, then “pencil” is a valid string. A string can be composed of alphabets, numbers, and special characters. A string is represented with “” symbols.
Some examples of a string are:
- “Jupiter”
- “hello123”
- “money$”
class StringProgram{ public static void main(String arg[]) { String letter = "a"; String word = "Mountain"; String line = "This is awesome"; String_mixed = "Mountain 33.35 with a symbol @"; System.out.println("Print a letter: " + letter); System.out.println("Print a word: " + word); System.out.println("Print a line: " + line); System.out.println("Print a mixed line: " + mixed); } }
/* Output
Print a letter: a
Print a word: Mountain
Print a line: This is awesome
Print a mixed line: We have 192 paper bags of $0.24 each
*/
Array
An array data types in Java Programming is also a non-primitive data type that holds a single type of data. The elements of an array will always have the same data type and it will not be changed for just one element. The elements of an array are structured in a sequential order where each element of the array can be accessed using its index value (starting from zero). There is no fixed size for an array as we see in primitive data types.
class ArrayProgram{ public static void main(String arg[]) { String[] simpleItems = {"Bricks", "Cake", "Noise", "Humor"}; System.out.println("1st element of simpleItems array: " + simpleItems [0]); System.out.println("2nd element of simpleItems array: " + simpleItems [1]); System.out.println("3rd element of simpleItems array: " + simpleItems [2]); System.out.println("4th element of simpleItems array: " + simpleItems [3]); System.out.println(); } }
/* Output
1st element of simpleItems array: Bricks
2nd element of simpleItems array: Cake
3rd element of simpleItems array: Noise
4th element of simpleItems array: Humor
*/
In the above program, the array “simpleItems ” is of type “String” which means it can only store String type value in it. The elements initialized to “simpleItems” array are “Bricks”, “Cake”, “Noise” and “Humor”. These elements can be accessed using their index values starting from zero.
- At index 0, the element in the array is “Bricks”.
- At index 1, the element in the array is “Cake”.
- At index 2, the element in the array is “Noise”.
- At index 3, the element in the array is “Humor”.
Class
In Java, everything is based on classes and objects. These classes are like “blueprints” which come to existence when their objects are created. Basically, a class can have variables and methods which are related to that class. Let’s try to understand a class with a simple example.
class Plastic { public int cost = 200; public String strength = "weak"; } class ClassProgram { public static void main(String[] args) { Plastic p = new Plastic(); System.out.println(p.cost); System.out.println(p.strength); } }
/* Output
200
weak
*/
In the above program, we have two classes namely “Plastic” and “ClassProgram”. The “ClassProgram” class is just used to contain the “main” method.
Here, the “Plastic” class has two variables that are related to that class. They are “cost” and “strength”. Both these variables are trying to tell the cost of the plastic material as well as the strength of that plastic material.
In the main method, we are creating an object of the Plastic class and storing the reference of the object in a variable name “p”. This “p” variable can be used to access the variables and functions of an object (in this case “cost” and “strength”). The main method also has two more statements that show how we are printing the values of the variables of the “p” object.
Interface
An interface data types in Java Programming is basically like a contract that guarantees that there is a function in the class that is specified in the interface.
Consider an interface that has a method declaration for the method “hello”. If this interface is implemented in a class, then it is mandatory that this class should also implement the method “hello”. If we just implement the interface to a class and not implement its functions, then the compiler throws an error and the program won’t compile.
Example 1:
public interface IHello { public void sayHello(); } class InterfaceProgram implements IHello{ public static void main(String[] args) { } }
// This program throws an error because we implemented the IHello interface but didn’t implement its method sayhello()
Example 2:
interface IHello { public void sayHello(); } class InterfaceProgram implements IHello{ public void sayHello() { System.out.println("Hello"); } public static void main(String[] args) { } }
// This program will not throw any error and compile successfully