Java Data Types:
Java provides several built-in data types that can be used to declare variables and store different kinds of data
There are two types of data type available in Java.
1. Primitive Types
2. Non-Primitive Types (Reference Types)
- Primitive Types:
boolean
: Represents a boolean value (true
orfalse
).byte
: Represents a small integer value from -128 to 127.short
: Represents a short integer value from -32,768 to 32,767.int
: Represents an integer value from -2,147,483,648 to 2,147,483,647.long
: Represents a long integer value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.float
: Represents a single-precision floating-point value.double
: Represents a double-precision floating-point value.char
: Represents a single Unicode character.
- Non-Primitive Types (Reference Types):
String
: Represents a sequence of characters.Array
: A collection of elements of the same type.Class
: Objects created from user-defined classes.Interface
: Defines a contract for implementing classes.- Various other classes and interfaces are provided by Java libraries.
Java also allows you to create user-defined data types by defining your own classes and interfaces.
Here's an example that demonstrates the usage of different data types in Java:
javapublic class Example {
public static void main(String[] args) {
boolean isTrue = true;
byte myByte = 10;
short myShort = 100;
int myInt = 1000;
long myLong = 1000000L;
float myFloat = 3.14f;
double myDouble = 3.14159;
char myChar = 'A';
String myString = "Hello, world!";
int[] myArray = {1, 2, 3, 4, 5};
System.out.println(isTrue);
System.out.println(myByte);
System.out.println(myShort);
System.out.println(myInt);
System.out.println(myLong);
System.out.println(myFloat);
System.out.println(myDouble);
System.out.println(myChar);
System.out.println(myString);
System.out.println(myArray[0]);
}
}
In this example, variables of different data types are declared and initialized, and their values are printed using the System.out.println()
method.
Understanding and utilizing the appropriate data types in Java is crucial for managing and manipulating data effectively within your programs.
0 Comments