0% found this document useful (0 votes)
61 views5 pages

Java Programming 2nd Periodical Test NOTES

This document discusses variables in Java. It defines variables as representations of values that can change in a program. It describes different variable types like String, int, float, char, and boolean. Examples are given to declare variables of each type and assign values. The document also discusses identifiers, naming conventions, data types, assignment statements, and named constants. It provides rules for naming variables and examples of declaring and assigning values to variables in Java.

Uploaded by

Throw Away
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
61 views5 pages

Java Programming 2nd Periodical Test NOTES

This document discusses variables in Java. It defines variables as representations of values that can change in a program. It describes different variable types like String, int, float, char, and boolean. Examples are given to declare variables of each type and assign values. The document also discusses identifiers, naming conventions, data types, assignment statements, and named constants. It provides rules for naming variables and examples of declaring and assigning values to variables in Java.

Uploaded by

Throw Away
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.

2nd Quarter – Week 5 (Day 1 and 2)


2.5 Variables
Variables are used to represent values that may be changed in the program.
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

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

myNum = 20; // myNum is now 20

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.

To combine both text and a variable, use the + character:

Example:

String name = "John";

System.out.println("Hello " + name);

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

String lastName = "Doe";

String fullName = firstName + lastName;

System.out.println(fullName);

Try it yourself: https://www.w3schools.com/java/tryjava.asp?filename=demo_variables3

Example
int x = 5;

int y = 6;

System.out.println(x + y); // Print the value of x + y

Java Declare Multiple Variables


To declare more than one variable of the same type, you can use a comma-separated list:
Example:
instead of writing.
int x = 5;

int y = 6;

int z = 50;

System.out.println(x + y + z);

You can simply write:


int x = 5, y = 6, z = 50;

System.out.println(x + y + z);

Try it to yourself: https://www.w3schools.com/java/tryjava.asp?filename=demo_variables5

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

Note: It is recommended to use descriptive names in order to create understandable and


maintainable code:

2|Pa ge
Computer Teacher: Mr. Mhuddy T. Dagatan, LPT.
2nd Quarter – Week 5 (Day 1 and 2)
Example:

// Good

int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is

int m = 60;

The general rules for naming variables are:

• Names can contain letters, digits, underscores, and dollar signs


• Names must begin with a letter
• Names should start with a lowercase letter and it cannot contain whitespace
• Names can also begin with $ and _ (but we will not use it in this tutorial)
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be used as names

Java Data Types


As explained in the previous chapter, a variable in Java must be a specified data type:

int myNum = 5; // Integer (whole number)

float myFloatNum = 5.99f; // Floating point number

char myLetter = 'D'; // Character

boolean myBool = true; // Boolean

String myText = "Hello"; // String

Try it yourself: https://www.w3schools.com/java/tryjava.asp?filename=demo_data_types

2.6 Assignment Statements and Assignment


An assignment statement designates a value for a variable. An assignment statement can be used as an
expression in Java.

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:

int y = 1; // Assign 1 to variable y


double radius = 1.0; // Assign 1.0 to variable radius
int x = 5 * (3 / 2); // Assign the value of the expression to x
x = y + 1; // Assign the addition of y and 1 to x

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;

2.8 Naming Conventions


Sticking with the Java naming conventions makes your programs easy to read and avoids errors.

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.

2.7 Named Constants


A named constant is an identifier that represents a permanent value.

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)

There are three benefits of using constants:


(1) you don’t have to repeatedly type the same value if it is used multiple
times;
(2) if you have to change the constant value (e.g., from 3.14 to 3.14159
for PI), you need to change it only in a single location in the source code;
and
(3) a descriptive name for a constant makes the program easy to read.

5|Pa ge

You might also like