Identifier, Literals, Operators, Variables, Keywords, Data Types

The vocabulary of every Java line: 8 fixed-size primitive types, identifier naming rules, literal forms, operator families and the keywords you can never use as names.

11 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

One book, in Java's words

BookBridge starts by storing facts about one book: a title, a section letter, how many copies exist, the price, and whether it is on the shelf right now.

Each fact needs a variable, each variable needs a type, each type has its own literal form, and every name you invent must follow the identifier rules. This one lesson is the vocabulary the whole subject is written in; get these words right and every later listing reads easily.

Theory

The 8 primitive types

Java gives exactly 8 primitive data types, and unlike C, their sizes are fixed on every platform (that is the "portable" property in action).

Whole numbers come in 4 sizes: byte, short, int, long. Decimals come in 2: float and double. Then char for a single character and boolean for true/false. Choosing means asking one question: how big can this value get, and does it have a decimal point?

At a glance

Primitive types (sizes are exam favourites)

TypeSizeExample use in BookBridge
byte1 bytecopies on one shelf (-128 to 127)
short2 bytesbooks in one section
int4 bytestotal books, fines in rupees (the default choice)
long8 bytesISBN numbers (too big for int)
float4 bytesaverage rating (needs f suffix)
double8 bytesprice (the default for decimals)
char2 bytessection letter 'C' (Unicode)
booleanJVM-dependentavailable: true or false

Theory

Identifiers and keywords

An identifier is any name you invent: class, variable or method names. The rules:

  • may contain letters, digits, underscore _ and dollar $
  • must NOT start with a digit
  • case-sensitive: copies and Copies are different variables
  • must not be a keyword

Keywords are Java's roughly 50 reserved words (class, int, if, static...). Two oddities worth a mark: const and goto are reserved but never usable.

Practical

Book facts as variables and literals

public class Book {
    public static void main(String[] args) {
        String title = "Let Us C";      // String literal
        char section = 'C';             // char literal: single quotes
        int copies = 5;                 // int literal
        double price = 350.50;          // decimals are double by default
        float rating = 4.5f;            // f suffix makes it float
        long isbn = 9788183331630L;     // L suffix: too big for int
        boolean available = true;       // boolean literal
        System.out.println(title + " | copies: " + copies);
    }
}

Theory

Literals: how values are spelt

A literal is a value written directly in code, and its spelling announces its type:

  • 5 is an int; 9788183331630L needs the L because it exceeds int's range
  • 350.50 is a double by default; 4.5f needs the f to be a float
  • 'C' (single quotes) is a char; "C" (double quotes) is a String
  • true, false, null are literals too

Most compile errors in week 1 are literal-spelling errors: float x = 4.5; fails because 4.5 is a double.

Quiz

Which of these is a valid Java identifier?

  1. 2ndBook
  2. book-price
  3. _bookCount
  4. class
Show the answer

_bookCount

Underscore is a legal starting character, so _bookCount is fine. 2ndBook breaks the no-starting-digit rule. book-price contains a hyphen, which Java reads as the minus operator (book minus price). class is a keyword, permanently reserved. Exams like to hide the keyword option among normal-looking names, so scan for reserved words first.

At a glance

Operator families

FamilyOperatorsBookBridge use
Arithmetic+ - * / %fine = days * 2
Relational== != > < >= <=copies > 0
Logical&& || !available && copies > 0
Assignment= += -= *= /=copies -= 1 on issue
Increment/decrement++ --issuedCount++
Ternary?:msg = ok ? "Issued" : "Denied"

Think first

The char that grew

In BCA104, a C char occupied 1 byte. Before tapping: how big is a Java char, and why the change?

Show the answer

A Java char is 2 bytes, because it stores Unicode, not just ASCII. One byte covers only 256 symbols, enough for English but not for the world's scripts; 2 bytes let a char hold characters from Gujarati, Hindi, Tamil and beyond. This is a classic 1-mark difference question, and it also explains why Java is comfortable in international software.

Watch out

Java is stricter than C, on purpose

In C, int x = 3.5; compiles and silently stores 3. In Java the same line is a compile error: possible lossy conversion from double to int. Java refuses to throw away your .5 without an explicit cast (next lessons).

Second trap: = assigns, == compares. Writing if (copies = 0) in Java is also a compile error (the condition is not a boolean), which saves you from C's most famous silent bug.

Summary

Key takeaways

  • 8 primitives with fixed sizes everywhere: byte 1, short 2, int 4, long 8, float 4, double 8, char 2, boolean JVM-dependent.
  • char is 2 bytes because it stores Unicode; C's char was 1 byte ASCII.
  • Identifiers: letters, digits, _ and $; never start with a digit; case-sensitive; never a keyword.
  • Literal spellings declare types: L for long, f for float, single quotes for char, double quotes for String.
  • Decimals default to double; whole-number literals default to int.
  • Operators come in families: arithmetic, relational, logical, assignment, increment, ternary.
  • Memory hook: 1-2-4-8 twice (byte-short-int-long, then float-double), char gets 2 for the world.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati