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

Java Variables and Literals: A Comprehensive Guide

Java Variables and Literals

This guide explores Java variables and literals, illustrated with clear examples for practical understanding.

Java Variables

A variable is a designated memory location that stores data.

Each variable must have a unique identifier to reference the storage area. Learn more about Java identifiers.


Creating Variables in Java

Here’s how to declare and initialize a variable:

int speedLimit = 80;

In this example, speedLimit is an int holding the value 80. The int type restricts the variable to integer values. For deeper insight, visit Java data types.

Variable declaration and initialization can be performed separately:

int speedLimit;
speedLimit = 80;

Note: Java is statically typed; all variables must be declared before use.


Modifying Variable Values

Variables can change during program execution. For instance:

int speedLimit = 80;
...
speedLimit = 90;

Initially, speedLimit is 80; later it updates to 90. However, the data type cannot change within the same scope.

For example, the following is invalid:

int speedLimit = 80;
...
float speedLimit;

Learn more at: Can I change declaration type for a variable in Java?


Naming Rules for Java Variables

Java’s naming conventions enhance code readability and maintainability:


Java distinguishes four variable categories:

Explore variable types in more depth: Java Variable Types.


Java Literals

Literals are fixed values embedded directly in source code. They provide a convenient way to assign data:

int a = 1;
float b = 2.5;
char c = 'F';

Below are the primary literal categories in Java.

1. Boolean Literals

Boolean literals are limited to true or false:

boolean flag1 = false;
boolean flag2 = true;

2. Integer Literals

Integer literals represent whole numbers and come in four bases:

  1. Binary (base‑2) – prefix 0b
  2. Decimal (base‑10)
  3. Octal (base‑8) – prefix 0
  4. Hexadecimal (base‑16) – prefix 0x

Examples:

// binary
int binaryNumber = 0b10010;

// octal
int octalNumber = 027;

// decimal
int decNumber = 34;

// hexadecimal
int hexNumber = 0x2F;

Note: Integer literals initialize byte, short, int, and long variables.

3. Floating‑point Literals

Floating‑point literals include fractional or exponential notation:

class Main {
  public static void main(String[] args) {
    double myDouble = 3.4;
    float myFloat = 3.4F;
    double myDoubleScientific = 3.445e2;

    System.out.println(myDouble);            // 3.4
    System.out.println(myFloat);             // 3.4
    System.out.println(myDoubleScientific);  // 344.5
  }
}

These literals initialize float and double variables.

4. Character Literals

Characters are single Unicode symbols enclosed in single quotes:

char letter = 'a';

Escape sequences are also valid, e.g., \b, \t, \n, etc.

5. String Literals

Strings are sequences of characters enclosed in double quotes:

String str1 = "Java Programming";
String str2 = "Programiz";

Java

  1. Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
  2. C Variables, Constants, and Literals: A Complete Guide
  3. Python Variables, Constants, and Literals – A Comprehensive Guide
  4. Understanding Java: JDK, JRE, and JVM Explained
  5. Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
  6. Java Classes and Objects: A Practical Guide
  7. Java Variables and Data Types – A Comprehensive Guide with Examples
  8. Understanding Java Variable Types: A Comprehensive Guide
  9. Understanding C Constants and Literals: Types, Usage, and Best Practices
  10. Mastering C# Constants & Literals: Types, Rules, and Best Practices