10computer 10 - Lesson 11 - DISECTING MY FIRST JAVA PROGRAM
10computer 10 - Lesson 11 - DISECTING MY FIRST JAVA PROGRAM
One of the advantages of learning Java programming is that Java is an elegant language combined
with a powerful and well-designed set of APIs. Programmers enjoy programming in Java because they get
quick results. Java increases programmer efficiency, because it provides a well-designed set of APIs, in which
programmers write better code with fewer bugs than other platforms, reducing development time.
NOTE:
Install “Netbeans” and “JRE” when you are using laptop or desktop computer.
Install “Java N IDE” or similar compiler for Java, for android phones.
Install similar application for Iphone or other smart phones.
OBJECTIVES
At the end of this lesson, you should be able to:
EXPLORE
Dissecting a Simple Java Program
Look at the following Java codes, then read the line-by-line explanation that follows:
Line 2 {
An opening curly brace { indicates the start of a block. The curly brace was placed on the next line after
the class declaration, but we can also write the curly brace next to the first line of our code,
Line 3 /*
Line 4 A simple Java program
Line 5 */
These three lines represent a Java comment. It is used to document a part of a code, but is not part of
the program. It only informs the programmer about the code or program. It is a good programming practice to
add comments to your code.
Line 10 }
Line 11 }
These last two lines that contain two curly braces are used to close the main method and class.
Java Comments
If you noticed in the basic code we dissected earlier, there were two comments with different syntax.
This is because in Java, there are three ways we can write comments.
Multi-line Comment - This is also known as C-style comment. It opens with a slash-asterisk (/*) and
closes with an asterisk-slash (*/). Any text that is placed between these two is ignored by the compiler.
Example:
/* this is a C-style comment
*or a multiple liner comment.
*It can occur in multiple lines
*/
Single-line Comment - This is a comment that is written in one line only. It is usually used to comment
on a single line of code. It opens with a double slash// and should be written in a single line only. The
compiler ignores everything from the double slash to end of the line.
Example:
// this is a single line comment
Java Statements
A statement is one or more lines of code in a Java program that should be terminated with a semicolon.
Example:
System.out.println ("I love programming!");
A block is one or more statements bounded by opening and closing curly braces { } that group the
statements as one unit.
Example:
public static void main( String[ ] args ) {
System.out.println("Java Programming");
System.out.println("is fun!");
Tech Book Series 10 | C++ and Java Programming Made Simple | Author: Rommel A. Bagtas | Page 2 of 6
}
An identifier must have at least one character, in which the first character must be an alpha,
underscore, or a dollar sign.
Identifiers should not contain spaces.
Keywords or reserved words should not be used as identifiers.
Identifiers are case-sensitive, meaning Name and name are considered different identifiers.
For class identifiers, write the first letter in uppercase format.
Example: MyFirstJavaProgram
For method and variable identifiers, write the first letter in lowercase format.
Example: firstMethod
Keywords can be identified easily in a code because they are highlighted in blue and bold fonts. These
are reserved words that have a predefined meaning in the programming language.
Java Literals
Literals are used to represent boolean, character, numeric, or string data. They provide a means of
expressing specific values in your program.
Tech Book Series 10 | C++ and Java Programming Made Simple | Author: Rommel A. Bagtas | Page 3 of 6
Character data type written as char, represents a single Unicode character. The literals must be
enclosed in single quotes.
Example: char gender = 'F' ;
Float data type is a single precision 32-bit IEEE 754 floating point.
Example: float fvalue = 234.5f;
Similar to C++ programming, we define variables as an item of data used to store the state of objects.
We can also declare and assign value to a variable at the same time.
Example: int number = 10;
When using literals, statements, and other features of Java, we must always keep the following in mind.
Example:
int value1 = 20;
boolean answer = true;
Tech Book Series 10 | C++ and Java Programming Made Simple | Author: Rommel A. Bagtas | Page 4 of 6
char letter = 'a';
TRY IT!
Sample
Relational Operators
The operators compare two values and determine the relationship between the values. These are usually used
in conditional statements.
Logical Operators
Truth table for && and & operators (AND)
Given an expression:
exp1 & & exp2
The short-circuit and (&&) operator will evaluate exp1, and immediately return a false value if exp1 is
false. But if exp1 is false, the operator never evaluates exp2 because the result of the operator will be false
regardless of the value of exp2. The ampersand and (&) operator always evaluates both exp1 and exp2 before
returning the result. It only returns a result of TRUE, if both expressions are TRUE.
The program code demonstrates the && operator where the result=true. Since the line, exp2=a<b, is
already true, it will no longer evaluate exp2. The program code that demonstrates the & operator where both
exp1 and exp2 are false produces a result=false. Remember that the ampersand and (&) will only have a
TRUE result if both expressions are TRUE.
Notice that the OR operator's result is the contrast of AND operator's result.
SUMMMARY
A statement is one or more lines of code in a Java program that should be terminated with a
semicolon.
A block is one or more statements bounded by an opening and closing curly braces that group the
statements as one unit.
Identifiers are what you assign as a name of your variables, methods, classes, packages, and
interfaces.
Literals are used to represent boolean, character, numeric, or string data. It provides a means of
expressing specific values in your program.
Variables, as an item of data, are used to store the state of objects.
o A package is a collection of classes.
o The print ( ) method will print or display the string enclosed in quotation marks but will not move
the cursor to a new line.
o The println ( ) method will print or display the string enclosed in quotation marks and will move
the cursor to a new line.
QUESTIONS?
CLARIFICATIONS?
message me on MS TEAMS
Tech Book Series 10 | C++ and Java Programming Made Simple | Author: Rommel A. Bagtas | Page 5 of 6
Tech Book Series 10 | C++ and Java Programming Made Simple | Author: Rommel A. Bagtas | Page 6 of 6