Page|21
CHAPTER 4:INTRODUCTION TO JAVA CODING
5.1 ES SENTIALS
In order to start writing code in java, it is essential to understand the meaning and use of
c ertain keywords that are the building blocks of any java code.
Keywords Meaning Example Code
public n access modifier that allows classes,
A ublic class Example1 {
p
attributes, methods and constructors to public static void main(String
be accessible by other classes. args[]) {
class This keyword defines a class. [Link]("Hello
World.");
main This defines the main method. }
}
args It represents arguments passed in the
java command-line.
static It is usedformemorymanagement.Itis
used to share properties of a class with
other classes.
void It is used during method declaration to
indicate that a method does not return
any type.
tring[]
S It represents an array of strings that
args stores the arguments passed inthejava
command-line.
5.2 SC OPES
I njava,ablockofcodeisthecodeinsidethecurlybraces,{}.Thescopeofavariablerepresents
the space or block of code in which the variable can be accessed and modified. Usually, the
scopeofavariableistheblockofcodeinwhichitiscreated,forexample-(1)Avariabledeclared
insidethecurlybracesofaloopisonlyvalid(canbeaccessedandmodified)withinthescopeof
the loop, (2) A variable declared inside the curly braces of a method is only valid within the
scope of the method.
Example Code Output
ublic class Example1 {
p rror: cannot find
e
public static void main(String args[]) { symbol
int x=10;
int y=25;
int z=x+y;
while (x>5){
int m=3;//m initialized inside loop.
int n=m+x;//n initialized inside loop.
x--;
}
[Link]("Sum of x+y = " + z);
[Link](m+" "+n);//m & n not accessibleoutside loop.
}
}
public class Example2 { 3 13
Page|22
public static void main(String args[]) { 12
3
int x=10; 3 11
int y=25; 3 10
int z=x+y; 3 9
while (x>5){ Sum of x+y = 35
int m=3;//m initialized inside loop.
int n=m+x;//n initialized inside loop.
[Link](m+" "+n);//m & n accessibleonly inside loop.
x--;
}
[Link]("Sum of x+y = " + z);
}
}
public class Example3 { rror: cannot find
e
public static void main(String args[]) { symbol
int x=10;
int y=25;
int z=x+y;
while (x>5){
int m=3;//m initialized inside loop.
int n=m+x;//n initialized inside loop.
x--;
}
[Link]("Sum of x+y = " + z);
m+=5;//m cannot be modified outside loop.
[Link](m+" "+n);//m & n not accessibleoutside loop.
}
}
public class Example4 { 13
8
public static void main(String args[]) { 8 12
int x=10; 8 11
int y=25; 8 10
int z=x+y; 8 9
while (x>5){ Sum of x+y = 35
int m=3;//m initialized inside loop.
int n=m+x;//n initialized inside loop.
m+=5;//m can only be modified inside loop.
[Link](m+" "+n);//m & n accessibleonly inside loop.
x--;
}
[Link]("Sum of x+y = " + z);
}
}
5.3 PR INTING
I nordertoshowanoutputofaprogram,weusetheprintstatement,[Link](“Text
to be printed.”).The[Link]commandbasicallyinvokestheSystemclasstogeneratean
output.Theprintln()isabuilt-inpublicmethodusedtoshowanoutput.Itprintsthetextinside
themethodparameter,aswellasanewline.Toavoidaddingthenewlineattheend,print()can
be used instead as[Link]().
Example Code Output
public class Example1 { 10
Page|23
public static void main(String args[]) {
9
int x=10; 8
while (x>5){ 7
[Link](x);//New line is addedat the end. 6
x--;
}
}
}
public class Example1 { 109876
public static void main(String args[]) {
int x=10;
while (x>5){
[Link](x);//No new line is addedat the end.
x--;
}
}
}
5.4 CO
MMENTS
I naprogram,commentsarelineswhicharenotexecutedbutaretheretohelpsomeonewhois
notfamiliarwiththecodetounderstandhowthecodeworks.Thecompilerignorestheselines
while compiling. Writing comments can also help to debug code easily.
In java, comments are 3 types:
1. S ingle-line comments: These are comments written in a single line and are usually
short.Thecommentisinitiatedusingtwoforwardslashes,//.Anythingwrittenafterthe
second slash in the same line will be considered as a comment.
2. Multi-line comments: These are comments written when descriptions need to be
written for methods or loops in multiple lines.Writingmulti-linecommentsusing//is
possible,butitbecomestediouswhenthereareseverallines,and//needstobewritten
before each line. In order to make writing multi-line comments easier, use a forward
slash,/,followedbyanasterisk,*,thenwritethecomment,andendusinganasterisk,
and a forward slash.
3. Documentation comments: It is often referred to asdoccomment.Itisusuallyused
when writing code for a software or package to generate a documentation page for
reference.Itfollowsthesameconventionasamulti-linecomment,onlyanexceptionof
an additional asterisk at the beginning of every new line.
Comment type Example
ingle line
S / /This is a single-line comment.
Multi–line /*Multi-line comment starts
This is a multi-line comment.
Multi-line comment ends.*/
Documentation /** Doc comment starts
*continue
*add tags
*doc comment ends*/
Page|24
5.5 WORKSHEET
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.
ample Input #1:
S utput:
O
Samiha Full Name: Samiha Haque
Haque
ample Input #2:
S
utput:
O
Sabbir
Full Name: Sabbir Ahmed
Ahmed
D. T ake the first name, middle name and last name from the userandprinttheoutputs
accordingly. Keepinmindthatausermayormaynothaveamiddlename.Iftheuser
does not have one, they must enter an empty string as the middle name.
ample Input #1:
S utput:
O
Aiman Full Name: Aiman Jabeen Ramisa
Jabeen
Ramisa
ample Input #2:
S utput:
O
Dibyo Full Name: Dibyo Fabian Dofadar
Fabian
Dofadar
ample Input #3:
S utput:
O
Sifat Full Name: Sifat Tanvir
Tanvir