Fixed Sidebar (true/false)

Primitive and Non Primitive Data Type In Java, Geeks for Geeks in 2021

Primitive and Non Primitive Data Type In Java

We will discuss all types of data types in java from basic to advanced level. That is

Do you know how many data types there are in java? Basically there are two major data types in java. Those are Primitive and Non Primitive data types. 

if you want to know about difference between python and java then you can click here.

and you are curious to know about java jdk installation you can click here

Primitive and Non Primitive Data Type In Java

 

In order to talk in detail there are two two types of languages in java.

Following are two types of languages in java.

1.     Dynamically typed languages

2.     Statically typed language

Dynamically typed language

Dynamic typed languages receive different data types as required.

Examples are python, ruby.

Statically typed language

When a variable is declared of one type then it remains of that data type only. It can not be converted into other data types.

When we compile program compiler already knows every variable and expression data type

Examples are java, c++, c

 

In java each data type is predefined and all variables and constants for programme are already described with any one data type therefore java is statically typed language.

 

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 data types.

Data Types in Java:

Following are the Data Types in Java:

Boolean data type

Boolean data type values are not converted in to any other type implicitly or explicitly. The size of this data type is depend on virtual machine. Either True or False is the only one bit of information Boolean data types represent.

syntax

boolean var;

size

Depend on virtual machine

Value

True, false

Default value

false

Byte

If you want to save memory in large arrays then byte data type is useful for it. This type of data type is 8 bit tow’s complement integer. Byte is 4 times smaller than an integer therefore It saves space. It is also placed in int data type.

 

syntax

byte var;

size

1 byte(8 bit)

Value

-128 to 127

Default value

0

 

Shorts

Short data type is very similar to byte data type. If you want to save memory in large arrays short data is very useful. It is used in that situation when memory saving really matters. The short is a data type 16 bit signed two’s complement integer.

 

syntax

short var;

size

2 byte (16 bit)

Value

-32,768 to 32,768

Default value

0

Int:

The Int is a very important type of data type. Int is a 32 bit signed two’s complement integer. If there is no problem about memory then int data type is mostly used for integer values.

syntax

int var;

size

4 byte(32 bit)

Value

-2,147,483,648to 2,147,483,647

Default value

0

 

long:

The long data type is two’s complement integer which is 64 bit in character. When you need a range of values than provided by int The long data type is used.

syntax

long var;

size

8 byte(64 bits)

Value

-9,223,372,036,854,775,808 to 
9,223,372,036,854,775,807

Default value

0

 

float:

If you want to save memory in large arrays of floating point numbers then float data type is used instead of double data type. The float data type is a single precision 32 bit IEEE 754 floating point.

 

syntax

float var;

size

4 byte(32 bits)

Value

upto 7 decimal digits

Default value

0.0


double:

This data type is generally the default choice For decimal values. The double data type is a double-precision 64-bit IEEE 754 floating-point. The double data type should not be used for precise values, such as currency.

syntax

double var;

size

8 byte(64 bits)

Value

upto 16 decimal digits

Default value

0.0

 

char:

 The char data type is a single 16-bit Unicode character. The char data type is used to store characters.

 

syntax

char var;

size

2 byte ( 16 bits )

Value

'\u0000' (0) to '\uffff' (65535)

Default value

'\u0000'

 

Primitive and Non Primitive Data Type In Java
 

Non-Primitive Data Type

The Non Primetive Data types are also called as Reference Data Types. The reference types won’t store the variable value directly in memory therefore reference data type will contain a memory address of variable value because. They are     strings,       objects,       arrays,etc. 
 

String:

the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char type entities  this is the basic  difference between a character array and a string in Java is. Strings are  an array of characters.

Below is the basic syntax for declaring a string in Java programming language.

Unlike C/C++, Java strings are not terminated with a null character.
 

syntax:

String_Type variable = “sequence of string”;
For example:
String a = “this is java data types”;
 

Object:

 A typical Java program creates many objects, which as you know, interact by invoking methods. It is a basic unit of Object-Oriented Programming and represents the real-life entities.   An object consists of : 

a.State: It is represented by attributes of an object. It  reflects the properties of an object.

b. Identity: It gives a unique name to an object and enables one object to interact with other objects.

c.Behavior: It is represented by methods of an object. It gives an idea of  response of an object with other objects.

Class:

A class represents the set of properties or methods that are common to all objects of one type .A class is a user-defined blueprint or prototype from which objects are created.  In general, class declarations can include these components, in order: 

·        Modifiers: A class can be public or has default access .

·        Class name: The name should begin with a initial letter.

·        Superclass(if any): The name of the superclass (class’s parent), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.

·        Interfaces(if any): It is separated by comma 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 surrounded by braces, the body of the class is written in   

{ }.

 

Interface: Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, nobody).   

1.     An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.

2.     Interfaces specify what a class must do and not how. It is the blueprint of the class.

3.     A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

4.     If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.

 

Array:

Arrays in Java work differently than they do in C/C++. An array is a group of like-typed variables that are referred to by a common name The following are some important points about Java arrays. 

1)    In Java, all arrays are dynamically allocated. (discussed below) 
 

2)    Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using size.

3)    The variables in the array are ordered and each has an index beginning from 0.

4)    A Java array variable can also be declared like other variables with [] after the data type.

5)    Java array can be also be used as a static field, a local variable or a method parameter.

6)    The size of an array must be specified by an int value and not long or short.

7)    Every array type implements the interfaces Cloneable and java.io.Serializable.

8)    The direct superclass of an array type is Object. 
 


Conclusion:

In this way we have learned what are the data types in java and what is the purpose of that data type. We have also learned her that what are subparts of that data types.

I hope you all understood it very well. If you have any doubts related to data types in java then you can ask in the comment section. You want any other information related to data types then you can also ask it in comment section.

Post a Comment

2 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

if you want any other information please let me know.