Java Data Types

Programming

Java must be a specified data type:

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

Data types are divided into two groups:

  • Primitive data types – includes byte, short, int, long, float, double, boolean and char
  • Non-primitive data types – such as String, Arrays and Classes (you will learn more about these in a later chapter)

Primitive Data Types

The size and type of variable values are specified by a primitive data type, which has no extra functions.

In Java, there are eight primitive data types:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Numbers

Primitive number types are divided into two groups:

Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.

Despite the fact that Java has multiple numeric types, the most commonly used for numbers are int (for whole numbers) and double (for floating point numbers).
However, as you read on, we’ll go through each one in detail.

Integer Types

Byte

From -128 to 127, the byte data type can hold entire values.
When you know the value will be between -128 and 127, you can use this instead of int or other integer types to conserve memory:

Example

byte myNum = 100;
System.out.println(myNum);

Short

Theshort data type can store whole numbers from -32768 to 32767:

Example

short myNum = 5000;
System.out.println(myNum);

Int

Whole numbers between -2147483648 and 2147483647 can be stored in the int data type.
When creating variables with a numeric value, the int data type is the ideal data type in general and in our course. 

Example

int myNum = 100000;
System.out.println(myNum);

Long

From -9223372036854775808 to 9223372036854775807, the long data type can store entire numbers. When int is insufficient to store the value, this is utilized. It’s important to note that the value should conclude with a “L”:

Example

long myNum = 15000000000L;
System.out.println(myNum);

Floating Point Types

When you need a decimal number, such as 9.99 or 3.14515, you should use a floating point type.


Float

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an “f”:

Example

float myNum = 5.75f;
System.out.println(myNum);

Double

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a “d”:

Example

double myNum = 19.99d;
System.out.println(myNum);

Is it better to use float or double?

The precision of a floating point value is the number of digits following the decimal point that the value can have.
The precision of float variables is just six or seven decimal digits, but the precision of double variables is around 15 digits.
As a result, for most calculations, it is safer to utilize double.

Scientific Numbers

A floating point number can also be a scientific number with an “e” to indicate the power of 10:

Example

float f1 = 35e3f;
double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);

Booleans

A boolean data type is declared with the boolean keyword and can only take the values true or false:

Example

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);     // Outputs true
System.out.println(isFishTasty);   // Outputs false

Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:

Example

char myGrade = 'B';
System.out.println(myGrade);

Alternatively, you can use ASCII values to display certain characters:

Example

char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1);
System.out.println(myVar2);
System.out.println(myVar3);

Strings

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:

Example

String greeting = "Hello World";
System.out.println(greeting);

Because the String type is so widely utilized and integrated in Java, it is sometimes referred to as “the special ninth type.”

Because it relates to an object, a String in Java is a non-primitive data type. Methods on the String object are used to execute various operations on strings.

Non-Primitive Data Types

Because they refer to things, non-primitive data types are termed reference types.

The following are the fundamental distinctions between primitive and non-primitive data types:

  • In Java, primitive types are predefined (that is, they have already been declared).
    Java does not specify non-primitive types, which are constructed by the programmer (except for String).
  • Non-primitive types, on the other hand, can be used to call methods that perform specific actions, whereas primitive types cannot.
  • Non-primitive types can be null, whereas primitive types always have a value.
  • A lowercase letter begins a primitive type, while an uppercase letter begins a non-primitive type.
  • A primitive type’s size is determined by the data type, whereas non-primitive types are all the same size.

Click here to read more useful and interesting articles.



 
X-Soft

X-Soft is an established player in the IT market committed to providing excellent solutions for Web/ Mobile (iOS, Android, Web) around the globe. Namely, we are a team of professional web and mobile developers with 10+ years of experience.

Leave a Reply

Leave a Reply

Your email address will not be published.