Learning Java in Windows
In our previous blog, we learned how to download and install Java on Linux systems in order to run our first Java program. Today, let’s bring the Windows users to the party!
In Windows, this could be done in 6 simple steps.
- Download and install the Java Development Kit
Simply search for “JDK download” and go to the Oracle website that will look like below:
After clicking on the “Java SE 12.0.1” link, you will be directed to the following page where you’ll have to choose your OS. In this case, it’s Windows. Make sure you accept the license agreement first!
After downloading and executing the installer, you can proceed to download and install the IDE of your choice and begin coding. But for text editors, you will have to add the JDK to your “Environment Path Variables” in order to conveniently run Java programs.
Don’t stress about IDEs and text editors. We’ll get to that in a bit.
It should finally look like this:
Now that your system knows what Java is, how can you write your programs?There are so many options available to Windows users that come in different forms, mainly IDEs and text editors.
Examples of IDEs are NetBeans, Eclipse, MS Visual Studio, IntelliJ IDEA and so on. Examples of text editors are Notepad++, Atom, Sublime Text, Vim and the list goes on and on.
Most IDEs have their own compiler so they can compile java programs. Running a java program does not require a JDK, developing and compiling does though.
- Downloading and Installing NetBeans IDE
Simply google “netbeans download” and open the first link to the official NetBeans website. Here you’ll be able to download the latest version preferably the one with all features.
- Create a new project
After installation, open the idea and start a new project. You will need to specify that it’s a Java project first:
Name your project appropriately. The IDE will proceed to open the project from where you can begin coding.
- Write your first Java program
Type the following code (we will explain what each line means in a sec) :
import java.util.Scanner; public class AreaofCircle { public static void main(String[] args) { Scanner s= new Scanner(System.in); System.out.println("Enter the radius:"); double r= s.nextDouble(); double area=(22*r*r)/7 ; System.out.println("Area of Circle is: " + area); } }
The code will look like this in NetBeans;
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –LIVE A LITTLE
My boss told me to have a good day…. So I went home.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
- Run the program
Now, let’s run the application. Click the “play” button on the palette. NetBeans will execute the application and show the output in the Console view where you’ll be prompted to enter the radius:Upon entering the radius, the program will return the radius of the circle as below:
- Here are some explanations of what the code does.
// Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Project1 is the name of the package.
package areaofcircle;
// java.util.Scanner means you are in need of scanner method from utility which contains set of predefined utility classes. The scanner is used for taking input from user.
import java.util.Scanner;
//This is the start of our program. A public class is accessible by all the java code, even outside the package.
public class AreaofCircle {
/*The main method where our program begins. Program cannot execute without this method. public – access specifier means from everywhere we can access it static – access modifier means we can call this method directly using class name without creating an object of it void – it returns nothing main – method name string [] args – in java accept only string type of argument and stores it in a string*/
public static void main(String[] args) {
/*Below statement we create an object of a Scanner class which is define in import java.util.scanner package. Scanner class allows user to take input form console. System.in is passed as parameter in scanner class will tell to java compiler system input will be provided through console (keyboard). */
Scanner s= new Scanner(System.in);
//Prints out the prompt
System.out.println(“Enter the radius: “);
// nextDouble() allows you to enter some text and it will be parsed into a double variable called r.
double r= s.nextDouble();
//variable area stores the computation that derives the area of a circle
double area=(22*r*r)/7 ;
//prints out the result
System.out.println(“Area of Circle is: ” + area);
//End of main method
}
//End of class
}
Notice how we wrote the explanations? That’s how you write comments in Java programs to better understand your code.
And that is your Java program!
Here’s the list of activities that you went through as you took your first steps in Java programming:
- Download and install the Java JDK
- Download and install the NetBeans IDE
- Create a new project
- Write the program
- Run the program
- Explanations
Congratulations, you got this far! Coding isn’t for everybody, you know what I mean? As you have realized, this was probably just the tip of the iceberg. There’s so much you can do! In the next blog, let’s find out the size of the scope of things you can create in Java.
PS: it’s endless.