Blocks are essential of any programming language. Java supports blocks.A block or compound statement is any number of simple Java statements that are surrounded
by a pair of braces. Blocks define the scope of your variables. Blocks can be nested inside another block.
public static void main(String[] args){
int a;
{
int k;
} //Scope of k limited till here only
}
Consider the case of shadowing a variable in inner block as in c/c++.
public static void main(String[] args){
int a;
{
int a;//Error as it is not allowed in java
}
}
Switch statement can use only int or enum type for cases. Nothing else is allowed.
Goto statement - In java goto is kept as a reserved keyword but java developers never implemented goto. The task of goto is accomplished by using "break label".
break label_name is used to break the control out of a block ,we cannot jump into another block.
label:
{
for(....)
{
while(...)
{
if(condition)
break label;
}
}
}
for each loop in java : syntax - for(datatype: array)
It is uesd to iterate the entire array. Read it as for each datatype in array
Eg int[] arr = new int[10];
for(int i = 0 ; i<>
Well Hello to all the readers of the blog. To start with the very first writeup of the blog i would like to introduce the purpose of the blog. The blog aims at providing in depth and sufficient knowledge of JAVA language. This blog will be beneficial for all those people who are new in professional world and figth with the concept and knowledge world in their early stages.
Total Pageviews
Wednesday, January 19, 2011
Blocks/Scope/Loops
Subscribe to:
Post Comments (Atom)
The new for loop structure in java 1.5
ReplyDeleteHere's how it worked prior to version 1.5:
vec is a Vector
SomeObject obj;
for(int i=0;i<vec.size();i++)
{
obj = (SomeObject) aVec.elementAt(i);
//code to use obj
}
Now, here's how it works in version 1.5:
for (Object aVec : vec)
{
obj = (SomeObject) aVec;
//code to use obj
}
As you can see, the loop incrementor is now handled implicitly.
@Neeraj it is not the case how it was implemented previosuly. The new for loop is an enhancement over the previosu for loop. This does not means that the earlier one(one with which most of the people are familier with) is not valid in java5.It is an advanced loop (copeid from the style used by the .net developers) which saves time in incrementing counter and calculating the length. And ya sorry for the incomplete article,i guess some problem in copy pasting form my file.
ReplyDelete