Data Types In Java

In this article we are going to learn about Data Types In Java.

Data Types In Java

Data Types are divided into two types:

1. PRIMITIVE DATA TYPES                                                                                                                                       2. NON-PRIMITIVE DATA TYPES

Primitive data types contain: Byte, short, int, long, float, double, boolean, and char.

Non-Primitive data types contain:  String, Arrays and Classes.

PRIMITIVE DATA TYPES: A Primitive data type tell the type of a variable and kind of values it can hold. Primitive data types are eight types. They are:

1. byte: byte stores whole numbers from -128 to 127

2. short: short stores whole numbers, it holds whole numbers from -32768 to 32767

3. int: int stores whole numbers from -2147483648 to 2147483647

4. Long: Long stores whole numbers from -9223372036854775808 to 9223372036854775807

5. double: double stores fractional numbers. It stores sufficient 15 to 16 decimal digits

6. float: float stores fractional numbers in it. It has the capability of storing 6 to 7 decimal digits

7. boolean: boolean stores true or false values in it

8. char: char stores a single character in it

WHY IS SIZE OF CHARACTER IS 2 BYTES IN JAVA PROGRAMMING?

Java doesn’t use the ASCII character set; it uses Unicode character. So, in this unicode character the unicode need more than 8 bits this is because to represent a wide range of characters from different languages.

WHY DATA TYPES ARE IMPORTANT IN JAVA

Data types are very important in Java because, in Java, for memory efficiency, for performance, and for code clarity, these data types are important in java.

Memory Efficiency: It chooses the right type and saves memory.

Performance: These proper types reduce the runtime errors in Java.

Code Clarity: it has clearer and readable code.

NON-PRIMITIVE DATA TYPES: Non- Primitive data types contains memory address of variable. The reference types won’t store the variable value directly in memory. They are objects,arrays, and strings etc.

1. String: Strings are arrays of characters. string is helps to hold a sequence of characters in a single variable.

2. ARRAY: Array is used for storing elements of the same data type.

3. CLASS: A Class is a user defined data type, it means it can be created manually by the user, objects are created from a class,it defines the set of propeties which are common to all the objects of same type.

4. INTERFACE:  An interface is same as a class, the only difference is that interface contains abstract methods by default and doesnt have any body.

 

Leave a Comment