Java Syntax Basics: Variables, Identifiers, and Structure. Practical Tasks
Below are six hands-on Java practice tasks for beginners covering the Language Lexicon section: comments, every kind of literal, declaring and calling methods, primitive type casting, and reading user input with the Scanner class. The tasks reinforce the theory from this section, and each one comes with a solution on Patreon.
Single-Line and Multi-Line Comments
Create a program that uses both single-line and multi-line comments.
Literals of Every Kind
Create a program that prints the following kinds of literals to the console:
- boolean;
- string;
- character;
- binary integer;
- octal integer;
- decimal integer;
- hexadecimal integer;
floatliteral;doubleliteral.
Hint
A binary literal is written with the 0b prefix (for example, 0b1010), a hexadecimal one with 0x, and an octal one with a leading zero. In long numbers you can separate digit groups with underscores: 1_000_000 — the compiler ignores them, but the number becomes far easier to read.
Calculator Built on Methods
- Create a method that takes two
intvariables as input. - The method calculates their sum and returns the result.
- Call this method from the
mainmethod. - Create additional methods for subtraction, multiplication, and division.
Type Conversion Table
Build a table of primitive type conversions: rows list the source type, columns list the type you convert to.
| from \ to | byte | short | char | int | long | float | double | boolean |
|---|---|---|---|---|---|---|---|---|
| byte | ||||||||
| short | ||||||||
| char | ||||||||
| int | ||||||||
| long | ||||||||
| float | ||||||||
| double | ||||||||
| boolean |
In each cell, write:
- i (implicit) — if the conversion happens automatically;
- e (explicit) — if an explicit cast is required;
- x — if the conversion is not possible;
- t — if it is an identity conversion (same type).
Easy to get wrong
Two rows of this table trip people up. First, boolean takes part in no conversions at all — its entire row and column (except the identity cell) are «not possible». Second, short and char are both 16 bits wide, yet each converts to the other only with an explicit cast: short is signed and char is unsigned, so neither range fits inside the other.
Type Casting Practice
Create a program that sequentially performs conversions between all compatible primitive types. Implement each conversion in a separate method.
Sum of Numbers with Scanner
Create a program that calculates and displays the sum of two integers entered by the user with the Scanner class. If the user enters at least one of the numbers incorrectly, report an error.
Hint
It is convenient to validate the input before reading it: scanner.hasNextInt() tells you whether the next token is an integer. That way nextInt() is called only on valid input, and you never have to catch an InputMismatchException.
Check your code
As you write these programs, pay attention to code style: meaningful names, consistent indentation, one task per method. Habits formed on small exercises carry over directly to real projects.
Frequently Asked Questions
What kinds of literals exist in Java?
Six groups: integer literals (in decimal, binary 0b1010, octal 012, and hexadecimal 0xA notation), floating-point literals (double by default, float with the f suffix), the boolean literals true/false, character literals in single quotes, string literals in double quotes, and the null literal.
What is the difference between implicit and explicit type conversion?
An implicit (widening) conversion is performed by the compiler automatically when the value is guaranteed to fit into the target type: int → long, int → double. An explicit (narrowing) conversion requires a cast operator, (byte) x, and may lose data: high-order bits are dropped and the fractional part is truncated.
Can you cast boolean to int in Java?
No. Unlike C/C++, boolean in Java takes part in no numeric conversions — neither implicit nor via an explicit cast. If you need 1 or 0, use the ternary operator: flag ? 1 : 0.
Why does the division method in the calculator return a whole number?
Because dividing two int values in Java is integer division: 7 / 2 yields 3, and the fractional part is discarded. To get 3.5, cast at least one operand to double — (double) a / b — and declare the method's return type as double.
What happens if the user types something other than a number into Scanner?
The nextInt() call throws an InputMismatchException, and the program crashes if the exception is not handled. It is safer to check the input first with hasNextInt() and read the number only when it returns true — otherwise print an error message.
Comments