0% found this document useful (0 votes)
6 views23 pages

Java Programming Basics and Syntax

Chapter Five (Part 2) of the Fundamentals of Information Technology introduces programming in Java, covering topics such as application structure, reserved words, comments, identifiers, variables, data types, assignment operators, and constants. It emphasizes the importance of syntax, the role of the main method, and the rules for naming identifiers and declaring variables. Examples and exercises are provided to reinforce understanding of these concepts.

Uploaded by

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

Java Programming Basics and Syntax

Chapter Five (Part 2) of the Fundamentals of Information Technology introduces programming in Java, covering topics such as application structure, reserved words, comments, identifiers, variables, data types, assignment operators, and constants. It emphasizes the importance of syntax, the role of the main method, and the rules for naming identifiers and declaring variables. Examples and exercises are provided to reinforce understanding of these concepts.

Uploaded by

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

Fundamentals of Information Technology

Chapter Five (Part 2)

Introduction to Programming in Java

Faculty of Information Technology - Computer Science Department 1


Outline
 Introduction

 Java Application Structure

 Reserved Words

 Comments

 Identifiers

 Variables and Data Types

 Assignment Operator

 Constants
Faculty of Information Technology - Computer Science Department 2
Java Application Structure
Notes
•Java applications must follow a predetermined syntax.
The syntax is a set of rules to strictly follow to write instructions.
•A class is a computer-understandable template for defining data
(attributes) and methods.
•Java source programs are case-sensitive.
• For example, Area differs from aRea and area.
•A pair of braces (curly brackets { } ) in a program marks the beginning
and end of a block of code that groups the program's components.
•A block is a construct that groups of programming statements.

Faculty of Information Technology - Computer Science Department 3


Java Application Structure
Notes (contd.)
• In Java, each block begins with an opening brace { and ends with a closing brace }.
• Every class has a class block that groups the data and methods of the class.
• Similarly, every method has a method block that groups the statements in the method.
• Blocks can be nested, meaning that one block can be placed within another.
• The following program skeleton contains two blocks:
• The class block and the main method block.
• The main method block is nested with the class block.

Faculty of Information Technology - Computer Science Department 4


A Simple Hello World Application
public class HelloWorld
{

public static void main(String[] args)


{

[Link]("Hello World! ");

}
}

Faculty of Information Technology - Computer Science Department 5


A Simple Hello World Application (contd.)
• The main method is referred to as the program’s entry point.
• It is where the program begins execution.
• A Java program is executed from the main() method in the class.
• A class may contain several methods.
• There CAN NEVER BE more than one main() method in the same class.
• The signature (i.e., header) of the main() method is always the same.
• A method is a block of code that contains or groups statements that perform an
operation.
Question:
Write a Java application that is called WelcomeApp that displays
Welcome to Java.

Faculty of Information Technology - Computer Science Department 6


Java Reserved Words
• A reserved word, or keyword, has a specific meaning to the compiler and cannot
be used for other purposes in the program.
• For example, when the compiler sees the word class, it understands that the word
after class is the name for the class.

Faculty of Information Technology - Computer Science Department 7


Comments
• Comments help programmers communicate, remember and understand the
program.
• They provide a better understanding of the program.
• Comments are not programming statements and thus are ignored by the
compiler.
• Two Types of Comments:
1. Single-line comments: comments are preceded by double forward slashes (//) and they
extend on only one line.
2. Multiline comments: comments are enclosed between /* and */ and they span
multiple lines.
• When the compiler encounters the // symbol, it ignores whatever comes after it.
• When it sees the /* symbol, it scans for the first occurrence of */ and ignores
any text between them, i.e., between /* and */.
Faculty of Information Technology - Computer Science Department 8
Comments
• Single-Line comment:
 // This is a single-line comment
• Multiline comment:
 /* This comment goes on one line */
 /* This
is a
multiline comment */

Faculty of Information Technology - Computer Science Department 9


Comments
Examples
public class JavaApplication49 {

public static void main(String[] args) {


/* [Link]("hello to java#");
[Link]("public"); */

} }
package javaapplication49;
public class JavaApplication49 {

public static void main(String[] args) {


[Link]("hello to java#"); //
// [Link]("hello to java#");
[Link]("hello to java#"); //
}
}

Faculty of Information Technology - Computer Science Department 10


Identifiers
 Identifiers are names that identify the different elements of a Java application, such as
classes, methods, and variables.
 An identifier is a sequence of characters that consists of letters, digits, underscores ( _ ),
or dollar signs ($).
 Restrictions on identifiers:
1. An identifier CANNOT be a reserved word.
2. An identifier MUST start with a letter, It CANNOT start with a digit.
3. An identifier CANNOT contain spaces or special characters, EXCEPT
underscores ( _ ) or a Dollar sign ($).
4. An identifier can be of any length.

Faculty of Information Technology - Computer Science Department 11


Identifiers
Example:
• The following table contains a list of valid identifiers in the first column and a list of
invalid identifiers in the second one.
Invalid Identifier Valid Identifier
cAr<class cAr
public My_naMe
My name Hel$4
12new T_12
false ST_
#new nAME$
hello? public5
na(me Class
Faculty of Information Technology - Computer Science Department 12
Identifiers
Exercise
• In the table below, fill in the second column whether the identifier is valid or invalid.
Write down the reason, if invalid, in the third column.
Reason Valid/Invalid Identifier

main

Main

AV$$M

_Aa

A B

3_AB

AB13
Faculty of Information Technology - Computer Science Department 13
Variables and Data Types
• A variable is a location in memory that is to store a value of a certain datatype.
• Java is a statically-typed programming language.
• This implies that all the variables must be declared before they can be used.
• A variable is declared by specifying its name and the type of information that it holds
(data type).
In other words, a variable has:
• A Name.
• A Data type.
• The following syntax shows how variables are declared:
datatype varName;

Faculty of Information Technology - Computer Science Department 14


Variables and Data Types
• Data types in Java are classified into:
• Primitive Data Types:
• Numeric data types: byte, short, int, long, float, double.
• Character data type: char.
• Boolean data type: boolean.
• Non-primitive Data Type:
• String.

Faculty of Information Technology - Computer Science Department 15


Variables and Data Types
• Examples:
• int x; // Declare an integer variable x
• double radius; // Declare a variable of the type

// double whose identifier is radius


• char a; // Declare a character variable a
• boolean checked; // Declare a boolean variable checked
• Multiple variables of the same data type can be declared at once, as follows:
• int x, y, z;
• double m1, m2, m3, avg;

Faculty of Information Technology - Computer Science Department 16


Variables and Data Types
Java Numeric Data Types
 Every numeric data type has a range of values.
 The compiler allocates memory space for each variable, or constant, according to
its data type.
 Example:
 double n = 12.2;
 int x =12;
 double m = -92.0;
 float y = 3.14f;
 long n2 = 125L;

Faculty of Information Technology - Computer Science


17
Department
Assignment Operator

• An assignment statement designates a value for a variable.


• The equal sign () is used as the assignment operator.
• The syntax of using the assignment operator is as follows:
varName = value;

Examples:
• x = 1; // Assign 1 to x
• radius = 2.7; // Assign 2.7 to radius

Faculty of Information Technology - Computer Science Department 18


Assignment Operator
Notes:
• The compiler DOES NOT assign a default value to an uninitialized local variable.
• Be sure to assign a value to every variable you declare before you attempt to use it.
• Accessing an uninitialized local variable will result in a compile-time error.

Faculty of Information Technology - Computer Science Department 19


Assignment Operator
Example:
public class Main public class Main
{ {
public static void main(String[] args)
public static void main (String[]
{
args) {
int x = 12;
int x = 12;
double y;
double y;
y = 55.7;
y = 55.7;
double num;
long num;
[Link](num);
} }
} No Errors } This code contains an error

Faculty of Information Technology - Computer Science Department 20


Declaration with Initialization

• It is possible to assign a value to a variable when the variable is declared.


• This is referred to as Declaration with Initialization.
• It is also referred to as Initialization on Declaration.

Example:
• int x = 1;
• double radius = 2.7;
• int x = 8, y = 3, z = 23;
• double m1 = 78, m2 = 84.5, avg = 70.0;

Faculty of Information Technology - Computer Science Department 21


Constants (Final Variables)
 A constant is a variable that holds a permanent value.
 A value that never changes.
 Once a constant is initialized, its value CANNOT be changed.
 The syntax for declaring a constant :
final datatype constantName = value;
 Example:
final double p=3.14;
final int size = 3;
• Constants can be declared in two phases, as follows:
final float PI;
PI = 3.14f;

Faculty of Information Technology - Computer Science Department 22


List of Well-Known Constants
• Exercise:
• Express the following constants using Java.

Constant Symbol Value


Pi
Euler's number
Speed of light
Boiling pint of water
Days per week

Faculty of Information Technology - Computer Science Department 23

You might also like