Java Programming 2nd Periodical Test NOTES
Java Programming 2nd Periodical Test NOTES
• 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
Example:
Create a variable called name of type String and assign it the value "John":
String name = "John"; Declaration of variable
System.out.println(name);
Try it to yourself:
https://www.w3schools.com/java/tryjava.asp?filename=demo_variables
Example:
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15; Declaration of variable
System.out.println(myNum);
Try it to yourself:
https://www.w3schools.com/java/tryjava.asp?filename=demo_variables_int
Example
Change the value of myNum from 15 to 20:
int myNum = 15; Declaration of variable
System.out.println(myNum);
Try it to yourself:
https://www.w3schools.com/java/tryjava.asp?filename=demo_variables_int2
Display Variables
The println() method is often used to display variables.
Example:
https://www.w3schools.com/java/tryjava.asp?filename=demo_variables2
Example
1|Pa ge
Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.
2nd Quarter – Week 5 (Day 1 and 2)
String firstName = "John ";
System.out.println(fullName);
Example
int x = 5;
int y = 6;
int y = 6;
int z = 50;
System.out.println(x + y + z);
System.out.println(x + y + z);
2.4 Identifiers
Identifiers are the names that identify the elements such as classes, methods, and
variables in a program.
All Java variables must be identified with unique names. These unique names are
called identifiers. Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume).
2|Pa ge
Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.
2nd Quarter – Week 5 (Day 1 and 2)
Example:
// Good
int m = 60;
After a variable is declared, you can assign a value to it by using an assignment statement. In
Java, the equal sign (=) is used as the assignment operator. The syntax for assignment statements
is as follows:
variable = expression;
An expression represents a computation involving values, variables, and operators that, taking them together,
evaluates to a value. For example, consider the following code:
3|Pa ge
Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.
2nd Quarter – Week 5 (Day 1 and 2)
double area = radius * radius * 3.14159; // Compute area
You can use a variable in an expression. A variable can also be used in both sides of the = operator.
For example,
x = x + 1;
Make sure that you choose descriptive names with straightforward meanings for the variables, constants,
classes, and methods in your program. As mentioned earlier, names are case sensitive. Listed below are the
conventions for naming variables, methods, and classes.
■ Use lowercase for variables and methods. If a name consists of several words, concatenate them into one,
making the first word lowercase and capitalizing the first letter of each subsequent word—for example, the
variables radius and area and the method print.
■ Capitalize the first letter of each word in a class name—for example, the class names ComputeArea and
System.
■ Capitalize every letter in a constant, and use underscores between words—for example, the constants PI and
MAX_VALUE. It is important to follow the naming conventions to make your programs easy to read.
Caution
Do not choose class names that are already used in the Java library. For example, since the System class is
defined in Java, you should not name your class System.
The value of a variable may change during the execution of a program, but a named constant,or simply
constant, represents permanent data that never changes. In our ComputeArea program, p is a
constant. If you use it frequently, you don’t want to keep typing 3.14159; instead, you can declare a
constant for p. Here is the syntax for declaring a constant: final datatype CONSTANTNAME = value;
A constant must be declared and initialized in the same statement. The word final is
a Java keyword for declaring a constant. For example, you can declare p as a constant and
rewrite Listing 2.1 as in Listing 2.4.
4|Pa ge
Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.
2nd Quarter – Week 5 (Day 1 and 2)
5|Pa ge