Java is statically typed and also a strongly typed language because, in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the Java data types.
Data Types in Java
Data types in Java are of different sizes and values that can be stored in the variable that is made as per convenience and circumstances to cover up all test cases. Java has two categories in which data types are segregated:
- Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double
- Non-Primitive Data Type or Object Data type: such as String, Array, etc.
Primitive Data Types in Java:
Primitive data are only single values and have no special capabilities. There are 8 primitive data types.
- boolean
- byte
- char
- short
- int
- long
- float
- double
Let us discuss and implement each one of the following data types that are as follows:
1. Boolean Data Type: The boolean data type represents a logical value that can be either true or false. the size of the Boolean data type is virtual machine-dependent and is typically one byte (eight bits) in practice.
- Syntax: boolean booleanVar;
2. Byte Data Type: The byte data type is an 8-bit signed two’s complement integer.
- Syntax: byte byteVar;
- Size: 1 byte (8 bits)
3. Short Data Type: The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
- Syntax: short shortVar;
- Size: 2 bytes (16 bits)
4. Integer Data Type: It is a 32-bit signed two’s complement integer.
- Syntax: int intVar;
- Size: 4 bytes ( 32 bits)
5. Long Data Type: The range of a long is quite large. The long data type is a 64-bit two’s complement integer and is useful for those occasions where an int type is not large enough to hold the desired value. The size of the Long Datatype is 8 bytes (64 bits).
- Syntax: long longVar;
- Size: 8 bytes ( 64 bits)
6. Float Data Type: The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits).
- Syntax: float floatVar;
- Size: 4 bytes ( 32 bits)
7. Double Data Type: The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. The size of the double data type is 8 bytes or 64 bits.
- Syntax: double doubleVar;
- Size: 4 bytes ( 32 bits)
8. Char Data Type: The char data type is a single 16-bit Unicode character with the size of 2 bytes (16 bits).
- Syntax: char charVar;
- Size: 2 bytes (16 bits)
Example:
// Java Program to Demonstrate Primitive Data Type public class PrimitiveDataType { // Main driver method public static void main(String[] args) { // Creating and initializing boolean data type boolean flag = true; // Creating and initializing custom character char a = 'A'; // Integer data type is generally used for numeric values int i = 89; // Use byte and short if memory is a constraint byte b = 4; // this will give error as number is larger than byte range // byte b1 = 7888888955; short s = 56; // this will give error as number is larger than short range // short s1 = 87878787878; // by default fraction value is double in java double d = 4.355453532; // for float use 'f' as suffix as standard float f = 4.7333434f; // need to hold big range of numbers then we need this data type long l = 12121; System.out.println("boolean: " + flag); System.out.println("char: " + a); System.out.println("integer: " + i); System.out.println("byte: " + b); System.out.println("short: " + s); System.out.println("float: " + f); System.out.println("double: " + d); System.out.println("long: " + l); } }
Syntax:Declaring a string:<String_Type> <string_variable> = “<sequence_of_string>”;Example:// Declare String without using new operatorString s = "JavaForTesters";// Declare String using new operatorString s1 = new String("JavaForTesters");
- Modifiers: A class can be public or has default access.
- Class name: The name should begin with an initial letter (capitalized by convention).
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
- Body: The class body is surrounded by braces, { }.
- State: It is represented by the attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
- In Java, all arrays are dynamically allocated.
- A Java array variable can also be declared like other variables with [] after the data type.
- The variables in the array are ordered and each has an index beginning with 0.
- Java array can also be used as a static field, a local variable, or a method parameter.
- The size of an array must be specified by an int value and not long or short.
- The direct superclass of an array type is Object.
- Every array type implements the interfaces Cloneable and java.io.Serializable.
Difference between the primitive and object data types in Java:
Now let’s look at a program that demonstrates the difference between the primitive and object data types in Java.
import java.util.Arrays; class NonPrimitiveDataType { public static void main(String ar[]) { System.out.println("PRIMITIVE DATA TYPES\n"); int x = 10; int y = x; System.out.print("Initially: "); System.out.println("x = " + x + ", y = " + y); // Here the change in the value of y, will not affect the value of x y = 30; System.out.print("After changing y to 30: "); System.out.println("x = " + x + ", y = " + y); System.out.println("**Only value of y is affected here " + "because of Primitive Data Type\n"); System.out.println("REFERENCE DATA TYPES\n"); int[] c = { 10, 20, 30, 40 }; // Here complete reference of c is copied to d, and both point to same memory in Heap int[] d = c; System.out.println("Initially"); System.out.println("Array c: " + Arrays.toString(c)); System.out.println("Array d: " + Arrays.toString(d)); // Modifying the value at, index 1 to 50 in array d System.out.println("\nModifying the value at " + "index 1 to 50 in array d\n"); d[1] = 50; System.out.println("After modification"); System.out.println("Array c: " + Arrays.toString(c)); System.out.println("Array d: " + Arrays.toString(d)); System.out.println("**Here value of c[1] is also affected " + "because of Reference Data Type\n"); } }Output:
PRIMITIVE DATA TYPES Initially: x = 10, y = 10 After changing y to 30: x = 10, y = 30 **Only value of y is affected here because of Primitive Data Type REFERENCE DATA TYPES Initially Array c: [10, 20, 30, 40] Array d: [10, 20, 30, 40] Modifying the value at index 1 to 50 in array d After modification Array c: [10, 50, 30, 40] Array d: [10, 50, 30, 40] **Here value of c[1] is also affected because of Reference Data Type
public class Test { public static void main(String[] args) { int a = 20; } }
- Test: class name.
- main: method name.
- String: predefined class name.
- args: variable name.
- a: variable name.
No comments:
Post a Comment