Variables, Data Types and Literals in Java Programming

 

Java Tutorial: Variables and Data Types in Java Programming

Just like we have some rules that we follow to speak English (the grammar), we have some rules to follow while writing a Java program. This set of these rules is called syntax. It’s like Vocabulary and Grammar of Java.

Variables

  • A variable is a container that stores a value.
  • This value can be changed during the execution of the program.
  • Example: int number = 8; (Here, int is a data type, the number is the variable name, and 8 is the value it contains/stores).
Rules for declaring a variable name

We can choose a name while declaring a Java variable if the following rules are followed:

  • Must not begin with a digit. (E.g., 1arr is an invalid variable)
  • Name is case sensitive. (Hemant and hemant are different)
  • Should not be a keyword (like Void).
  • White space is not allowed. (int Code With Hemant is invalid)
  • Can contain alphabets, $character, _character, and digits if the other conditions are met.

Data Types

Data types in Java fall under the following categories

  1. Primitive Data Types (Intrinsic)
  2. Non-Primitive Data Types (Derived)

Primitive Data Types

Java is statically typed, i.e., variables must be declared before use. Java supports 8 primitive data types:

Data TypeSizeValue Range
1. Byte1 byte-128 to 127
2. short1 byte-32,768 to 32,767
3. int2 byte-2,147,483,648 to 2,147,483,647
4. float4 byte3.40282347 x 1038 to 1.40239846 x 10-45
5. long8 byte-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
6. double8 byte1.7976931348623157 x 10308, 4.9406564584124654 x 10-324
7. char2 byte0 to 65,535
8. booleanDepends on JVMTrue or False

 

Quick Quiz: Write a Java program to add three numbers,





How to choose data types for our variables

In order to choose the data type, we first need to find the type of data we want to store. After that, we need to analyze the min & max value we might use.




Java Tutorial: Literals in Java

Literals

A constant value that can be assigned to the variable is called a literal.

  • 101 – Integer literal
  • 10.1f – float literal
  • 10.1 – double literal (default type for decimals)
  • ‘A’ – character literal
  • true – Boolean literal
  • “Hemant” – String literal

Keywords

Words that are reserved and used by the Java compiler. They cannot be used as an Identifier.

{You can visit docs.oracle.com for a comprehensive list}

Code as Described in the Video

package com.company;

public class CWH_04_literals {
    public static void main(String[] args) {
        byte age = 34;
        int age2 = 56;
        short age3 = 87;
        long ageDino = 5666666666666L;
        char ch = 'A';
        float f1 = 5.6f;
        double d1 = 4.66;

        boolean a = true;
        System.out.print(age);
        String str = "Hemant";
        System.out.println(str);

    }
}


Handwritten Notes: Click To Download

Ultimate Java Cheatsheet: Click To Download

Previous Post Next Post