Assignment 01
Assignment 01
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;
15.
import java.util.Scanner;
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.