Java Data Type:
- Data types represent the different values to be stored in the variable. Any value store in Variable using Java Data type.
- Using Java Data Types we can return value to perform any operation.
- Data type is used for representing the data in main memory (RAM) of the computer.
In java, there are two types of data types
- Primitive Java data types.
- Non-primitive Java data types.
Primitive data types
- byte, short, int and long data types are used for storing whole numbers.
- float and double are used for fractional numbers.
- char is used for storing characters(letters).
- boolean data type is used for variables that holds either true or false.
Example of Primitive DataType:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Primitive_Java { public static void main(String[] args) { // DataType Declaration byte b = 100; short s = 123; int i = 123543; long long_Val = 1234567891; float float_Val = 12.25f; double double_Val = 12345.234d; boolean boolean_True = true; boolean boolean_false = false; char char_Val = 'Y'; // Print Variable Values System.out.println("byte Value = " + b); System.out.println("short Value = " + s); System.out.println("int Value = " + i); System.out.println("long Value = " + long_Val); System.out.println("float Value = " + float_Val); System.out.println("double Value = " + double_Val); System.out.println("boolean Value = " + boolean_True); System.out.println("boolean Value = " + boolean_false); System.out.println("char Value = " + char_Val); } } |
Output:
1 2 3 4 5 6 7 8 9 | byte Value = 100 short Value = 123 int Value = 123543 long Value = 1234567891 float Value = 12.25 double Value = 12345.234 boolean Value = true boolean Value = false char Value = Y |
Non-primitive data types
- Non-primitive data types are created by programmer.
- String, Array are Non-Primitive Datatypes.
- It is also called as ‘Reference Variables’ or ‘Object reference’ because it refers a memory location where data is stored.
- On the other hand, primitive data types stores value.
- It is class in java provided with several methods used to store group of characters.
- Example: abcd, india123 etc. String is not a primitive data type because it has methods and only class can have methods.
- Primitive data types cannot have methods. So, String is a class which is non-primitive type.
- ‘String’ is used to represent string class.
Example:
1 | String s="World"; |
Objects
- Object is an Instance of a class with states and behaviors.
characteristics of an object:
state: represents statistics (value) of an object.
behavior: represents the behavior (functionality) of an object consisting of deposit, withdraw and many others.
identification: item identification is normally applied through a unique identification. The price of the id is not visible to the outside person. however, it’s miles used internally by using the JVM to become aware of every object uniquely.
Array
- Array is non-primitive data types because it refers to the memory location.
- An array is a collection of like-typed variables which are referred to by a common name.
Syntax
1 | dataType[] arrayRefVar; |