Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Constants and Block or Scope
Previous Home Next

A constant is a variable whose value cannot change once it has been assigned. The final keyword can be used with primitive data types and immutable. The final keyword means is that once the value has been assigned, it cannot be re-assigned. A block begins with an open braceand close is close brace.A block is a sequence of statements, local class declarations and local variable declaration statements within braces. A block is executed by executing each of the local variable declaration statements and other statements in order from first to last (left to right). After a block is executed all local variables defined inside the block is discarded, go out of scope.

You can declare local variables anywhere in a method body. if you declare a variable within a block, then it only has scope within the block.

PI = 3.14 

Example :

public class Constants
{
	public static void main(String[] args) 
	{
		double r = 10;
		double area;
		final double PI=3.14;
		area = PI * r * r;
		System.out.print("radius = ");
		System.out.println(r);
		System.out.print("area = ");
		System.out.println(area);
	}
}

output :

radius = 10.0

area = 314.00
Block or Scope in Java Programming

A block is a compound statement and consists of all the statements between an opening and closing brace. The scope of a variable defines the section of the code in which the variable is visible. variables that are defined within a block are not accessible outside that block.

  1. Empty body (Zero statements):
  2. { // Open brace starts block
    } // Close brace ends block 
  3. One statement:
  4. { // Open brace starts block
    
    	System.out.println( "One" ) ;
    
    } // Close brace ends block
  5. More than one statement:
  6. { // Open brace starts block
    
    	System.out.println( "One" ) ;
    	System.out.println( "Two" ) ;
    	System.out.println( "Three" ) ;
    
    } // Close brace ends block 

    Example :

    public void set()
    {
    	int x = 3 ;
    
    	{ // Open brace starts block
    		
    		int y = 4 ;
    
    	// Prints 7. y is accessible here
    	
    		System.out.println( "Sum is " + (x + y) );
    
    	} // End of block. y is out of scope
    
    		System.out.println( "x is " + x );
    	
    	// Won't compile. y is not in scope
    
    		System.out.println( "Sum is " + (x + y) ) ;
    }
    Previous Home Next