CodeGym /Courses /New Java Syntax /Commands and code blocks

Commands and code blocks

New Java Syntax
Level 3 , Lesson 6
Available

"Let me tell you about commands (statements) and code blocks. This is really simple stuff. A method body consists of commands, or statements. Each command ends in a semicolon."

Examples of commands:
1
String s = "Name";
2
System.out.println(1234);
3
return a + b * c;
4
throw new RuntimeException();
5
;

"A code block consists of several commands combined using curly brackets. A method body is a code block."

Examples:
1
{}
2
{
    throw new RuntimeException();
}
3
{
    return null;
}
4
{
    System.out.println(23);
    System.out.println(1);
    System.out.println(14);
}

"The following rule is valid in almost any situation: wherever you can write one command, you can also write a code block. We will see examples of this in subsequent tasks."

Comments (43)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Eden Level 4, Brighton, United States Expert
26 April 2024
This lesson should have been presented right before teaching us about variable scope
Geoffrey F. Level 3, United States of America, United States
15 May 2024
Yes, I totally agree with you!
Khrysus Level 3, Denver, United States
14 January 2024
Don't give up!
Lunita Level 4, Dominican Republic
3 February 2023
Block Scope A block of code refers to all the code between curly braces {}. A block of code may exist on its own or it can belong to an if, while or for statement. In the case of for statements, variables declared in the statement itself are also available inside the block's scope. Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared... Example:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

    } // The block ends here

  // Code here CANNOT use x

  }
}
ayadi1 Level 18, berkane, Morocco
8 April 2022
nice one
steven anthony kwong Level 9, United Kingdom, United Kingdom
5 January 2022
I love it. Once you get over the initial hurdle. I think this way of teaching is so much more fun. It gives me a lot of failures but eventually, I do manage to overcome it. To be totally honest, I am not sure if I am able to solve the puzzles If I come across them again because everything is so unfamiliar. It's definitely down to practice and doing these exercises has given me a real taste of what it is like in the programming world.
Elyssa Level 3, Australia
5 December 2021
It's an interesting way of teaching and learning but I don't think i'll be renewing when this month is over. I see why it's effective but every lesson I feel dumber and dumber and feel less and less motivated to continue over such simple codes and fixes.
Phil Level 3, UK
26 December 2024
Agreed, the course is not intuitive at all... Hope you're doing well, I know you posted that comment 3 years ago!
hosniaro Level 27, Heiloo, Israel
15 February 2021
Initialization blocks do not replace the constructors and may be used along with them. But it is very important to mention that initialization blocks are always called before any constructor.
Agent Smith Level 38
5 August 2020
An interesting way of teaching indeed. Usually this stuff is being explained first, before practicing, but here practicing comes first.
Daisy Level 8, San Jose
30 May 2021
Yeah, and I find this way really helpful. Post tasks first to force us to think, and then we will learn faster.
Denis Edward Hirschhorn Level 3, Belmont, United States
3 November 2021
I honestly find this way of learning a lot slower. It's a lot harder for me to figure out what to do next when I haven't learned WHY half the stuff on your screen is even there to begin with.
Austin Okojie Level 8, London, Canada
10 June 2020
A code block can also be referred to as code scope, scopes can be nested
Andrei Level 41
5 September 2020
Next level Matrix stuff right here. Noice
26 December 2019
considering the above, codeblock basically creates a scope right?