08 Feb Data Types in Java
Java has a variety of data types that you can use to store different kinds of information in your programs. The following are the types of data types in Java:
- Primitive
- Non-Primitive
Primitive Data Types
- byte: 8-bit integer (values from -128 to 127)
- short: 16-bit integer (values from -32,768 to 32,767)
- int: 32-bit integer (values from -2^31 to 2^31-1)
- long: 64-bit integer (values from -2^63 to 2^63-1)
- float: 32-bit floating point
- double: 64-bit floating point
- char: Single 16-bit Unicode character (e.g., ‘a’, ‘*’, etc.)
- boolean: Represents true or false
Non-Primitive Data Types
- String: A sequence of characters, e.g., “Amit Diwan”
- Array: A collection of elements of the same type.
- Class: Templates for creating objects and defining object data types and methods.
- Interface: An abstract type used to specify a behavior that classes must implement.
- Object: The root class of the Java class hierarchy. All classes inherit from Object.
Let us now see an example and work with some data types:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Studyopedia { public static void main(String[] args) { int num = 100; char letter = 'W'; boolean flag = true; String name = "Hello, Amit!"; System.out.println("Number: " + num); System.out.println("Letter: " + letter); System.out.println("Flag: " + flag); System.out.println("Name: " + name); } } |
Here is the output:
1 2 3 4 5 6 |
Number: 100 Letter: W Flag: true Name: Hello, Amit! |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments