Installing Java on Your Computer
Welcome to your very first Java ! Before we start writing programs, we
need to download the Java Development Kit (JDK), which lets you write
and compile Java code. Here's how to do it:
Step 1: Download the JDK
1. Open your internet browser and go to java.sun.com.
2. Find the Java SE link and click it.
3. Download the Standard Basic Edition JDK.
4. Double-click the executable file to start the wizard download.
Step 2: Set Up the Java Compiler
1. Click on "Computer" in your start menu and then click on your main
hard drive.
2. Click on "Program Files" and then "Java".
3. Click on the version of JDK you downloaded.
4. Click on "bin" and right-click on any of the files.
5. Click on "Properties" and copy the location.
6. Click on "My Computer" again and then "Properties".
7. Click on "Advanced System Settings" and then "Environment
Variables".
8. Create a new user variable named "path" with the value of the
location you copied.
9. Click "OK" and you now have your compiler set up.
Step 3: Test the Compiler
Open your command prompt by pressing "cmd" in your start menu.
Type "javac" and press enter.
If the compiler is set up correctly, a bunch of text will appear.
That's it! You now have Java installed on your computer and can write
Java programs.
,How to Run a Java Program
Before we dive into the syntax of Java programming, we need to know
how to run a program.
Step 1: Open an Editor
Open any editor of your choice. I will be using Notepad++ because it is my
favorite. Once you have your editor open, go to File > Save As. Save the
file as "youtube.java" in a location of your choice.
Step 2: Write a Simple Program
For the purpose of this , I will show you a simple "Hello World" program.
In Java, everything begins with a class. You cannot do anything until you
create a class. So, let's create a class called "Apples". Inside the class,
we will have a method called "main". This method is essentially a list of
things to do. Your computer will look for the method called "main" when it
runs a Java program.
After creating the class and method, we will instruct the computer to print
out "Hello, YouTube" by writing "System.out.println("Hello, YouTube");"
inside the main method.
Step 3: Compile the Program
Before we can run the program, we need to compile it. Compiling the
program means taking what we wrote in the "youtube.java" file and
changing it into something called a "class" file. Your computer can only
read files with the ".class" extension, not ".java".
To compile the program, open your command prompt and navigate to the
location where you saved the "youtube.java" file. Use the command "javac
youtube.java" to compile the program. This will create a new file called
"Apples.class".
Step 4: Run the Program
Now that we have a ".class" file, we can run the program using the "java"
command followed by the name of the class. In this case, the command is
,"java Apples". When you press enter, you should see "Hello, YouTube"
printed in the command prompt.
How to Download an IDE for Java Programming
how to download an Integrated Development Environment (IDE) to write
your Java code. While you can create a program in any text editor, using
an IDE made specifically for Java is often useful, especially for large
programs. The IDE we will be downloading is called Eclipse.
Steps to Download Eclipse IDE
1. Open your internet browser and go to eclipse.org
2. Click on the "Downloads" tab
3. Choose the appropriate download for your operating system
4. Once downloaded, extract the files from the zip folder
5. Double click on the Eclipse application file to run the program
Once Eclipse is running, create a new Java project and class to start
writing your code. One benefit of using an IDE like Eclipse is that it has a
compiler built-in, making it easier to run your code without using the
command line. Additionally, if there are any errors in your code, the IDE
will point them out and tell you where they are located.
With Eclipse, you can easily write and run your Java programs, making it
a powerful tool for Java programming.
In this , we will write a basic "Hello World" program in Java. Before we
get started, let me show you how to display line numbers in Eclipse,
which helps in identifying errors in your code. Click on Window >
Preferences > General > Editors > Text Editors and select "Show line
numbers".
Creating a Java Program
Everything in Java begins with a class. Inside the class, we will create
a method called "main". The method header is the name of the
method, and the method body contains the instructions on how to run
the program.
, public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Here, we have created a public class named "HelloWorld" and a
public static void main method inside the class. The "println" method
is used to print the string "Hello World!" to the console.
Click on the "Run" button to execute the program.
Introduction to Variables in Java
In this , we'll be discussing variables in Java. A variable in Java is
like a placeholder for something else, just like in math class where
variables are named x and set to a value like 73. When you use x in a
calculation or program, it takes on the value of 73. Variables in Java
work the same way. You assign a variable name to a value, and when
you use the variable, it represents that value.
Declaring and Assigning Variables in Java
To declare a variable in Java, you first need to tell Java what kind of
variable you're working with. There are different types of variables in
Java: integers, decimals, letters, and strings of characters. Once you
have decided on a type, you need to give it a name. In this , we'll be
working with a type called "double," which allows for decimal values.
To create a variable called "tuna," for example, you would write:
double tuna;
Next, you need to assign a value to the variable "tuna." You can do
this using the assignment operator, "=", like this:
tuna = 5.28;
Now you have a variable called "tuna" with the value of 5.28 that you
can use in any Java program.
Printing Variables in Java
To print a variable in Java, you can use the "System.out.print"
command and the name of the variable. For example:
System.out.print(tuna);
This will print the value of the variable "tuna" to the screen.