CodeBlanks Logo Icon CodeBlanks

Summary - Variables

Combine int, double and String to write a profile card.

Summary - Variables

In this group you learned Java's basic variable types:

  • int: for integers
  • double: for decimals
  • boolean: for true/false
  • String: for text
  • final: for constants

Variable names use camelCase, constants use ALL_CAPS. Now combine them and write a profile card.

Practice Write a profile card

Write a Main class with a main method. Assign "Alex" to String name and 28 to int age. Print both with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        String name = "Alex";
        int age = 28;
        System.out.println(name);
        System.out.println(age);
    }
}

Expected output

Alex
28

Next steps

You now know the basic variable types. In the next groups you will see:

  • Type casting and the Math class
  • String methods (length, substring)
  • Conditionals (if, switch)
  • Loops (for, while)
  • Arrays and collections

To keep the knowledge fresh, write your own small programs. Declare variables, assign values, and print with println. With practice the concepts will settle in.

Practice Final constant with a profile

Write a Main class with a main method. Assign "London" to final String CITY. Print it with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        final String CITY = "London";
        System.out.println(CITY);
    }
}

Expected output

London

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the types

Term Meaning
int Integer
double Decimal
boolean Logical
String Text

2 Define a constant

Question
final int MAX_USERS = ____;
Answer
final int MAX_USERS = 100;

Explanation: A final constant takes an integer without quotes.

All lessons in Beginner