Data Types


Data Types

Java data types are classified as specifiers that assign various sizes and types of values that can be stored in an identifier or variable. There is a rich collection of data types in Java. There are 2 main data types: Primitive and Non-Primitive/Reference.

Primitive Data Type

In the Java language, the building blocks of data manipulation are primitive data types. These are the most basic Java language data types available. Primitive Data Types are predefined in the Java language and are available. It would be difficult to frame programs without primitive data types. Primitive data types are as follows:

  • Byte
  • Int
  • Short
  • Long
  • Float
  • Double
  • Boolean
  • Char

Non-Primitive Data Type

A non-primitive type of data is defined as the data type that is derived from primary data types. For the storage of a set of values, non-primitive data types are used. Non Primitive data types are as follows:

Classes

class A template that defines the data and actions associated with instances of that class is the basic building block of an object-oriented language. You produce an object when you instantiate a class that looks and feels like other instances of the same class. To build multiple objects, a class can be used. In variables, the data associated with a class or object is stored.

Syntax:

class extends implements interface_name
{
//fields;
//constructors;
//methods;
//blocks;
}

Example:

We want to build a library. The library here is an object. To build a library we require a template for the library. We cannot go about directly building the library This is where class is used. Therefore, to build an object or real-life entity, we will first have a template that determines the contents and behavior of an object. This is called as class in OOP.

Public class Library
{
//all the methods,
//fields,
//etc
}

Interfaces

The interface tends to be a class, but it is not a class. Like a class, an interface may have methods and variables, but the methods declared in the interface are abstract by default. Also, by default, the variables declared in an interface are public, static & final. All methods of that interface must be implemented by the class which implements the interface. The Java programming language also does not allow you to extend more than one class, but in your class you can implement more than one interface.

Syntax:

Interface declaration starts with the word interface.

Interface iExample
{
public void a();
public void b();
}

Arrays

An array is a container object that contains a fixed number of single-type values. When the array is generated, the length of the array is determined. Its length is fixed after development. An array refers to a structure of data which includes homogeneous elements. This implies that the data form of all the elements in the array is the same. The data items that are positioned in the array are referred to as elements, and the first element in the array begins with index zero. The object class is inherited by arrays. In an array, we can store primitive values or objects.

Syntax:

elementType arrayName[ ];

OR

elementType[ ] arrayName;

Example:

int a[ ];

OR

int[ ] a;

Strings

A string is a character set, but it is not a primitive form. When we create a Java string, it creates an object of type String. The string is an immutable object, meaning that once it is generated, it can not be modified. The only class where operator overloading is supported in Java is String. There are many methods available which can be used and are discussed in later topics in detail.

Syntax:

String variableName=value;

Example:

String s1 = "abc";
String s2 = "def";

Example:

com.knowledge2life Public class Main { public static void main(String[] args) { //Int int myNumi = 100000; System.out.println(myNumi); //Long long myNuml = 15000000000L; System.out.println(myNuml); //Floating float myNumf = 5.75f; System.out.println(myNumf); //Double double myNumd = 19.99d; System.out.println(myNumd); //Booleans boolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); System.out.println(isFishTasty); //Characters char myGrade = 'K'; System.out.println(myGrade); //Strings String greeting = "KNowledge2life"; System.out.println(greeting); } }

OUTPUT:

100000
15000000000
5.75
19.99
true
false
K
KNowledge2life