What are Blocks in java



In this core java tutorial we will learn about blocks in java.


Contents of page >
  • What are blocks in java?
    • Program / Example of blocks in java -
  • What is scope of variable declared in block in java?
  • What happens to variable declared in block when block ends in java?
  • What does java docs say about blocks?



Control Flow Statements

4 oops (object oriented programming system) concepts in java



What are blocks in java?
A block is a group of zero or more statements in java written between the balanced braces.

Program / Example of blocks in java -
class MyClass {
   public static void main(String[] args) {
          boolean var = true;
          if (var) { // begin block 1
                 int x = 1; // x is declared inside block
                 //..........
                 //code inside block...
                 //..........
          } // end block 1
          else { // begin block 2
                 int y = 1;
                 //..........
                 //code inside block...
                 //..........
          } // end block 2
   }
}

What is scope of variable declared in block in java?
scope of variable declared in block is till the end of block in java.


What happens to variable declared in block when block ends in java?
All the variables declared inside block becomes eligible for gc (garbage collection) when we exit that block (As scope of those variable is only that block) in java.
Program / Example to show variables declared inside block becomes eligible for gc (garbage collection) when we exit that block in java-
class MyClass {
   public static void main(String[] args) {
          boolean var = true;
          if (var) { // begin block 1
                 int x = 1; // x is declared inside block
                 //..........
                 //code inside block...
                 //..........
          } // end block 1 //And now x is eligible for gc (garbage collection)
          else { // begin block 2
                 int y = 1;
                 //..........
                 //code inside block...
                 //..........
          } // end block 2 //And now y is eligible for gc (garbage collection)
   }
}


What does java docs say about blocks?
“A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed”.


Summary -
We learned What are blocks in java? scope of variable declared in block in java. What happens to variable declared in block when block ends in java? What does java docs say about blocks?



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Super keyword in java - Invoke constructor, Access instance variable, Invoke instance method of immediate super class using super



this keyword in java - in constructor, refer instance variable, call instance method, this (current class object) as a parameter, return this



Differences between Instance initialization block and Static initialization block in java - Features in detail with programs



Inner class/ nested class, static, local and anonymous inner class in java



Arithmetic, Unary, Equality, Relational, Conditional, Bitwise, Bit Shift, Assignment, instanceof operators in detail with programs.



JDK (Java Development Kit), JRE (Java Runtime environment), JVM (java virtual machine), Differences between JDK, JRE and JVM, OpenJDK, JIT Compiler (Just In Time Compiler) internal working


No comments:

Post a Comment