Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java data type


May 10, 2021 Java


Table of contents


Java Data Type Tutorial - Java Data Type


The following two lines of Java code define two integers: num1 and num2:

int num1;
int num2;

num1 and num2 are two int variables.

The int keyword represents a name that represents an integer value, such as 10, 15, 70, 1000, and so on.

Since you have declared the num1 and num2 variables of the num data type, we cannot store a real number, such as 10.1.

The following code stores 5 in num1 and 7 in num2:

num1 = 5;
num2 = 7;

Two data types

Java supports two data types:

  • The original data type
  • Refers to the data type

Variables of the base data type maintain a value, while variables that reference the data type maintain references to objects in memory.

String is a class defined in the Java library that we can use to handle character sequences.

You declare the reference variable str of the String type as

String str;

There is a reference constant null be assigned to any reference variable.

If null is assigned to a reference variable, it means that the reference variable does not refer to any object in memory.

Empty reference text can be assigned to str.

str = null;

Use the new operator to create a String object.

Strings are often used, and there is a shortcut to creating a string object.

All string text, a sequence of characters enclosed in double quotes, is considered a String object.

We can use string text as follows:

// Assigns "Hello" to str1
String str1 = "Hello";

// Assigns the   reference of  a  String object with  text  "Hello" to str1
String str1 = new String ("Hello");