Primitive and non-Primitive Data types in Java
Data Type: Java Data types of data which is used in the java program. There are many predefined data types in java library like character data type, integer data type and floating data type.
Data Types specify the type of data that cab be stored inside a variable in java. Java Language is a statically –typed language. This means that all variables must be declared before they can be used data types.
int data |
Primitive Data Types in java
Primitive Data Type: Primitive data type which is predefined in java library is called primitive data type. It is named by a keyword integer, float or character data type. Primitive data types are 8 types in java. Byte, Short, int, long, float double boolean and char.
int: This data type is used to represent integer values. It can hold values from -2147483648 to 2147483647.
2. long: This data type is used to represent long integer values. It can hold values from -9223372036854775808 to 9223372036854775807.
3. short: This data type is used to represent short integer values. It can hold values from -32768 to 32767.
4. byte: This data type is used to represent byte values. It can hold values from -128 to 127.
5. float: This data type is used to represent single-precision floating-point values. It is generally used to represent decimal values.
6. double: This data type is used to represent double-precision floating-point values. It is generally used to represent decimal values with higher precision.
7. boolean: This data type is used to represent boolean values (true or false).
8. char: This data type is used to represent a single character.
Integer Data Type in Java
Data Type | Size in bytes | Range |
---|---|---|
byte | 1 | -128 To 127 |
short | 2 | -32768 To 32767 |
int | 4 | -2147483648 To 2147483648 |
long | 8 | -9223372036854775808 |
Float Data Type in Java
Data Type | Size in bytes | Range |
---|---|---|
float | 4 | -3.4e-038 To +3.4e-038 |
double | 8 | 1.7e-308 To 1.7e+038 |
Boolean Data Type in Java
Keyword | boolean |
Syntax | boolean a; |
Value | True and False |
Default | False |
Character Data Type in Java
Keyword | char |
Syntax | char a; |
Value | 'a','@','9' |
Default | 0 To 65536 |
Boolean Data Type in Java
class Bool { public static void main(String args[]) boolean data=true; System.out.println(data); } } **********OUTPUT********** true |
When we use Byte Data Type in Java
class Bytesd { public static void main(String args[]) byte data; data =430; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:430 |
Short Data Type in Java Example
class Bytesd { public static void main(String args[]) byte data; data =430; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:430 |
Int Data Type Range in Java
class Integerdata { public static void main(String args[]) int data=-564536; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:-564536 |
Long Data Type in Java Example
class Longdata { public static void main(String args[]) long int data=6564536; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:6564536 |
Double Data Type in Java Example
class Doubledata { public static void main(String args[]) double float data=656.45d; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:656.45 |
Float Data Type in Java Example
class Floatdata { public static void main(String args[]) float data=65.45f; System.out.println(“Value of Data:”+data); } } **********OUTPUT********** Value of Data:65.45 |
Character Data Type Example
class Chardata { public static void main(String args[]) char letter='R'; System.out.println(“Value of Data:”+letter); } } **********OUTPUT********** Value of Data:R |
Non-Primitive Data Types in Java
Non-Primitive Data Type: A data type which is derived from a primitive data type is called a non-primitive data type. It is also called reference data type in java. derived data types array, class interface and more.
Reference data types
String: This data type is used to represent a sequence of characters. It is not a primitive data type, but it is treated as a special data type in Java.
Class: This data type is used to represent a user-defined class.
Array: This data type is used to represent a collection of elements of the same data type.
Interface: This data type is used to define a set of related methods that a class can implement.
In addition to these data types, Java also provides support for type casting, which allows you to convert a value from one data type to another. For example, you can cast a float value to an int value by using the (int) type cast operator.
What is constant in java
Constant: Constant is an element of java program which values can not be changed at a time of execution of java program is called constant. constant is int, float and character data types.what is an integer constant write integer forming rule of java
2. integer constant is used to must not have a decimal point number.
3. integer constant is used to positive or negative values.
4. integer constant can not, no comma or blank space are allowed in integer constant.
0 Comments