Programming Assignment
Programming Assignment
BS IT 21013
1. Java Identifiers
In Java, an identifier is a name used to identify a variable, method, class, or any other user-defined
item. Identifiers are essentially the names you assign to these elements in your code.
Examples of Valid Identifiers:
myVariable
_totalAmount
employeeName
$salary
number1
Example in Code:
2. Java Literals
In Java, a literal is a fixed value that is directly written in the code and represents a constant value.
These are used to assign values to variables.
Types of Java Literals:
Integer Literals:
These are numeric values without any fractional or decimal part.
Example: 10, -100, 0
They can be written in various number systems:
Decimal (base 10): 123
Binary (base 2): 0b1010 (represents 10 in decimal)
Octal (base 8): 0123 (represents 83 in decimal)
Hexadecimal (base 16): 0x7B (represents 123 in decimal)
Floating-Point Literals - These are numeric values with a fractional or decimal part. Example: 3.14, -0.01, 2.5e3 (which
is 2.5 × 10³ or 2500.0)
- Character literals can also represent special characters using escape sequences, like '\n' for a
newline.
Example in Code:
public class LiteralExam ple {
public static void m ain(String[] args) {
int age = 30; // Integer literal
double salary = 45000.50; // Floating-point literal
char grade = 'A'; // Character literal
String nam e = "John Doe"; // String literal
boolean isEm ployed = true; // Boolean literal
String address = null; // Null literal
Variables
A variable in Java is a container that holds data that can be changed during the execution of a
program. Each variable must be declared with a data type, which determines the type of data it
can store.
Types of Variables:
Local Variables
Instance Variables
Example in Code:
// Static variable
static String staticVar = "Hello, W orld!";
// Printing variables
System.out.println("Nam e: " + nam e);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Active: " + isActive);
System.out.println("Static Var: " + staticVar);
System.out.println("Local Var: " + localVar);
}
}