How Java Works
Sun Microsystems first introduced Java, a high-level, object-oriented programming
language, in 1995. Java code can run on any computer or operating system that has a Java
Virtual Machine (JVM) installed because of its well-known platform freedom.
Java code is first converted into bytecode, a platform-independent format that can be run
by a JVM, when it is written. The JVM then interprets the bytecode at runtime, converting it
into machine code that the CPU of the computer may use to operate.
Classes, which are collections of related code that may be reused and shared across various
portions of the programme, are how Java programmes are organised.
Classes contain variables, which are data values that may be used by the methods, and
methods, which are discrete chunks of code that carry out duties.
A built-in memory management mechanism in Java also takes care of the automatic
allocation and deallocation of memory for programme objects. By doing so, manual memory
management is made less likely to result in memory leaks and other problems.
Overall, Java is a strong and adaptable programming language that is frequently utilised in a
wide range of applications, including enterprise software, mobile app development, and
web development. It is a popular option for developers all over the world because to its
platform freedom and powerful capabilities.
How to Setup Java Development Kit (JDK) Setup
1. You can use the instructions below to install the Java Development Kit (JDK):
2. On the Oracle website, get the JDK:
3. Visit the Oracle website (https://www.oracle.com/java/technologies/downloads/) to
get the downloads.
4. Choose the correct JDK version for your operating system, then download it.
5. Set up the JDK:
6. Launch the installer tool you downloaded and adhere to the installation guidelines.
7. Make the environment variables available:
, 8. Set the PATH variable in the environment variables to contain the path to the bin
directory for the JDK. The Java compiler (javac) and Java runtime (java) commands
can thus be used from any directory in the command prompt thanks to this.
9. Set the JDK installation directory as the value for the JAVA HOME variable.
Installer verification:
To make that the JDK is correctly installed, open a command prompt and execute "java -
version".
Installing an integrated development environment (IDE), such as Eclipse or IntelliJ IDEA, is an
optional step that can make creating Java programmes more user-friendly and effective.
After completing these steps, you should be prepared to begin utilising the JDK to create
Java applications.
First Code in Java
Here is an example of a basic "Hello World" program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Let's dissect this code:
"HelloWorld" is a public class that is declared in the first line. Every Java application must
contain at least one class, and the class name and filename must coincide (in this case,
"HelloWorld.java").
We define a public static method called "main" inside the class. The program's entry point is
where execution starts. If necessary, we can pass command-line arguments to the
programme using the "String[] args" option.
The string "Hello World!" is printed to the console using the "System.out.println" method
inside the main method.
, This programme can be run by saving it as a file named "HelloWorld.java," compiling it with
the command "javac HelloWorld.java," and then launching it with the command "java
HelloWorld". You ought to notice the message. "Hello World!" printed to the console.
Variables in Java
A variable in Java is a named memory region where a value of a certain data type is kept. In
Java, you use the following syntax to declare a variable:
data_type variable_name;
Thus, "data type" denotes the kind of information the variable can hold (such as an int,
double, boolean, etc.), and "variable name" is the name you give the variable. For instance,
the code below would be used to declare an integer variable called "myInt":
int myInt;
Moreover, when a variable is declared, it can be initialised as follows:
data_type variable_name = initial_value;
For instance, the following code would be used to declare and create a double variable
named "myDouble" with the number 3.14:
double myDouble = 3.14;
The assignment operator "=" can be used to change a variable's value after it has been
declared.
variable_name = new_value;
For instance, the following code would be used to give the "myInt" variable a new value of
10.
myInt = 10;
Variable declaration and initialization can also be combined into a single statement:
data_type variable_name = expression;