Core Java -DatatypesVariables:Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two data types available in Java: Primitive Data Types:There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.byte: short: int: long: float: double: boolean: char: Non-Primitive Data Types:Unlike primitive data types, these are not predefined. These are user-defined data types created by programmers. These data types are used to store multiple values. For example, consider an array that stores a group of values.Class is also a primitive type that stores different methods and variables. Therefore, these are also called as advanced data types in Java. Whenever a non-primitive data type is defined, it refers a memory location where the data is stored in heap memory i.e., it refers to the memory location where an object is placed. Therefore, a non-primitive data type variable is also called referenced data type or simply object reference variable. An object reference variable lives on the stack memory and the object to which it points always lives on the heap memory. The stack holds a pointer to the object on the heap.In Java programming, all non-primitive data types are simply called objects that are created by instantiating a class. Types of Non-primitive data types: Types of non-primitive data types in Java are as follows: String: A string represents a sequence of characters.String is the class of Java. One of the ways to create a string and store a value in it is shown below: String str = "You're the best";Eg: public class StringExample { public static void main(String[] args) { // creating a string and initializing it String str = "Hello! This is example of String type"; // applying substring() on above string String subStr = str.substring(0,14); // printing the string System.out.println(subStr); } }Output: Hello! This is Array: An array is a data type which can store multiple homogenous variables i.e., variables of same type in a sequence. They are stored in an indexed manner starting with index 0. The variables can be either primitive or non-primitive data types. Below example shows how to declare array of primitive data type int: int [ ] marksEg: import java.io. * ; import java.util. * ; public class ArrayExample { public static void main(String[] args) throws IOException { int i; Scanner sc = new Scanner(System. in ); // declaring and initializing an array int arr[] = {1, 2, 3, 6, 9}; // defining another array arr1 int arr1[] = new int[5]; // reading values from the user System.out.println("Enter the numbers (size = 5) :"); for (i = 0; i < 5; i++) { arr1[i] = sc.nextInt(); } System.out.println("Previous array with initialized size is: "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println("\nThe new array we have entered is:"); for (i = 0; i < 5; i++) { System.out.print(arr1[i] + " "); } } }Output: Enter the numbers (size = 5) : 56 43 22 1 7 Previous array with initialized size is: 1 2 3 6 9 The new array we have entered is: 56 43 22 1 7 ☛ Join to Learn from Experts: Java / J2EE Training in Chennai by TesDBAcademy
« Previous
Next Topic »
(Core Java - Constraints)
|