Java is statically typed programming language which means variable types are known at the compile time.
int x = "GfG";"
gives compiler error in Java as we are trying to store string in an integer type. Data types matter in Java because of the following reasons, which are listed below:
Java has two categories in which data types are segregated
1. Primitive Data Type: These are the basic building blocks that store simple values directly in memory. Examples of primitive data types are
Note: The Boolean with uppercase B is a wrapper class for the primitive boolean type.
2. Non-Primitive Data Types (Object Types): These are reference types that store memory addresses of objects. Examples of Non-primitive data types are
The below diagram demonstrates different types of primitive and non-primitive data types in Java.
Primitive Data Types in JavaPrimitive data store only single values and have no additional capabilities. There are 8 primitive data types. They are depicted below in tabular format below as follows:
Type
Description
Default
Size
Example Literals
Range of values
boolean true or false false JVM-dependent (typically 1 byte) true, falsetrue, false
byte 8-bit signed integer 0 1 byte (none)-128 to 127
char Unicode character(16 bit) \u0000 2 bytes 'a', '\u0041', '\101', '\\', '\', '\n', 'β'0 to 65,535 (unsigned)
short 16-bit signed integer 0 2 bytes (none)-32,768 to 32,767
int 32-bit signed integer 0 4 bytes -2,0,1-2,147,483,648
to
2,147,483,647
long 64-bit signed integer 0L 8 bytes -2L,0L,1L-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
float 32-bit IEEE 754 floating-point 0.0f 4 bytes 3.14f, -1.23e-10f~6-7 significant decimal digits
double 64-bit IEEE 754 floating-point 0.0d 8 bytes 3.1415d, 1.23e100d~15-16 significant decimal digits
1. boolean Data TypeThe boolean data type represents a logical value that can be either true or false. Conceptually, it represents a single bit of information, but the actual size used by the virtual machine is implementation-dependent and typically at least one byte (eight bits) in practice. Values of the boolean type are not implicitly or explicitly converted to any other type using casts. However, programmers can write conversion code if needed.
Syntax:
boolean booleanVar;
Size : Virtual machine dependent (typically 1 byte, 8 bits)
Example: This example, demonstrating how to use boolean data type to display true/false values.
Java
// Demonstrating boolean data type
public class Geeks {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
System.out.println("Is Java fun? " + b1);
System.out.println("Is fish tasty? " + b2);
}
}
Is Java fun? true Is fish tasty? false2. byte Data Type
The byte data type is an 8-bit signed two's complement integer. The byte data type is useful for saving memory in large arrays.
Syntax:
byte byteVar;
Size : 1 byte (8 bits)
Example: This example, demonstrating how to use byte data type to display small integer values.
Java
// Demonstrating byte data type
public class Geeks {
public static void main(String[] args) {
byte a = 25;
byte t = -10;
System.out.println("Age: " + a);
System.out.println("Temperature: " + t);
}
}
Age: 25 Temperature: -103. short Data Type
The short data type is a 16-bit signed two's complement integer. Similar to byte, a short is used when memory savings matter, especially in large arrays where space is constrained.
Syntax:
short shortVar;
Size : 2 bytes (16 bits)
Example: This example, demonstrates how to use short data type to store moderately small integer value.
Java
// Demonstrating short data types
public class Geeks {
public static void main(String[] args) {
short num = 1000;
short t = -200;
System.out.println("Number of Students: " + num);
System.out.println("Temperature: " + t);
}
}
Number of Students: 1000 Temperature: -2004. int Data Type
It is a 32-bit signed two's complement integer.
Syntax:
int intVar;
Size : 4 bytes ( 32 bits )
Remember: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has a value in the range [0, 2 32 -1]. Use the Integer class to use the int data type as an unsigned integer.
Example: This example demonstrates how to use int data type to display larger integer values.
Java
// Demonstrating int data types
public class Geeks {
public static void main(String[] args) {
int p = 2000000;
int d = 150000000;
System.out.println("Population: " + p);
System.out.println("Distance: " + d);
}
}
Population: 2000000 Distance: 1500000005. long Data Type
The long data type is a 64-bit signed two's complement integer. It is used when an int is not large enough to hold a value, offering a much broader range.
Syntax:
long longVar;
Size : 8 bytes (64 bits)
Remember: In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2 64 -1. The Long class also contains methods like comparing Unsigned, divide Unsigned, etc to support arithmetic operations for unsigned long.
Example: This example demonstrates how to use long data type to store large integer value.
Java
// Demonstrating long data type
public class Geeks {
public static void main(String[] args) {
long w = 7800000000L;
long l = 9460730472580800L;
System.out.println("World Population: " + w);
System.out.println("Light Year Distance: " + l);
}
}
World Population: 7800000000 Light Year Distance: 94607304725808006. float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits).
Syntax:
float floatVar;
Size : 4 bytes (32 bits)
Example: This example demonstrates how to use float data type to store decimal value.
Java
// Demonstrating float data type
public class Geeks {
public static void main(String[] args) {
float pi = 3.14f;
float gravity = 9.81f;
System.out.println("Value of Pi: " + pi);
System.out.println("Gravity: " + gravity);
}
}
Value of Pi: 3.14 Gravity: 9.817. double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. The size of the double data type is 8 bytes or 64 bits.
Syntax:
double doubleVar;
Size : 8 bytes (64 bits)
Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use BigDecimal class instead.
It is recommended to go through rounding off errors in java.
Example: This example demonstrates how to use double data type to store precise decimal value.
Java
// Demonstrating double data type
public class Geeks {
public static void main(String[] args) {
double pi = 3.141592653589793;
double an = 6.02214076e23;
System.out.println("Value of Pi: " + pi);
System.out.println("Avogadro's Number: " + an);
}
}
Value of Pi: 3.141592653589793 Avogadro's Number: 6.02214076E238. char Data Type
The char data type is a single 16-bit Unicode character with the size of 2 bytes (16 bits).
Syntax:
char charVar;
Size : 2 bytes (16 bits)
Example: This example, demonstrates how to use char data type to store individual characters.
Java
// Demonstrating char data type
public class Geeks{
public static void main(String[] args) {
char g = 'A';
char s = '$';
System.out.println("Grade: " + g);
System.out.println("Symbol: " + s);
}
}
Grade: A Symbol: $Why is the Size of char 2 bytes in Java?
Unlike languages such as C or C++ that use the ASCII character set, Java uses the Unicode character set to support internationalization. Unicode requires more than 8 bits to represent a wide range of characters from different languages, including Latin, Greek, Cyrillic, Chinese, Arabic, and more. As a result, Java uses 2 bytes to store a char, ensuring it can represent any Unicode character.
Example: Here we are demonstrating how to use various primitive data types.
Java
// Java Program to Demonstrate Char Primitive Data Type
class Geeks
{
public static void main(String args[])
{
// Creating and initializing custom character
char a = 'G';
// Integer data type is generally
// used for numeric values
int i = 89;
// use byte and short
// if memory is a constraint
byte b = 4;
// this will give error as number is
// larger than byte range
// byte b1 = 7888888955;
short s = 56;
// this will give error as number is
// larger than short range
// short s1 = 87878787878;
// by default fraction value
// is double in java
double d = 4.355453532;
// for float use 'f' as suffix as standard
float f = 4.7333434f;
// need to hold big range of numbers then we need
// this data type
long l = 12121;
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("long: " + l);
}
}
char: G integer: 89 byte: 4 short: 56 float: 4.7333436 double: 4.355453532 long: 12121Non-Primitive (Reference) Data Types
The Non-Primitive (Reference) Data Types will contain a memory address of variable values because the reference types won’t store the variable value directly in memory. They are strings, objects, arrays, etc.
1. StringsStrings are defined as an array of characters. The difference between a character array and a string in Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char-type entities. Unlike C/C++, Java strings are not terminated with a null character.
Syntax: Declaring a string
<String_Type> <string_variable> = “<sequence_of_string>”;
Example: This example demonstrates how to use string variables to store and display text values.
Java
// Demonstrating String data type
public class Geeks {
public static void main(String[] args) {
String n = "Geek1";
String m = "Hello, World!";
System.out.println("Name: " + n);
System.out.println("Message: " + m);
}
}
Name: Geek1 Message: Hello, World!
Note: String cannot be modified after creation. Use StringBuilder for heavy string manipulation
2. ClassA Class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:
Example: This example demonstrates how to create a class with a constructor and method, and how to create an object to call the method.
Java
// Demonstrating how to create a class
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
void display() {
System.out.println(model + " " + year);
}
}
public class Geeks {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2020);
myCar.display();
}
}
3. Object
An Object is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :
Example: This example demonstrates how to create the object of a class.
Java
// Define the Car class
class Car {
String model;
int year;
// Constructor to initialize the Car object
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
// Main class to demonstrate object creation
public class Geeks {
public static void main(String[] args) {
// Create an object of the Car class
Car myCar = new Car("Honda", 2021);
// Access and print the object's properties
System.out.println("Car Model: " + myCar.model);
System.out.println("Car Year: " + myCar.year);
}
}
Car Model: Honda Car Year: 20214. Interface
Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
Example: This example demonstrates how to implement an interface.
Java
// Demonstrating the working of interface
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}
5. Array
An Array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++. The following are some important points about Java arrays.
Example: This example demonstrates how to create and access elements of an array.
Java
// Demonstrating how to create an array
public class Geeks {
public static void main(String[] args) {
int[] num = {1, 2, 3, 4, 5};
String[] arr = {"Geek1", "Geek2", "Geek3"};
System.out.println("First Number: " + num[0]);
System.out.println("Second Fruit: " + arr[1]);
}
}
First Number: 1 Second Fruit: Geek2Primitive vs Non-Primitive Data Types
The table below demonstrates the difference between Primitive and Non-Primitive Data types
Aspect
Primitive
Non-Primitive
Memory
Stored on the stack
Stored on the heap
Speed
Primitive data types are faster
Non-primitive data types are slower
Example
int x = 5;
String s = "Geeks";
Understanding Java’s data types is fundamental to efficient programming. Each data type has specific use cases and constraints, making it essential to choose the right type for the task at hand. This ensures optimal memory usage and program performance while leveraging Java’s strong typing system to catch errors early in the development process.
Check Out: Quiz on Data Type in Java
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4