FAQ: Learn Java: Variables - Introduction

This community-built FAQ covers the “Introduction” exercise from the lesson “Learn Java: Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Introduction

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This is really frustrating, Can someone explain why this code errors? I use the exact same command to print out the year as I did the name and it still errors although it prints the year and name? Is it because name has ‘String’ in it?
https://www.codecademy.com/courses/learn-java/lessons/learn-java-variables/exercises/introduction

public class Creator {
	public static void main(String[] args) {
    String name = "James Gosling";
    int yearCreated = 1995;
    System.out.println (name);
    System.out.println (yearCreated);
	}
}
5 Likes

The issue here is how the Submission Correctness Test for this lesson checks your code. The SCT is using pattern matching to see if your code matches what is expected. The SCT is not expecting a space between println and the parenthesis:

System.out.println (name);
                  ^

Even though your code executes without error, and produces the desired output, it fails the test.

2 Likes

Thank you for the reply.

3 Likes

Hi!

Why cannot I use the print command like this?

System.out.println(name, year)

I.e. to print two variables with one command. Is there a command that would allow it?
Thanks!

As far as I know Java doesn’t support multi-value returns or print. On a print you can put as many values as you want in many ways into one print.

Welcome to the forums!

As @psmilliorn mentioned, Java doesn’t support printing or returning multiple variables. However, there are a few workarounds.

Printing

Returning

  • If the variables are of the same data type, you could return an array.
  • If the variables are not of the same data type, you could return an array of the Object class.
  • and much more…