0% found this document useful (0 votes)
66 views7 pages

Java Primitive Data Types Explained

This document provides an overview of the 8 primitive data types in Java, including boolean, byte, short, int, long, double, float, and char, along with their default values and examples of usage. It emphasizes that Java is a statically-typed language, requiring variable declaration before use. Additionally, it briefly mentions that strings are not primitive types but objects in Java.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views7 pages

Java Primitive Data Types Explained

This document provides an overview of the 8 primitive data types in Java, including boolean, byte, short, int, long, double, float, and char, along with their default values and examples of usage. It emphasizes that Java is a statically-typed language, requiring variable declaration before use. Additionally, it briefly mentions that strings are not primitive types but objects in Java.

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Data Types (Primitive)

In this tutorial, we will learn about all 8 primitive data types in Java with the
help of examples.

Java Data Types


As the name suggests, data types specify the type of data that can be
stored inside variables in Java.
Java is a statically-typed language. This means that all variables must be
declared before they can be used.

int speed;

Here, speed is a variable, and the data type of the variable is int .

The int data type determines that the speed variable can only contain
integers.
There are 8 data types predefined in Java, known as primitive data types.

Note: In addition to primitive data types, there are also referenced types
(object type).

8 Primitive Data Types


1. boolean type

• The boolean data type has two possible values, either true or false .

• Default value: false .

• They are usually used for true/false conditions.


Example 1: Java boolean data type
class Main {
public static void main(String[] args) {

boolean flag = true;


[Link](flag); // prints true
}
}
Run Code

2. byte type

• The byte data type can have values from -128 to 127 (8-bit signed two's
complement integer).
• If it's certain that the value of a variable will be within -128 to 127, then it is
used instead of int to save memory.

• Default value: 0

Example 2: Java byte data type


class Main {
public static void main(String[] args) {

byte range;
range = 124;
[Link](range); // prints 124
}
}
Run Code

3. short type
• The short data type in Java can have values from -32768 to 32767 (16-bit
signed two's complement integer).
• If it's certain that the value of a variable will be within -32768 and 32767,
then it is used instead of other integer data types ( int , long ).

• Default value: 0

Example 3: Java short data type


class Main {
public static void main(String[] args) {

short temperature;
temperature = -200;
[Link](temperature); // prints -200
}
}
Run Code

4. int type

• The int data type can have values from -231 to 231-1 (32-bit signed two's
complement integer).
• If you are using Java 8 or later, you can use an unsigned 32-bit integer.
This will have a minimum value of 0 and a maximum value of 232-1. To
learn more, visit How to use the unsigned integer in java 8?
• Default value: 0

Example 4: Java int data type


class Main {
public static void main(String[] args) {

int range = -4250000;


[Link](range); // print -4250000
}
}
Run Code
5. long type

• The long data type can have values from -263 to 263-1 (64-bit signed two's
complement integer).
• If you are using Java 8 or later, you can use an unsigned 64-bit integer with
a minimum value of 0 and a maximum value of 264-1.
• Default value: 0

Example 5: Java long data type


class LongExample {
public static void main(String[] args) {

long range = -42332200000L;


[Link](range); // prints -42332200000
}
}
Run Code

Notice, the use of L at the end of -42332200000 . This represents that it's an
integer of the long type.

6. double type

• The double data type is a double-precision 64-bit floating-point.


• It should never be used for precise values such as currency.

• Default value: 0.0 (0.0d)

Example 6: Java double data type


class Main {
public static void main(String[] args) {

double number = -42.3;


[Link](number); // prints -42.3
}
}
Run Code

7. float type

• The float data type is a single-precision 32-bit floating-point. Learn more


about single-precision and double-precision floating-point if you are
interested.
• It should never be used for precise values such as currency.

• Default value: 0.0 (0.0f)

Example 7: Java float data type


class Main {
public static void main(String[] args) {

float number = -42.3f;


[Link](number); // prints -42.3
}
}
Run Code

Notice that we have used -42.3f instead of -42.3 in the above program. It's
because -42.3 is a double literal.
To tell the compiler to treat -42.3 as float rather than double , you need to
use f or F .
If you want to know about single-precision and double-precision, visit Java
single-precision and double-precision floating-point.

8. char type

• It's a 16-bit Unicode character.


• The minimum value of the char data type is '\u0000' (0) and the maximum
value of the is '\uffff' .

• Default value: '\u0000'

Example 8: Java char data type


class Main {
public static void main(String[] args) {

char letter = '\u0051';


[Link](letter); // prints Q
}
}
Run Code

Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.


Here is another example:

class Main {
public static void main(String[] args) {

char letter1 = '9';


[Link](letter1); // prints 9

char letter2 = 65;


[Link](letter2); // prints A

}
}
Run Code

Here, we have assigned 9 as a character (specified by single quotes) to


the letter1 variable. However, the letter2 variable is assigned 65 as an
integer number (no single quotes).
Hence, A is printed to the output. It is because Java treats characters as an
integer and the ASCII value of A is 65. To learn more about ASCII,
visit What is ASCII Code?.

String type
Java also provides support for character strings via [Link] class.
Strings in Java are not primitive types. Instead, they are objects. For
example,

String myString = "Java Programming";

Here, myString is an object of the String class. To learn more, visit Java
Strings.

Common questions

Powered by AI

Java's primitive data types support static typing by requiring explicit declaration of variable types before use, enforcing type safety and consistency throughout a program. This means every variable has a fixed data type which does not change or implicitly convert to another type during execution. For example, declaring a variable as 'int' means it can only store integers, ensuring the code is reliable and predictable .

Java's 'long' data type, allowing a range from -2^63 to 2^63-1, provides benefits for handling large numbers beyond the limits of 'int'. It is particularly useful in calculations involving extensive ranges, such as scientific computations. However, it consumes more memory than 'int', making it less efficient for smaller numbers or applications with strict memory constraints. Additionally, arithmetic operations with 'long' can be slower due to larger bit handling .

The 'short' data type should be preferred over 'int' or 'byte' when dealing with integer values that fall within the range of -32,768 to 32,767 and where memory efficiency is crucial. It uses less memory than 'int' (16 bits vs. 32 bits) while offering a larger range than 'byte' (-128 to 127). This is particularly beneficial in systems with memory constraints or when dealing with large collections of medium-range integer values .

Using 'double' or 'float' is inappropriate for scenarios requiring precise decimal representations, such as financial calculations (currency) and exact measurements in sensitive applications. This is because floating-point types can introduce rounding errors due to their binary representation of decimals, which can lead to inaccurate results in precise calculations .

The 'byte' data type is preferred over 'int' when the numerical values are certain to be within the range of -128 to 127 and memory usage optimization is essential. Since 'byte' uses 8 bits compared to 'int' which uses 32 bits, it saves significant memory space when handling large arrays or collections of values within this range .

Java's 'char' data type supports 16-bit Unicode character representation, which allows it to accommodate a wide international set of characters beyond ASCII, enabling support for multiple languages and scripts within the same program. This makes Java particularly suitable for developing applications intended for global markets, facilitating easier localization and internationalization because characters across different languages can be uniformly represented .

Static typing, as used in Java, ensures type accuracy at compile time by requiring explicit variable type declarations. This leads to fewer runtime errors, better performance due to type-specific optimizations, and enhanced code readability and maintenance. However, it can limit flexibility, requiring upfront design considerations and potentially more lines of code compared to dynamic typing, where types are resolved at runtime. This discipline in Java enhances reliability and stability in large-scale applications .

Java's 'char' data type is used to store single 16-bit Unicode characters, and it can represent character values as well as corresponding integer values from the Unicode set. Unlike numeric data types such as 'int' or 'short', which store integer values directly, 'char' interprets numbers as character codes. For example, assigning a numeric value to a 'char' type results in the corresponding ASCII character being represented, such as 65 representing 'A' .

Using 'boolean' to represent true/false states is more memory-efficient than using 'int'. A 'boolean' typically requires only one bit of memory, although it often uses a full byte (8 bits) at the hardware level. On the other hand, 'int' reserves 32 bits to represent values, making it significantly less efficient for merely storing binary states. Hence, for applications heavily reliant on numerous binary flags, 'boolean' conserves memory considerably .

Appending 'f' or 'F' to a float literal in Java specifies that the literal should be treated as a float rather than the default double precision. Without it, the compiler treats the number as a double, and attempting to assign it to a float variable without the suffix results in a compilation error due to type mismatch, since 'float' has a lower precision capacity compared to 'double' .

You might also like