Java makeup
23/08313
JACOBSON ETEMESI
INPUT
public class PersonalInfo {
public static void main(String[] args) {
String name = "Your Name"; // Replace with your actual name
String birthday = "YYYY-MM-DD"; // Replace with your actual birthday
String hobbies = "Reading, Coding, Hiking"; // Replace with your hobbies
String favoriteBooks = "Book1, Book2, Book3"; // Replace with your favorite
books
[Link]("Name: " + name);
[Link]("Birthday: " + birthday);
[Link]("Hobbies: " + hobbies);
[Link]("Favorite Books: " + favoriteBooks);
}
}
OUTPUT
Name: Your Name
Birthday: YYYY-MM-DD
Hobbies: Reading, Coding, Hiking
Favorite Books: Book1, Book2, Book3
=== Code Execution Successful ===
The String variable s is declared but not initialized. When you try to print it, Java
will throw a compilation error because of trying to use a variable that has not been
given a value.
This results in a compilation error: "variable s might not have been initialized".
The String variable s is explicitly initialized using new String(). This creates an
empty String object.
When you print it, it will output "s = " followed by an empty line, as the string
contains no characters.
k
i
INPUT
n
g
f
o
public class DoWhileExample {
public static void main(String[] args) {
int i = 5;
[Link]("Starting the do-while loop:");
do {
[Link]("Current value of i: " + i);
i--; // Decrement i in each iteration
} while (i > 0); // Condition to continue the loop
[Link]("Loop finished.");
}
}
OUTPUT
Starting the do-while loop:
Current value of i: 5
Current value of i: 4
Current value of i: 3
Current value of i: 2
Current value of i: 1
Loop finished.
=== Code Execution Successful ===
1 Method Overloading
class OverloadingExample {
// Method with one parameter
void display(int number) {
[Link]("Displaying an integer: " + number);
4 }
// Method with two parameters
void display(int number, String message) {
[Link]("Displaying an integer with a message: " + number + " - " +
message);
}
// Method with a different parameter type
void display(double value) {
[Link]("Displaying a double: " + value);
}
public static void main(String[] args) {
OverloadingExample example = new OverloadingExample();
[Link](10); // Calls method with one parameter
[Link](20, "Hello!"); // Calls method with two parameters
[Link](15.5); // Calls method with different parameter type
}
}
OUTPUT
Displaying an integer: 10
Displaying an integer with a message: 20 - Hello!
Displaying a double: 15.5
=== Code Execution Successful ===
Method Overriding INPUT
class Parent {
// Parent class method
void showMessage() {
[Link]("Message from Parent class");
}
}
class Child extends Parent {
// Child class overriding the parent's method
@Override
void showMessage() {
[Link]("Message from Child class");
}
}
public class OverridingExample {
public static void main(String[] args) {
Parent parentInstance = new Parent();
[Link](); // Calls Parent class method
Parent childInstance = new Child();
[Link](); // Calls Child class method (runtime polymorphism)
}
}
1
The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode.
It acts as an intermediary between Java programs and the host system's hardware.
T
Importance of JVM:
Multithreading Support: Efficiently runs concurrent tasks
Security: Enforces strict security for safer code execution.