Java Structure, Identifiers, Data Types and Variables: ICSE Class 8
Updated 17 October 2026 · Fact-checked
A Java program is a class with a main method; the compiler checks syntax and turns it into bytecode that the JVM runs. Identifiers may use letters, digits, underscore and dollar sign, must not start with a digit or be keywords, and are case-sensitive. Data types such as int, float, double, char, boolean and String say what a variable holds. Declare, then initialise.
The compiler catches syntax errors before running; logic errors only show up as wrong output, so test your program.
Illustration drawn for QuestionBuddy.
Understand Java Program Structure, Identifiers, Data Types and Variables
Program shape.
class Welcome
{
public static void main(String args[])
{
System.out.println("Welcome to Java");
System.out.println("I am in Class 8");
}
}Output: two lines, Welcome to Java and I am in Class 8. main is where execution starts; every statement ends with a semicolon; // starts a comment. println prints and moves to a new line; print stays on the same line.
Compile and run. The source code is checked by the compiler for syntax errors and translated into bytecode, which the Java Virtual Machine executes. In BlueJ: click Compile, then run void main. Syntax errors (missing semicolon, Println instead of println) are found when compiling; logical errors (wrong formula) give wrong answers and are found by testing.
Identifiers. Names of variables and classes. Rules: letters, digits, underscore and dollar sign only; must not start with a digit (2marks is invalid, marks2 is valid); no spaces or symbols such as %; not a keyword (int, class, if); case-sensitive (Age and age differ).
Literals. Integer 25; real 3.14; character 'A'; string "Riya"; boolean true.
Data types.
| Type | Holds | Example |
|---|---|---|
| int | whole numbers | int age = 13; |
| float | decimals, less precise | float height = 152.5f; |
| double | decimals, more precise | double pi = 3.14159; |
| char | one character | char grade = 'A'; |
| boolean | true or false | boolean passed = true; |
| String | text (a class) | String name = "Riya"; |
Types save memory, store values correctly and let the computer catch errors (you cannot store "ten" in an int). A float literal needs f at the end.
Variables. A named memory location whose value can change. Declaration: int age; initialisation: age = 13; both together: int age = 13; several: int a = 17, b = 5;. The + between text and a value joins them.
Java Program Structure, Identifiers, Data Types and Variables: key facts
- Identifier rules
- Letters, digits, underscore and dollar sign
- Not start with a digit
- No spaces
- Not a keyword
- Case-sensitive
- 2marks is invalid; marks2 is valid.
- Data types
int whole numbers float and double decimals char one character boolean true or false String text - float needs f after the literal.
- Errors
- Syntax error: found at compile time | Logical error: wrong answer found by testing
- A missing semicolon is a syntax error.
How to answer Java Program Structure, Identifiers, Data Types and Variables questions
Use this to answer or write code questions.
- 1Check each identifier against the five naming rules.
- 2Choose the data type from the value: whole, decimal, one character, text or true or false.
- 3Declare and initialise with the correct literal form (f, single quotes, double quotes).
- 4Trace the printed output line by line.
- 5Classify any error as syntax or logical.
Java Program Structure, Identifiers, Data Types and Variables in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
A hypothetical report card program
Suppose a program stores a student's name, roll number, percentage and grade letter.
Choose a suitable declaration for each.
Try it first, then show the solutionSolution
- Name is text: String.
- Roll number is whole: int.
- Percentage has decimals: double.
- Grade is one character: char.
Answer: String name; int roll; double percentage; char grade;
Memory hook: Match the type to the value.
Tricks to remember Java Program Structure, Identifiers, Data Types and Variables
Short memory aids. Say each one aloud once, then use it on a question the same day.
2marks no, marks2 yes
An identifier cannot start with a digit.
Use it when: Naming rules.
Quotes tell the type
Single quotes: char; double quotes: String.
Use it when: Literals.
Java Program Structure, Identifiers, Data Types and Variables: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Valid identifier · 1 mark · MCQBoard level
Which is a valid Java identifier?
(a) 2marks (b) total marks (c) marks2 (d) marks%
Try it first, then show the solutionSolution
- 2marks starts with a digit, total marks has a space, marks% has a symbol.
Answer: (c) marks2.
Example 2: Choose the data type · 2 marks · Short answerBoard level
Choose a data type for each: (i) age 13 (ii) grade 'A' (iii) name "Riya" (iv) passed true. (2 marks)
Try it first, then show the solutionSolution
- (i) int and (ii) char (1).
- (iii) String and (iv) boolean (1).
Answer: (i) int, (ii) char, (iii) String, (iv) boolean.
Example 3: Trace a program · 3 marks · Short answerHarder board level
Write the output:
int age = 13;
String name = "Riya";
char grade = 'A';
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);(3 marks)
Try it first, then show the solutionSolution
- The + joins text and a value (1).
- Line 1: Name: Riya (1).
- Line 2: Age: 13 and line 3: Grade: A (1).
Answer: Name: Riya
Age: 13
Grade: A (hand-traced).
Example 4: Find the errors · 4 marks · Long answerHarder board level
Find all errors and correct them:
class Test
{
public static void main(String args[])
{
int 2age = 14
System.out.Println(2age);
}
}(4 marks)
Try it first, then show the solutionSolution
- 2age is invalid because it starts with a digit; use age2 (1).
- Semicolon missing after 14 (1).
- Println should be println (1).
- The name used in println must be changed to match, so int age2 = 14; and println(age2); (1).
Answer: Corrected: int age2 = 14; System.out.println(age2); The errors are a bad identifier, a missing semicolon and Println with a capital P; all are syntax errors found at compile time.
Example 5: Float or double? · 4 marks · Long answerHOTS / competency
A student writes float h = 152.5; and the compiler complains. Explain why, give two fixes, and say which type holds more precision. (4 marks)
Try it first, then show the solutionSolution
- Java treats 152.5 as a double (1).
- A double cannot be stored in a float without a suffix (1).
- Fix 1: float h = 152.5f; Fix 2: double h = 152.5; (1).
- double is more precise (1).
Answer: In Java a decimal literal without f is a double, so it does not fit a float. Write float h = 152.5f; or use double h = 152.5;. A double is more precise than a float.
Where students lose marks in Java Program Structure, Identifiers, Data Types and Variables
The slips examiners see most often, and what to write instead.
Mistake: Using a digit to start a name
Identifiers cannot start with a digit.
Wrong working
int 2age;
Right working
int age2;
Do this: Check rule 2.
Usually costs: 1 mark
Mistake: Writing a float without f
Decimals are doubles by default.
Wrong working
float h = 152.5;
Right working
float h = 152.5f;
Do this: Add f or use double.
Usually costs: 1 mark
Mistake: Using wrong quotes
Single quotes are for char, double quotes for String.
Wrong working
String name = 'Riya';
Right working
String name = "Riya";
Do this: Match quotes to type.
Usually costs: 1 mark
Exam tips
- Write Java keywords and method names in the exact case.
- Always end a statement with a semicolon.
- Show the output exactly, line by line.
- Name the error type when correcting code.
Practice questions on Java Program Structure, Identifiers, Data Types and Variables
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Which declaration correctly stores the text Class 8?
(a) char t = "Class 8"; (b) String t = "Class 8"; (c) int t = "Class 8"; (d) boolean t = "Class 8";Hint
Text in double quotes needs a class type.
Answer
(b) String t = "Class 8";
Q2 · 2 marks · Short answerBoard level
State four rules for naming an identifier in Java. (2 marks)
Hint
Digits, spaces, keywords, case.
Answer
It may use letters, digits, underscore and $; it must not start with a digit; it must not contain spaces; it must not be a keyword; it is case-sensitive.
Q3 · 3 marks · Short answerHarder board level
Write the output of: int a = 17, b = 5; System.out.println("Sum = " + a + b); (3 marks)
Hint
Text first, so numbers are joined as text.
Answer
Sum = 175, because after the text each number is joined as text. With brackets (a + b) the output would be Sum = 22.
Q4 · 4 marks · Long answerHarder board level
Declare variables to store a book title, number of pages, price with paise and whether it is available. Give a one-line reason for each type. (4 marks)
Hint
Four types.
Answer
String title; (text). int pages; (whole number). double price; (decimal). boolean available; (true or false).
Q5 · 4 marks · Long answerHOTS / competency
One program finds the area of a rectangle with + instead of *, and another leaves out a semicolon. Classify each error, say when it is found and how it is corrected. (4 marks)
Hint
Compiler versus testing.
Answer
Leaving out the semicolon is a syntax error; the compiler reports it when compiling and it is corrected by adding the semicolon. Using + instead of * is a logical error; the program runs but gives a wrong area, so it is found only by testing with sample data and corrected by using * in the formula.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Where does a Java program start running?Show answer
In the main method.
2.Which tool turns source code into bytecode?Show answer
The compiler.
3.What runs bytecode?Show answer
The Java Virtual Machine.
4.Is Age the same as age?Show answer
No, Java is case-sensitive.
5.What does println do?Show answer
Prints and moves to a new line.
6.Which type holds true or false?Show answer
boolean.
Java Program Structure, Identifiers, Data Types and Variables: frequently asked questions
Is String a basic data type?
No, it is a class, but it is used like a type for text.
Do I need BlueJ?
The chapter uses Java as in BlueJ, but any Java setup works.
What does // do?
It starts a comment that the computer ignores.