2 - Elementary Programming 1_Syntax, Comments, Variables
2 - Elementary Programming 1_Syntax, Comments, Variables
Part (1)
Modified from:
-W3Schools.com
-Introduction to Java Programming, Liang, 10 th Edition
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Outline
● Java Syntax.
● The main Method.
● Java Comments.
● Java Variables.
● Declaring Variables .
● Assignment Statements.
● Display Variables.
● Java Identifiers.
● Naming Conventions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
Objectives
● To write Java programs to perform simple computations .
● To use identifiers to name variables, constants, methods, and
classes .
● To use variables to store data.
● To program with assignment statements and assignment
expressions .
● To use constants to store permanent data.
● To name classes, methods, variables, and constants by
following their naming conventions.
● To perform operations using operators +, -, *, /, and % .
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
Java Syntax
● In the previous chapter, we created a Java file
called Welcome.java, and we used the following code to
print “Welcome to Java" to the screen:
Welcome.java
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Java Syntax (Cont..)
● Every line of code that runs in Java must be inside a
class. In our example, we named the class Welcome.
A class should always start with an uppercase first
letter.
● The name of the java file must match the class name.
When saving the file, save it using the class name and
add ".java" to the end of the filename.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
The main Method
● The main() method is required and you will see it in
every Java program:
public static void main(String[] args)
● Any code inside the main() method will be executed.
You don't have to understand the keywords before
and after main. You will get to know them bit by bit
during the course.
● For now, just remember that every Java program has
a class name which must match the filename, and that
every program must contain the main() method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
System.out.println()
● Inside the main() method, we can use the println()
method to print a line of text to the screen:
● Note: The curly braces {} marks the beginning and the end of a
block of code.
● Note: Each code statement must end with a semicolon.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Exercise:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Java Comments
● Comments can be used to explain Java code, and to make
it more readable. It can also be used to prevent execution
when testing alternative code.
– Single-line Comments
– Multi-line comments
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Single-line Comments
● Single-line comments start with two forward slashes (//).
● Any text between // and the end of the line is ignored by
Java (will not be executed).
● This example uses a single-line comment before a line of
code:
// This is a comment
System.out.println("Hello");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Multi-line comments
● Multi-line comments start with /* and ends with */.
● Any text between /* and */ will be ignored by Java.
● This example uses a multi-line comment (a comment
block) to explain the code:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
Java Variables
● Variables are containers for storing data values.
● In Java, there are different types of variables, for
example:
– String - stores text, such as "Hello". String values are
surrounded by double quotes
– int - stores integers (whole numbers), without decimals, such
as 123 or -123
– float - stores floating point numbers, with decimals, such as
19.99 or -19.99
– char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
– boolean - stores values with two states: true or false
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
Declaring (Creating) Variables
Syntax:
type variable;
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Declaring and Initializing in One
Step
● Syntax:
type variable = value;
int x = 1;
double d = 1.4;
float myFloatNum = 5.99f;
char myLetter = ‘S';
boolean myBool = true;
String myName = “Sara";
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Declaring and Initializing
in One Step
● if you assign a new value to an existing
variable, it will overwrite the previous value:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Display Variables
String name = “Sara";
System.out.println("Hello " + name);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Display Variables (Cont.)
● To combine both text and a variable, use
the + character.
● You can also use the + character to add a variable to
another variable.
● For numeric values, the + character works as a
mathematical operator.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
Declare Many Variables
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
Example: Computing the Area of a Circle
public class ComputeArea {
/* Main method */
public static void main(String[] args) {
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
animation
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
animation
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
animation
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
animation
// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Example
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28
Final Variables/Named Constants
● However, you can add the final keyword if you don't
want others (or yourself) to overwrite existing values
(this will declare the variable as "final" or "constant",
which means unchangeable and read-only):
final datatype CONSTANTNAME = VALUE;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
30
Naming Conventions
● Choose meaningful and descriptive names.
● Variables and method names:
– Use lowercase. If the name consists of several words,
concatenate all in one, use lowercase for the first
word, and capitalize the first letter of each
subsequent word in the name. For example, the
variables radius and area, and the method
computeArea.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
31
Naming Conventions (Cont..)
● Class names:
– Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.
● Constants:
– Capitalize all letters in constants, and use underscores
to connect words. For example, the constant PI and
MAX_VALUE
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
32
Exercise:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
33
Summary
● Java code must be written inside a class.
● The main() method is required in every Java program and serves as the
entry point of execution.
● System.out.println() is used to print text to the screen.
● Comments can be used to explain code and are either single-line (//) or
multi-line (/* */).
● Class names capitalize the first letter of each word, while constants are
written in all capitalized letters .
● Java supports variables for storing different types of data, such as
strings, integers, floating-point numbers, characters, and boolean values.
● All Java variables must have unique names called identifiers.
● Choosing meaningful and descriptive names for variables and methods
enhances code readability.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
34
References
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
35