0% found this document useful (0 votes)
26 views6 pages

10computer 10 - Lesson 11 - DISECTING MY FIRST JAVA PROGRAM

Programming lesson for gr 10 computer JAVA

Uploaded by

diannklare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
26 views6 pages

10computer 10 - Lesson 11 - DISECTING MY FIRST JAVA PROGRAM

Programming lesson for gr 10 computer JAVA

Uploaded by

diannklare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

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:

 identify the basic parts of a Java program;


 differentiate among Java literals, primitive data types, variable types, identifiers, and operators; and
 develop a simple valid Java program using the concepts learned in this lesson.

EXPLORE
Dissecting a Simple Java Program
Look at the following Java codes, then read the line-by-line explanation that follows:

Line 1 public class Hello


Line 2 {
Line 3 /**
Line 4 * A simple Java program
Line 5 */
Line 6 public static void main ( String [ ] args ) {
Line 7
Line 8 //prints the string Java program on screen
Line 9 System.out.println(“Java program”);
Line 10 }
Line 11 }

Line 1 public class Hello


This indicates the name of the class which is Hello. All codes in Java should be written inside a class
declaration. The class uses an access specifier public, which indicates that the class is accessible to
their classes from other packages.

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 6 public static void main( String [ ] args ){


This line indicates the name of method which is the main
method. All Java programs begin their process in the main method. Note:
In C++, it was called “function”,
while in
Line 8 //prints the string Java program on screen Java it is called “method”
This is a Java comment.

Line 9 System.out.println("Java program");


This line prints or displays the line "Java program". The command System.out.println() prints the text
enclosed by quotation marks on the screen.

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
}

In the block statement, any amount of white space is allowed.


Identifiers are what you assign as a name for your variables, methods, classes, packages, and
interfaces.

Java Identifier Rules

 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.

abstract boolean break byte case catch

char class const continue default do

double else extends final finally float

for goto if implements import instanceof

int interface long native new package

private protected public return short static

strictfp super switch synchronized this throw

throws transient try void volatile while

Java Literals
Literals are used to represent boolean, character, numeric, or string data. They provide a means of
expressing specific values in your program.

Primitive Data Types


Java programming language includes eight primitive data types. They represent atomic values and are
built-in to Java.
Boolean data type has two states, true and false.
Example: bool answer = true;

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' ;

Double data type is a floating point default data type.


Example: 3.14 //simple floating -point value (double) 8.02E23// a large floating-point value

Float data type is a single precision 32-bit IEEE 754 floating point.
Example: float fvalue = 234.5f;

Byte data type has a length of 8 bits.


Example: byte num1 = 100;

Short data type has a length of 16 bits.


Example: short w = 100 000;

Int data type has a length of 32 bits.


Example: int a = 500

Long data type has a length of 64 bits.


Example: long b = 100000L;

Similar to C++ programming, we define variables as an item of data used to store the state of objects.

To declare a variable, it must follow the general form:


<datatype> <name>
Example: int number;

To assign a value to a variable, we use the equal sign.


Example: number = 10 ;

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.

-A package is a collection of classes.


-You should always end your Java program with the .java extension.
-Assign a file name that matches the name of your public class. For example, if the name of your public
class is MyFirstProgram, you should save it in a file called MyFirstProgram.java.
-Always write comments in your code explaining what your class or method does or is supposed to do.
-In writing blocks, you can place the opening curly brace in the line with the statement or you can place
the curly brace on the next line. Add indention to the statements inside the block.
Example:
public static void main( String[ ] args )
{
System.out.println("Java Programming");
System.out.println("is fun!");
}

-It is a good practice to initialize your variables as you declare them.

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

Increment and Decrement Operators

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

You might also like