0% found this document useful (0 votes)
8 views

Assignment 01

Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 01

Hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

The Java platform is a versatile and widely used computing platform that provides a runtime
environment and a comprehensive set of libraries and tools for developing and running software
applications. The heart of the Java platform is the JVM. It is an integral part of the Java Runtime
Environment (JRE) and is responsible for executing Java bytecode. The JVM translates Java source code
into machine-specific instructions, making it possible for Java applications to be cross-platform. Java
programs are typically compiled into bytecode, which can be run on any system with a compatible JVM.
This "write once, run anywhere" capability is a major selling point of Java and makes it ideal for cross-
platform development.
2. Another component of the JVM is the bytecode verifier. Its job is to ensure that bytecodes are valid and
do not violate Java’s security restrictions. This feature helps to prevent Java programs arriving over the
network from damaging our system.
3. The attributes that contribute to the Java platform's independence
Bytecodes:
Bytecodes are not machine language binary code but platform-independent instructions. They are
independent of any microprocessor or hardware platform. Bytecodes need an interpreter (JVM) to convert
them into machine code that the underlying microprocessor can understand.
JVM (Java Virtual Machine):
The JVM is a crucial part of the Java Development Kit (JDK) and the foundation of the Java platform. It
can be installed separately or as part of the JDK. A JVM is a virtual machine that simulates a computer,
hiding the underlying operating system and hardware from the programs interacting with it. The JVM is
responsible for making Java a portable language by interpreting and executing bytecode. It allows the
same bytecode to be executed on any platform that contains a compatible JVM.
4. Java: The "java" command is used to execute Java applications. It is used to run Java programs after
they have been compiled into bytecode.
Javac: The "javac" command is used to compile Java source code into bytecode. It is the Java compiler.
5. The main method should be declared as static. This is because it needs to be accessible without creating
an instance of the class. When the Java program starts, there is no object of the class yet, and it would be
impossible to create one just to call the main method. Making it static allows the method to be called
directly on the class itself, without an instance.
6. Primitive types are handled by value – the actual primitive values are stored in variable and passed to
methods.
int x = 10;
public MyPrimitive(int x) { }

Non‐primitive data types (objects and arrays) are handled by reference – the reference is stored in variable
and passed to methods.
Box b = new Box(1,2,3);
public MyNonPrimitive(Box x) { }
7. Some examples of local variable inference:
int var = 1;
var myArray = new int[10];
var str = “This is a string”;
8. Java virtual machine:
It is a part of the JDK and the foundation of the Java platform. It can be installed separately or with JDK.
It is the JVM that makes Java a portable language. The same bytecodes can be executed on any platform
containing a compatible JVM. The JVM is invoked by the java command.
Java source code is compiled into bytecode, which is a platform-independent set of instructions. The
bytecode can be executed on any platform that has a JVM for that platform, whether it's running on a
Windows PC, a macOS machine, or a Linux server. This abstraction of the hardware and operating system
makes Java truly architectural neutral.
9. True.
Java source code is written in plain text files with the .java extension. To run the program, the Java
compiler is used to compile the source code into bytecode. Bytecode is a platform-independent,
intermediate representation of the program.
The compiled bytecode is executed by the Java Virtual Machine (JVM). The JVM is responsible for
interpreting the bytecode and converting it into machine-specific instructions or, in some cases, using a
Just-In-Time (JIT) compiler to further optimize the code.
So, to run a Java program, it must need first compile with the javac compiler to produce bytecode, and
then you use the java command to execute the program using the JVM.

11. Bytecodes:
Bytecodes are not machine language binary code but platform-independent instructions. They are
independent of any microprocessor or hardware platform. Bytecodes need an interpreter (JVM) to convert
them into machine code that the underlying microprocessor can understand.
JVM (Java Virtual Machine):
The JVM is a crucial part of the Java Development Kit (JDK) and the foundation of the Java platform. It
can be installed separately or as part of the JDK. A JVM is a virtual machine that simulates a computer,
hiding the underlying operating system and hardware from the programs interacting with it. The JVM is
responsible for making Java a portable language by interpreting and executing bytecode. It allows the
same bytecode to be executed on any platform that contains a compatible JVM.
12.
There is no performance differences between the two import statements:
1. import java.util.Scanner;: This statement specifically imports the Scanner class from the
java.util package.
2. import java.util.*;: This statement is a wildcard import that imports all classes and sub-packages
within the java.util package. It includes the Scanner class as well.
In terms of performance, both import statements have negligible impact. The Java compiler, during the
compilation process, resolves the references to the imported classes and ensures that they are available.
The wildcard import statement import java.util.*; may import more classes and sub-packages than you
need, which could marginally affect compilation times, but this impact is typically very minimal.

13.
public class CelsiusToKelvin {
public static void main(String[] args) {
double celsius = 73.0;
final double CELSIUS_TO_KELVIN = 273.0;
double kelvin = celsius + CELSIUS_TO_KELVIN;
System.out.println("Temperature in Kelvin: " + kelvin);
}
}
14.
import java.util.Scanner;

public class CircleAreaPerimeter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
scanner.close();
double pi = Math.PI; // Value of pi
double perimeter = 2 * pi * radius;
double area = pi * radius * radius;
System.out.println("Perimeter of the circle: " + perimeter);
System.out.println("Area of the circle: " + area);
}
}

15.
import java.util.Scanner;

public class InputID {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter your ID: ");
String userID = scanner.nextLine();
scanner.close();
System.out.println("Your ID is: " + userID);
}
}
16.
While Loop: In a while loop, the loop condition is evaluated before the loop body is executed. If the
condition is initially false, the loop body may not execute at all.
Do-While Loop: In a do-while loop, the loop body is executed at least once, and then the condition is
checked. If the condition is false, the loop may terminate after the first iteration.
17.
1. Immutability:
• String is immutable, meaning that once a String object is created, its value cannot be
changed. Any operation that appears to modify a String actually creates a new String
object.
• StringBuffer and StringBuilder are mutable, allowing you to change their contents
without creating new objects.
2. Thread Safety:
• StringBuffer is thread-safe and synchronized, making it safe for use in multi-threaded
environments where multiple threads may access and modify the same StringBuffer
instance.
• StringBuilder is not thread-safe, so it is more suitable for single-threaded environments.
However, it offers better performance compared to StringBuffer in single-threaded
scenarios.
3. Performance:
• In a single-threaded environment, StringBuilder is generally faster than StringBuffer
because it lacks the synchronization overhead.
• The + operator for string concatenation internally uses StringBuilder or StringBuffer,
making them more efficient than repeated string concatenation using String.
4. Best Practices:
• For string manipulations in a non-multi-threaded environment where performance is a
concern, use StringBuilder.
• In multi-threaded environments where thread safety is required, use StringBuffer.
• Use String when you have static, unchanging text data or when you need an immutable
representation.

18.
If you need to perform thread-safe operations with faster performance for operations involving negligible
modifications to a variable, you should use the StringBuilder class. StringBuilder is a mutable class, like
StringBuffer, but it is not synchronized. This makes it suitable for single-threaded or situations where you
can ensure thread safety using other means (e.g., synchronized blocks) while benefiting from better
performance.
19.There is an error in the following code segment in line 3. “hello world” is the correct syntax.

You might also like