ApexSensei

Drills › Apex basics

Quick win: greet by name

Apex basics · Beginner · 5 min · Runs in your Salesforce Developer org · Free

Scenario. A welcome message needs to work for any name, so the name has to come from the input, not be typed in.

Join text and a variable with + to build a greeting for any name.

What you will learn

+ joins text together. Text in Apex sits inside single quotes.

String city = 'Austin';
String note = 'Welcome, ' + city + '!';

note is Welcome, Austin!. Watch the spaces: the one after the comma lives inside the quotes. Build the greeting the same way and return it from your method.

The task

Write a method greet(String name) that returns Hello, followed by the name and !.

Examples:

  • greet('Ana') → Hello, Ana!
  • greet('Jo Li') → Hello, Jo Li!

Requirements:

  1. Return 'Hello, ' + name + '!'.
  2. Keep exactly one space after the comma and no space before !.
  3. Keep the method's name and its String input.

Starter code

String greet(String name) {
    return null;
}

System.debug(greet('Ana'));
System.debug(greet('Sam'));

When it passes

`greet` returns `Hello, <name>!` for every name the grader tries.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Quick win: double a number · Quick win: is it positive? ›