4 - Introduction To Java Coding
4 - Introduction To Java Coding
5.1 ESSENTIALS
Inordertostartwritingcodeinjava,itisessentialtounderstandthemeaninganduseofcertainkeywordsthat
a re the building blocks of any java code.
5.2 SCOPES
I njava,ablockofcodeisthecodeinsidethecurlybraces,{}.Thescopeofavariablerepresentsthespaceor
blockofcodeinwhichthevariablecanbeaccessedandmodified.Usually,thescopeofavariableistheblock
ofcodeinwhichitiscreated,forexample-(1)Avariabledeclaredinsidethecurlybracesofaloopisonlyvalid
(canbeaccessedandmodified)withinthescopeoftheloop,(2)Avariabledeclaredinsidethecurlybracesofa
method is only valid within the scope of the method.
int z=x+y; 9
3
while (x>5){ Sum of x+y = 35
int m=3;//m initialized inside loop.
int n=m+x;//n initialized inside loop.
System.out.println(m+" "+n);//m & n accessibleonly inside loop.
x--;
}
5.3 PRINTING
I n order to show an output of a program, we use the print statement, System.out.println(“Text to be
printed.”).TheSystem.outcommandbasicallyinvokestheSystemclasstogenerateanoutput.Theprintln()is
abuilt-inpublicmethodusedtoshowanoutput.Itprintsthetextinsidethemethodparameter,aswellasanew
line. To avoid adding the new line at the end,print()can be used instead asSystem.out.print().
x--;
}
}
}
public class Example1 { 109876
public static void main(String args[]) {
int x=10;
while (x>5){
System.out.print(x);//No new line is addedat the end.
x--;
}
}
}
5.4 COMMENTS
I naprogram,commentsarelineswhicharenotexecutedbutaretheretohelpsomeonewhoisnotfamiliarwith
the code to understand how the code works. The compiler ignores these lines while compiling. Writing
comments can also help to debug code easily.
1. S ingle-line comments: These are comments written in a single line and are usually short. The
commentisinitiatedusingtwoforwardslashes,//.Anythingwrittenafterthesecondslashinthesame
line will be considered as a comment.
2. Multi-linecomments:Thesearecommentswrittenwhendescriptionsneedtobewrittenformethods
or loops in multiple lines. Writing multi-line comments using // is possible, but it becomes tedious
when there are several lines, and // needs to be written before each line. In order to make writing
multi-linecommentseasier,useaforwardslash,/,followedbyanasterisk,*,thenwritethecomment,
and end using an asterisk, and a forward slash.
3. Documentation comments: It is often referred to as doc comment. It isusuallyusedwhenwriting
code for a software or package to generate a documentation page forreference.Itfollowsthesame
convention as a multi-line comment, only an exception of an additionalasteriskatthebeginningof
every new line.
5.5 WO
RKSHEET
A. Take two integer numbers from the user and print the summation and average.
B. T
ake the values of x, y and z and solve the result of the following equation:
result= x+(x%2+y*z^ (4/2))
C. Take the first name and last name from the user and print the outputs accordingly.
D. T akethefirstname,middlenameandlastnamefromtheuserandprinttheoutputsaccordingly.Keep
inmindthatausermayormaynothaveamiddlename.Iftheuserdoesnothaveone,theymustenter
an empty string as the middle name.
Tanvir