Data Types in Java: Everything You Need to Know

Data Types in Java

Java is an object-oriented language very similar to C++, but with simplified and advanced features. Java is a programming language you can access for free and run on almost all the platforms. The language is concurrent that allows you to execute several statements instead of executing them sequentially. It is class-based, independent programming language following the logic of ‘Write once, run anywhere’, which means the code you compile can run on all platforms supporting Java.

Java was developed by James Gosling and his team members named, Patrick Naughton and Mike Sheridan, for sun Microsystems in 1995. It was first developed for digital devices like televisions, set-top boxes, etc.

To say in layman terminology, Java is nothing but a computing platform allowing people to develop applications.

This blog focuses on the different data types of Java, along with various other important things that you should know to have a clearer understanding of the programming language.

Data Types in Java

A data type is a valuable attribute that tells the interpreter or compiler how the programmer wants to use the variable. It is an attribute that defines the operations that can be done using the data and the type of values it can store. Based on their properties, data types of java are classified into two groups – Primitive Data Type and Non-primitive data type.

Primitive data type: It is a data type that is pre-defined by the computing language. The type and size of its variable values are fixed and require no additional methods.

Non-primitive data type: These are the data types that are not defined by the programming language exactly, but are developed bu the programmer. Non-primitive data types are also known as ‘object variables’ or ‘reference variables’, since they refer to a memory location where the data is stored.

Before moving to the different types of primitive and non-primitive data types, it is important to know about the Java variables.

Java Variables

A variable is a location in the storage area (memory) to hold data. Each variable is given a unique name to indicate the storage area. The unique name is known as ‘identifier’.

Declaring variables in Java

See the below example to declare a variable in Java:

int speedLimit = 80;

 

In the above example, ‘speedlimit’ is a variable of data type ‘int’ assigned with the value 90. This means that the speedlimit variable can store integer values. Here we have assigned the variable a value during declaration. But, it is not mandatory.

You can also declare variables without assigning the value. You can store the value as per your wish later.

Refer to the below example to get a clear idea of what we intend to say:

int speedLimit;

speedLimit = 90;

The value of a variable can be altered in the program, hence named ‘variable’.

Example:

int speedLimit = 80;

… .. …

speedLimit = 90;

Java is a statically-typed language. It means that you must declare all the variables before using them. Remember, you cannot change the data type of the specific variable within the same scope.

int speedLimit = 90;

… .. …

float speedLimit;

Rules to name variables in Java

Java programing language has got its own set of conventions and rules to name variables. Read on to know what are they:

  • The Java variables are case-sensitive
  • The name of a variable is a sequence of Unicode digits and letters. It can begin with a letter, $ or _. However, it’s a traditional thing to start a variable name with a letter.
  • Variable names cannot use whitespace in Java.
  • In case you choose one-word variable names, use lowercase for all the letters. Consider this example – it is better using “speed” rather than “sPEED” or “SPEED”.
  • While declaring a variable, select a sensible name. Names like ‘score’, ‘level’, ‘number’, etc. make more sense than choosing names like ‘s’. ‘n’ and ‘l’ for a variable.
  • On choosing a variable name that has more than one word, use lowercase letter for the first word and use uppercase for the initial letter of each following word (No whitespace).

Example: ‘speedLimit’

Let us tell you about the four types of Java variables:

  • Instance Variable or Non-static Fields
  • Local Variables
  • Class Variables or Status Fields
  • Parameters

Now that you have got a brief idea about variables in Java Language, let’s proceed to discuss the primitive and non-primitive data types of the programming language.

Primitive Data Types in Java

Primitive-data-types-in-java

Mainly, the primitive data types of Java are int, character, boolean and float. But, in general, primitive data types are of 8 types, such as:

  • boolean data type
  • integer data type
  • float data type
  • char data type
  • short data type
  • long data type
  • double data type
  • byte data type

We have discussed all the 8 data types in detail below. Keep Reading.

As we know that variables should be declared before their use, consider the example:

int speed;

Here ‘speed’ is a variable with data type ‘int’. The ‘int’ data type specifies that ‘speed’ variable can only store integers.

A variable’s data type identifies the kind of values a variable can store.

boolean

The ‘boolean’ data type has got two possible values – ‘true’ or ‘false’. The default value of this data type is ‘false’. ‘boolean’ is mostly used in situations where the answer is either true or false.

Example:

class BooleanExample {

public static void main(String[] args) {

 

boolean flag = true;

System.out.println(flag);

}

}

 

Output:

true

 

byte

‘byte’ is a data type that can have values from -128 to 127 (8-bit signed 2’s complement integer). This data type is used in place of ‘int’ or other integer data types in order to save memory. This is done in case it is known that the value of the variable is within -128 and 127. Its default value is 0.

For example:

Must Know:  Windows Update Cleanup- Top 10 Fixes to Disk Cleanup Stuck on

class ByteExample {

public static void main(String[] args) {

 

byte range;

range = 122;

System.out.println(range);

}

}

Output:

 

122

 

short

Next on the list of primitive data types is ‘short’. This is a data type that can have values from -32768 to 32767 – the 16-bit signed two’s complement integer. It is the data type used in place of various other integer data types to store memory when it is known that the variable will have the value between -32768 and 32767. Its default value is 0.

For example:

class ShortExample {

public static void main(String[] args) {

 

short temperature;

temperature = -400;

System.out.println(temperature);

}

}

On running the program, you will get the output:

-400

 

int

The int data type can have values from 0 to  -1  (32-bit signed 2’s complement integer). If you are using Java 8 or an upgraded version, you can use unsigned 32-bit integer with a minimum value of 0 and a maximum value of  – 1. The default value of an integer is 0.

For example:

class IntExample {

public static void main(String[] args) {

 

int range = -4200000;

System.out.println(range);

}

}

 

Output:

-4200000

 

long

The ‘long’ data type can have values from – to -1 (64-bit signed two’s complement integer). If you use Java 8 or an upgraded version, you can use the unsigned 64-bit integer with a minimum value of 0 and a maximum value of  – 1. The default value of this data type is also 0.

For example:

class LongExample {

public static void main(String[] args) {

 

long range = -42333300000L;

System.out.println(range);

}

}

 

Output:

-42333300000

 

You should notice the use of the letter ‘L’ at the end of -42333300000. It is a representation of the integral literal of ‘long’ type.

Note: We will explain about integer literals later in the blog.

double

The ‘double’ data type is known as a double-precision 64-bit floating-point. You should not use it for precise values like currency. The default value of double data type is 0.0 (0.0d).

For example:

class DoubleExample {

public static void main(String[] args) {

 

double number = -45.3;

System.out.println(number);

}

}

 

Output:

-45.3

 

float

The ‘float’ is a primitive data type – a single-precision 32-bit floating-point. Like ‘double’, you should not use this data type for precise values like currency. The default value of ‘float’ is 0.0 (0.0f)

For example:

class FloatExample {

public static void main(String[] args) {

 

float number = -44.2f;

System.out.println(number);

}

}

 

Output:

-44.2

 

In the above example, we have used the letter ‘f’ with the value ‘-44.2’ as it is a ‘double’ literal. It was to let the compiler know that it should treat the value ’44.2’ as ‘float’ and not ‘double’.

char

char is a primitive data type with a 16-bit Unicode character. The minimum value of this data time is ‘\u0000’ (0) and the maximum value is ‘\uffff’. Its default value remains ‘\u0000’.

For example:

class CharExample {

public static void main(String[] args) {

 

char letter = ‘\u0051’;

System.out.println(letter);

}

}

 

Output:

Q

In the above program, you receive the output ‘Q’ because ‘\u0051’ is the Unicode value of ‘Q’.

Consider another example:

class CharExample {

public static void main(String[] args) {

 

char letter1 = ‘8’;

System.out.println(letter1);

 

char letter2 = 65;

System.out.println(letter2);

 

}

}

 

Output:

8

A

 

When you print ‘letter1’, you get ‘8’ as the result because it is assigned ‘9’ as the character. However, when you print ‘letter2’, you get ‘A’ as the result. Why so? It is because ‘65’ is the ASCII value of ‘A’. The Java compiler treats the character as an integral type.

String

Java is also known to provide support for character strings through ‘java.lang.string’ class.

Consider the below example for creating a String object in Java

myString = “Programming is awesome”;

Java literals

When understanding data types of Java, it is essential to learn about the Java literals. Consider the following example to understand literals.

Boolean flag = false;

In the above program:

‘Flag’ is a variable; ‘boolean’ is a data type and ‘false is a literal’.

A Java literal is a source source code representation of a specific value.

Values like 5, 1, 4, ‘\u0050’, true that you see appearing directly in a Java program without any need of computation are literals.

Referring to the above example, ‘flag’ is a variable. As it is a ‘boolean’ type variable, it will only store values ‘true’ or ‘false’. To help the compiler understand it, it needs computation. However, literals like ‘a’, ‘-5’, ‘true’, etc. represent specific (fixed) value.

integer Literals

Integer literals help in initializing variables of integer data types such as ‘short’, ‘int’, ‘byte’ and ‘long’. In case an integer literal ends with ‘L’ or ‘l’, it represents ‘long’ data type. (remember the example used to explain ‘long’ data type above? Now you know the significance of ‘L’ there.).

Consider the example below:

// Error! Literal 42333300000 of type int is out of range

int myVariable1 = 42333300000;// 42333300000L is of type long, and it’s not out of range

long myVariable2 = 42333300000L;

You can express integer literals in a binary number, decimal and hexadecimal systems. The numbers which start with prefix ‘0x’ are represented as ‘hexadecimal’. And, the numbers which start with prefix ‘0b’ are represented as binary.

Example:

  • // decimal
  • int decNumber = 34;// 0x represents hexadecimal
  • int hexNumber = 0x2F;// 0b represents binary

int binNumber = 0b10010;

floating-point Literals

Floating-point literals are used in order to initialize variables belonging to data type ‘double and ‘float’. IF you come across a floating-point literal ending with ‘f’ or ‘F’, it represents the data type ‘float’, otherwise it belongs to data type ‘double’.

You can end a ‘double’ data type with ‘d’ or ‘D’, but it is not mandatory. This literal type can also be expressed in scientific notation using ‘e’ or ‘E’.

For example:

 

Class DoubleExample {

public static void main(String[] args) {

 

double myDouble = 3.4;

float myFloat = 3.4F;

 

// 3.445*10^2

double myDoubleScientific = 3.445e2;

 

System.out.println(myDouble);

System.out.println(myFloat);

System.out.println(myDoubleScientific);

}

}

 

Output:

3.4

3.4

344.5

 

character and String Literals

These Java literals contain UTF-16 (Unicode) characters. For ‘char’ literals, you can use single quotations, such as ‘a’, ‘\u0222’, etc. For ‘String’ literals, you should use double quotations, such as “Java 8”, “programming”.

Java also supports some escape sequences, such as \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \” (double quote), \’ (single quote), and \\ (backslash).

For example:

Class DoubleExample {

public static void main(String[] args) {

char myChar = ‘g’;

char newLine = ‘\n’;

String myString = “Java 8”;

System.out.println(myChar);

System.out.println(newLine);

System.out.println(myString);

}

}

Output:

g

 

 

Java 8

 

Non-primitive data types

Non-primitive data types of Java refer to objects and so-called ‘’reference types. There are four types of non-primitive data types in Java programming language – Strings, Classes, Arrays and Interface.

Must Know:  10 Amazing Gadgets for the Tech-Lover on Your List

non-primitive-data-types-in-java

So, what are these non-primitive data types? Read on.

Strings

A string is nothing but a sequence of characters. However, in Java, a String is an object representing a sequence of characters. To create a string object in Java, the java.land.String class is used.

Arrays

In Java, arrays are homogenous data structures that are implemented in the programming language as objects. Arrays save one or more values of a particular data type and render them indexed access to store those values. A particular element in an array is accessed by its index.

Classes

In Java, A ‘class’ is a blueprint that involves all your data. It contains variables or fields and methods to show an object’s behavior.

Interface

Similar to a class, an ‘interface’ in Java also have variables and methods. However, the methods declared in the interface are abstract by default (no body, only method signature).

Now that you know about both the types of data types in Java, why not know the basic differences between them!

Difference between non-primitive and primitive data types in Java

The difference between both the datatypes in Java are described below:

  • In Java, primitive data types are pre-defined, while non-primitive data types are declared by the programmer and not defined by Java.
  • A primitive data type always contains a value, while non-primitive types can also be null.
  • You can use non-primitive data types to call methods in order to perform some specific operations, while this is not possible with the primitive data types.
  • The size of a primitive data type can change as per the data type. However, the non-primitive types are all of the same sizes.
  • A primitive data type begins with a lowercase letter, whereas non-primitive data types begin with an uppercase letter.

Uses of Java

We thought it would be nice to have a basic idea about the programming language to be able to use it wisely. Let’s start with its uses in different fields.

Java is used in varied domains, such as:

  • In Banking, to carry out transaction management.
  • In Android devices, to develop applications, Yes, android applications are either written in Java API or Java.
  • For the Scientific and Research community, to manage a huge amount of data.
  • In Financial Services, for server-side applications.
  • For maintaining the Stock market, Java is used writing algorithms to finalize in which company to invest
  • In IT sector, Java is used to solve implementation dependencies
  • In Retail, it is used to bill applications.
  • Not just these, Java can do even more!

Features of Java

Here are some interesting features of Java that you should know:

Simple: it is a programming language that has made things easier for software technicians by removing almost all the complications such as operator overloading, pointers, etc., seen in other programming languages like C++.

Object-oriented: In Java, everything is taken as an ‘object’ which possesses some behavior, state, etc. You perform all the operations using these objects.

Portable: it is an independent computing platform that suggests that any application you write on one platform can be easily ported to a different platform.

Secured: All the code in Java is converted in bytecode after compilation of a program that a human cannot read. Java runs the programs inside the sandbox and doesn’t use an explicit pointer to prevent any activities from untrusted sources. It allows you to develop tamper-free, virus-free applications/systems.

Robust: The programming language has a strong memory management system. It allows you to eliminate errors as it checks the code during compilation and runtime.

Dynamic: Java has got the ability to adapt to a growing environment that supports dynamic memory allocation because of which the amount of memory wastage is minimized and the application’s performance is improved.

High-performance: Java attains a high-performance level by the use of bytecode which can be translated into native machine code easily. By using Just-in-Time (JIT) compilers, the programming language enables high performance.

Distributed: Java offers a feature that helps to develop distributed applications. Using RMI (Remote Method Invocation), a program can invoke a method of a different program across a network and receive the output. You can access the files by calling those from any device on the Internet (that supports Java).

Interpreted: The programming language is compiled to bytecodes that are usually interpreted by a Java run-time environment.

Multi-threaded: Java favors multiple threads of execution (all lightweight processes), including a set synchronization primitives. This way, programming with threads becomes easier.

Components of Java

Java Virtual Machine (JVM)

It is an abstract machine; a specification that offers a run-time environment in which Java bytecode can be easily executed. JVM follows three notations:

Specification: A document that describes the implementation of the JVM. It is offered by Sun and other companies.

Implementation: A program that fulfills the requirements of JVM specification.

Runtime instance: An instance of JVM is developed whenever you write a java command on the command prompt and run the class.

Java Runtime Environment (JRE)

JRE is the runtime environment where Java bytecode can be executed. It implements the JVM and offers all the class libraries and other support files that are used by JVM at runtime. So, JRE is known as a software package consisting of everything required to run a Java program. Cutting it short, it is an implementation of the Java Virtual Machine that physically exists.

Java Development Kit (JDK)

JDK or Java Development Kit is a tool essential for compiling, documenting and packaging Java programs. It includes JRE completely that consists of tools for Java programmers. The Java Development Kit is offered free of cost. Additionally, it also contains a loader/interpreter, an archiver (jar), a compiler (javac), a documentation generator (Javadoc) and various other tools required in Java development. In simpler terms, JDK is a combination of JRE and other development tools.

FAQs about Java

Do I have to pay for downloading Java?

No, Java is a programming language that is free to access.

Why do I need to upgrade to the latest version of Java?

You will find significant enhancements to improve the performance, security and stability of the Java applications running on your machine by upgrading to the latest version of the programming language. On installing the free update, you will know that your Java applications run efficiently and safely.

What do I get on downloading Java software?

On downloading Java software, you will get the Java Runtime Environment (JRE). It consists of the Java Virtual Machine, supporting Java platform libraries and Java platform core classes. The JRE is the runtime portion of Java software and that’s all you need to run it successfully in your Web browser.

What is JVM? Is it a Java software?

JVM or Java Virtual Machine is just one aspect of Java software involved in web interaction. It is built into your Java software and enables smooth running of Java applications.

What is Java plug-in software?

It is a component of JRE (Java Runtime Environment). JRE enables the applets written in the programming language to run in various browsers. The Java Plug-in software is not a standalone program and cannot be installed separately.

Conclusion

We hope this blog on data types of Java helped you understand the programming language better. If you are planning to learn application building, understanding the basics of Java can be helpful for you.

 

Leave a Reply