Java Data types For Beginners To Learn Java


1. Objective

In this tutorial for Java data types, we are going to learn about the various data types in Java programming language.

Introduction Java Data Types

Java Data Types – To learn Java Language

2. Introduction to Java Data types

Any programming language can be of two types, viz, statically typed language or dynamically typed language. While the statically typed language is the type of language where every variable and keyword is already known at the compile time, the former language can receive different data types over time. To learn more about Java Features follow this link.

Java is a statically typed language, i.e. it does not allow to change the data type once declared, they cannot be changed and also Java variable and Java keyword should be assigned a data type, they are predefined. But before we discuss Java Data Types, we must refer Java Syntax and Java Classes.

There are basically two types of data types in Java-

a. Primitive Data type in Java

b. Non- Primitive Data type in Java

Primitive and Non Primitive java Data Type

Java Data Types – Primitive and Non Primitive Data Type

a. Primitive data type in Java

Java Primitive data types are the simple type, i.e. they don’t have any special capabilities and are only single values.

There are 8 primitive data types in Java, they are-

i. Java boolean

In a java boolean data type, only one bit of information can be represented, true or false. They cannot be changed either explicitly or implicitly, but the programs for conversion can be easily written.

Example-

// A Java program to demonstrate boolean data type

class boolean
{
public static void main(String args[])
{
boolean b = true;
if (b == true)
System.out.println("true");
}
}

ii. Java byte

Byte data type in java is useful in saving memory for large arrays, it is a signed two’s complement integer. It has a size of 8 bit with a range from -128 to 127.

Example-

// Java program to demonstrate byte data type in Java

class byte
{
public static void main(String args[])
{
byte a = 126;
System.out.println(a);
}
}

iii. Java char

A char data type in java is also a single character with a memory of 16 Bit, unicode character. It has a range from ‘\u0000’ (or 0) to ‘\uffff’ 65535.

Example-

// Java program to demonstrate char data type in Java

class short
{
public static void main(String args[])
{
char a = ‘G’;
System.out.println(a);
}
}

iv. Java short

The short data type in Java is similar to byte and is used to save memory in large arrays. It is also a two’s compliment with a bit size of 16 bit and a range of -32,768 to 32,767 (inclusive).

Example-

// Java program to demonstrate short data type in Java

class short
{
public static void main(String args[])
{
short a = 56;
System.out.println(a);
}
}

v. Java int

Int data type in Java is two’s compliment with a memory allocation of 32 bits and range of-2^31 to 2^31-1 .

Example-

// Java program to demonstrate int data type in Java

class int
{
public static void main(String args[])
{
int a = 56;
System.out.println(a);
}
}

vi. Java long

The long data type in Java is two’s compliment with a memory allocation of 64 bit and a range of -2^63 to 2^63-1.

Example-

// Java program to demonstrate long data type in Java

class long
{
public static void main(String args[])
{
long a = 100000L;
System.out.println(a);
}
}

vii. Java float

Float Java data type is single precision IEEE floating point with a memory allocation of 32 bit and uses a suffix as F/f.

Example-

// Java program to demonstrate float data type in Java

class float
{
public static void main(String args[])
{
float a = 4.5541132f;
System.out.println(a);
}
}

viii. Java double

The double Java data type is a default choice when, it comes to decimal as it is a double precision 64 bit IEEE floating point variable.

Example-

// Java program to demonstrate double data type in Java

class double
{
public static void main(String args[])
{
double a = 1.33526252726;
System.out.println(a);
}
}

b. Non Primitive Data Types

The non-primitive data types in Java are created by the programmer during the coding process, they are known as the “reference variables” or “object variables” as they refer to a location where data is stored.

i. String

Strings are basically a collection of characters, they cannot be changed once they are created.

ii. Array and object

An array is a group of variables that are similar in nature, they are dynamically allocated and can contain both primitive or objects depending on the definition. Their size has to be specified in the only int, and their length can be found out by member length. Their indexing always starts with zero, and they are ordered.

They have methods, which a primitive data type cannot have. Further details will be given in the upcoming tutorials. To learn Java Operators and Java Modifiers follow this link.

3. Conclusion

In this Java Data types tutorial, we learned about the primitive and non-primitive data types in Java and in the upcoming tutorials, we will study more about the non-primitive type.

Best Java Books for Learn java language.

For reference