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)
| Type | Size | Example use in BookBridge |
|---|---|---|
| byte | 1 byte | copies on one shelf (-128 to 127) |
| short | 2 bytes | books in one section |
| int | 4 bytes | total books, fines in rupees (the default choice) |
| long | 8 bytes | ISBN numbers (too big for int) |
| float | 4 bytes | average rating (needs f suffix) |
| double | 8 bytes | price (the default for decimals) |
| char | 2 bytes | section letter 'C' (Unicode) |
| boolean | JVM-dependent | available: 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:
copiesandCopiesare 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:
5is an int;9788183331630Lneeds the L because it exceeds int's range350.50is a double by default;4.5fneeds the f to be a float'C'(single quotes) is a char;"C"(double quotes) is a Stringtrue,false,nullare 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?
- 2ndBook
- book-price
- _bookCount
- 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
| Family | Operators | BookBridge 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.