0% found this document useful (0 votes)
11 views4 pages

Java Coding Basics and Examples

Chapter 4 introduces essential Java coding concepts, including keywords, variable scopes, printing outputs, and comments. It explains the significance of keywords like 'public', 'class', and 'static', and illustrates variable scope through code examples. The chapter also covers different types of comments in Java and provides worksheets for practical coding exercises.

Uploaded by

Tashfiqul Islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Java Coding Basics and Examples

Chapter 4 introduces essential Java coding concepts, including keywords, variable scopes, printing outputs, and comments. It explains the significance of keywords like 'public', 'class', and 'static', and illustrates variable scope through code examples. The chapter also covers different types of comments in Java and provides worksheets for practical coding exercises.

Uploaded by

Tashfiqul Islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Page‬‭|‬‭21‬

‭CHAPTER 4:‬‭INTRODUCTION TO JAVA CODING‬

‭5.1‬ ‭E‭S‬ 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‬
j‭ava command-line.‬
‭static‬ ‭It‬ ‭is‬ ‭used‬‭for‬‭memory‬‭management.‬‭It‬‭is‬
‭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‬ ‭in‬‭the‬‭java‬
‭command-line.‬

‭5.2‬ ‭S‭C‬ OPES‬


I‭ n‬‭java,‬‭a‬‭block‬‭of‬‭code‬‭is‬‭the‬‭code‬‭inside‬‭the‬‭curly‬‭braces,‬‭{}.‬‭The‬‭scope‬‭of‬‭a‬‭variable‬‭represents‬
‭the‬ ‭space‬ ‭or‬ ‭block‬ ‭of‬ ‭code‬ ‭in‬ ‭which‬ ‭the‬ ‭variable‬ ‭can‬ ‭be‬ ‭accessed‬ ‭and‬ ‭modified.‬ ‭Usually,‬ ‭the‬
‭scope‬‭of‬‭a‬‭variable‬‭is‬‭the‬‭block‬‭of‬‭code‬‭in‬‭which‬‭it‬‭is‬‭created,‬‭for‬‭example-‬‭(1)‬‭A‬‭variable‬‭declared‬
‭inside‬‭the‬‭curly‬‭braces‬‭of‬‭a‬‭loop‬‭is‬‭only‬‭valid‬‭(can‬‭be‬‭accessed‬‭and‬‭modified)‬‭within‬‭the‬‭scope‬‭of‬
‭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 accessible‬‭outside 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 accessible‬‭only 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 accessible‬‭outside 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 accessible‬‭only inside loop.‬
‭ x--;‬
‭ }‬

‭ [Link]("Sum of x+y = " + z);‬


‭ }‬
‭}‬

‭5.3‬ ‭P‭R‬ INTING‬


I‭ n‬‭order‬‭to‬‭show‬‭an‬‭output‬‭of‬‭a‬‭program,‬‭we‬‭use‬‭the‬‭print‬‭statement,‬‭[Link](“Text‬
‭to‬ ‭be‬ ‭printed.”).‬‭The‬‭[Link]‬‭command‬‭basically‬‭invokes‬‭the‬‭System‬‭class‬‭to‬‭generate‬‭an‬
‭output.‬‭The‬‭println()‬‭is‬‭a‬‭built-in‬‭public‬‭method‬‭used‬‭to‬‭show‬‭an‬‭output.‬‭It‬‭prints‬‭the‬‭text‬‭inside‬
‭the‬‭method‬‭parameter,‬‭as‬‭well‬‭as‬‭a‬‭new‬‭line.‬‭To‬‭avoid‬‭adding‬‭the‬‭new‬‭line‬‭at‬‭the‬‭end,‬‭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 added‬‭at 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 added‬‭at the end.‬
‭ x--;‬
‭ }‬
‭ }‬
‭}‬

‭5.4‬ ‭C‭O
‬ MMENTS‬
I‭ n‬‭a‬‭program,‬‭comments‬‭are‬‭lines‬‭which‬‭are‬‭not‬‭executed‬‭but‬‭are‬‭there‬‭to‬‭help‬‭someone‬‭who‬‭is‬
‭not‬‭familiar‬‭with‬‭the‬‭code‬‭to‬‭understand‬‭how‬‭the‬‭code‬‭works.‬‭The‬‭compiler‬‭ignores‬‭these‬‭lines‬
‭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.‬‭The‬‭comment‬‭is‬‭initiated‬‭using‬‭two‬‭forward‬‭slashes,‬‭//.‬‭Anything‬‭written‬‭after‬‭the‬
‭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.‬‭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-line‬ ‭comments‬ ‭easier,‬ ‭use‬ ‭a‬ ‭forward‬
‭slash,‬‭/,‬‭followed‬‭by‬‭an‬‭asterisk,‬‭*,‬‭then‬‭write‬‭the‬‭comment,‬‭and‬‭end‬‭using‬‭an‬‭asterisk,‬
‭and a forward slash.‬
‭3.‬ ‭Documentation‬ ‭comments:‬ ‭It‬ ‭is‬ ‭often‬ ‭referred‬ ‭to‬ ‭as‬‭doc‬‭comment.‬‭It‬‭is‬‭usually‬‭used‬
‭when‬ ‭writing‬ ‭code‬ ‭for‬ ‭a‬ ‭software‬ ‭or‬ ‭package‬ ‭to‬ ‭generate‬ ‭a‬ ‭documentation‬ ‭page‬ ‭for‬
‭reference.‬‭It‬‭follows‬‭the‬‭same‬‭convention‬‭as‬‭a‬‭multi-line‬‭comment,‬‭only‬‭an‬‭exception‬‭of‬
‭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‬ ‭W‬‭ORKSHEET‬
‭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‬ ‭user‬‭and‬‭print‬‭the‬‭outputs‬
‭accordingly.‬ ‭Keep‬‭in‬‭mind‬‭that‬‭a‬‭user‬‭may‬‭or‬‭may‬‭not‬‭have‬‭a‬‭middle‬‭name.‬‭If‬‭the‬‭user‬
‭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‬

You might also like