data types in java

By | April 6, 2019

Data types in java-:  A lot of Java code was written but when we are doing designing of classes, only the most simple data types are used, a few numeric ones and text. In the JDK  a lot of data types are declared for a multitude of purposes: for modelling calendar dates, for representing multiple types of numeric, for manipulating texts, collections, files, database connections, and so forth. In JDK, there are libraries created by other parties that provide even more functionality. But the data types provided by the JDK are fundamental ones, the bricks every Java application is built from. Of course, depending on the type of application we are building, we might not need all of them.

Data types In java -: Java types can be split in two main categories:

  • primitive
  •  reference types.

 

Java code files are stored on the HDD, Java byte-code files as well. Java programs run on the JVM, which is launched as a process by executing the java executable. During execution all data is stored in two different types of memory named: stack and heap that are allocated for a program’s execution by the operating system.

The stack memory -: It is used during execution( also referred to as at run-time) to store method primitive local variables and references to objects stored in the heap. A stack is also a data-structure represented by a list of values that can only be accessed at one end, also called a LIFO order, which is an acronym for Last In, First Out . The name fits, because every time a method gets called, a new block is created in the stack memory to hold local variables of the method.

The heap memory-: It is used at run-time to allocate memory for objects and JRE classes. Objects are instances of JDK classes or developer defined classes. Any object created with new is stored inside the heap memory. Objects created inside the heap memory can be accessed by all threads of the application.The heap size may vary, depending on the number of objects created by the program, and if all heap memory allocated to a Java program is full, then a java.lang.OutOfMemoryError is thrown.

 

Primitive Data types -: These are defined by the Java programming language as special types that do not have a supporting class and Its named by their reserved keyword. Variables of these types are saved on the stack memory and when values are assigned to them using the = (equals) operator , the value is actually copied. So, if we declare two primitive variables of type int, as in the following code listing, we end up with two variables, k and q, both having the same value: 42.

public class PrimitivesDemo {
    public static void main(String... args) {
        int k = 42;
        int q = k;
        System.out.println("k = " + k);
        System.out.println("q = " + q);
    }
}

 

When passed as arguments to other methods, the values of primitive values are copied and used without the initial variables being modified. This can be proved by creating a method to swap the values of two int variables. The another example has been given here..

public class PrimitivesDemo {
public static void main(String... args) {
                            int k = 42;
                            int q = 44;
                            swap(k, q);
                            System.out.println("k = " + k);
                            System.out.println("q = " + q);
                                        }
static void swap(int a, int b) {
                               int temp = a;
                               a = b;
                               b = temp;
                               }
                            }

 

Primitive data types  are divided in some parts as like

  • Boolean value-:Variables of this type can have only one of the two accepted values: true and false. This type of variable is used in conditions to decide a course of action. The values true and false are themselves reserved keywords. Default value for a Boolean variable is false.
  • The char type -:  It represents characters. The values are 16-bit unsigned integers representing UTF-16 code units. The interval of the possible values for char variables is : from ‘\u0000’ to ‘\uffff’ inclusive, as numbers this means: from 0 to 65535. This means that we can try to print the full set of values. As the representation of the characters is numeric, this means we can convert int values from interval to char values.
  • Integer Primitive-: We mostly used variables of type int but there is more than one numeric primitive type in Java. Java defines six primitive numeric types  and each of them has a specific internal representation, on a certain number of bits, which means that there is a minimum and a maximum value. There are four numeric types to represent integer values and two numeric types to represent real numbers.
  • Real Primitive-:  Real numbers contain a decimal point and decimals after it. To represent real numbers in Java, two primitive types (called floating-point types ) are defined: float and double.Float represents single-precision 32-bit format IEEE 754 values as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).Double represents single-precision 64-bit format IEEE 754 values as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York)and represents numbers between 4.9E −324 and 2127 * 10308.

 

 

 Reference types in Java-: Reference types are different from primitive types as these types are instantiable (except interfaces). Objects of these types are created by calling constructors and variables of these types are references to objects stored in the heap. Because the references are stored on the stack as well, even if we modify the previous code to use references the behaviour is the same.This is the four type..

  1.  Class Types
  2. Interface Types
  3. Enums
  4. Array Types

 

Array-: An array is a data structure that holds a group of variables together. Its size is defined when it is created, and it cannot be changed.

Each variable can be accessed using an index that starts at 0 and goes up to the length of the array to –1. Arrays can hold primitive and reference values.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *