Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Primitive Data Types: A Clear Guide

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

There are two data types available in Java −

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.

byte

short

int

long

float

double

boolean

char

Reference Datatypes

Java Literals

A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.

Literals can be assigned to any primitive type variable. For example −

byte a = 68;
char a = 'A';

byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.

Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For example −

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are −

Example

"Hello World"
"two\nlines"
"\"This is in quotes\""

String and char types of literals can contain any Unicode characters. For example −

char a = '\u0001';
String a = "\u0001";

Java language supports few special escape sequences for String and char literals as well. They are −

Notation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character (xxxx)

What is Next?

This chapter explained the various data types. The next topic explains different variable types and their usage. This will give you a good understanding on how they can be used in the Java classes, interfaces, etc.


Java

  1. Java Primitive Data Types: A Complete Guide with Examples
  2. Mastering Java Encapsulation: A Practical Guide to Data Hiding
  3. Mastering Java Type Casting: From Widening to Narrowing and Beyond
  4. C Variables, Data Types, and Constants – A Practical Guide
  5. Java Fundamentals: Mastering Basic Syntax
  6. Java Essentials: A Comprehensive Guide to Core Operators
  7. Java Data Structures: Exploring Core Classes & Collections Framework
  8. Mastering Java Generics: Simplify Sorting and Enhance Type Safety
  9. Java 10 Feature Spotlight: Mastering Local Variable Type Inference
  10. Comprehensive Guide to C# Data Types: Value, Reference, and Pointer