Austria, Christian Paul A.
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
Examples of Invalid Identifiers:
1stNumber (starts with a digit)
total-amount (contains a hyphen)
class (reserved keyword)
my variable (contains a space)
Example in Code:
public class Exam pleClass {
public static void m ain(String[] args) {
int age = 25; // "age" is an identifier for an integer variable.
String name = "John"; // "nam e" is an identifier for a String variable.
System.out.println("Nam e: " + nam e);
System.out.println("Age: " + age);
}
}
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 - These represent a single character enclosed in single quotes.
Example: 'A', '1', '@'
- Character literals can also represent special characters using escape sequences, like '\n' for a
newline.
String Literals - These are sequences of characters enclosed in double quotes.
Example: "Hello, World!", "12345", "Java Programming"
Boolean Literals - These represent the two boolean values: true and false.
Example: true, false
Null Literal:
Represents the absence of a value for reference types.
Example: null
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
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 Em ployed: " + isEm ployed);
System.out.println("Address: " + address);
}
}
3. Java Data Types and Variables
Data Types
In Java, a data type defines the type of data that a variable can hold. Java is a statically-typed
language, meaning you must declare the data type of a variable when you define it.
Categories of Data Types:
1. Primitive Data Types - These are the most basic data types in Java, predefined by the language itself.
o Integer Types:
byte: 8-bit signed integer, range: -128 to 127
short: 16-bit signed integer, range: -32,768 to 32,767
int: 32-bit signed integer, range: -2^31 to 2^31-1
long: 64-bit signed integer, range: -2^63 to 2^63-1
o Floating-Point Types:
float: 32-bit floating-point number
double: 64-bit floating-point number
o Character Type:
char: 16-bit Unicode character
o Boolean Type - boolean: Represents two values: true or false
2. Reference Data Types - These are used for objects and arrays. They refer to memory locations where data is
stored, unlike primitive types which hold the actual values.
Examples:
String
Arrays (int[], String[])
Classes (MyClass, Object)
Interfaces
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
Declared inside a method, constructor, or block.
Accessible only within the method or block where it is declared.
Instance Variables
Declared inside a class but outside any method, constructor, or block.
Each instance of the class (i.e., object) has its own copy of the instance variable.
Static Variables
Declared with the static keyword inside a class.
Shared among all instances of the class, meaning there is only one copy of a static variable
for all objects.
Example in Code:
public class DataTypeExam ple {
// Instance variable
int instanceVar = 5;
// Static variable
static String staticVar = "Hello, W orld!";
public static void m ain(String[] args) {
// Local variable
int localVar = 10;
// Prim itive data types
int age = 25; // Integer data type
double salary = 50000.75; // Floating-point data type
char grade = 'A'; // Character data type
boolean isActive = true; // Boolean data type
// Reference data type
String name = "John Doe"; // String is a reference type
// 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);
}
}