Java: Installation and First program!

Java: Installation and First program!

Hello there,

To start with, I would like to apologize for the delay in writing this post. It was due to circumstances beyond my control. Never the less, I would like to mention here that I'll be posting a new topic in the Java and Python series on weekends.

THIS POST WILL COVER.

*Java Installation

*Setting the PATH: Environment variable

*Tools

*First java program

INSTALLATION.

Downloading

In order to program in Java, one needs to have the Java Development Kit JDK running on their machine. Download JDK from Oracle.
JDKs for Windows, OS X, and Linux platforms are available there.

JDK installation instructions.

After downloading the installer (bin.exe or bin.zip) file run along with the installation by just clicking next. Nothing to customize here.

Setting the PATH environment variable

The PATH environment variable on your computer indicates which directories the computer searches when looking for applications, such as the applications that enable you to compile and run your Java applications (called javac and java, respectively). Carefully follow the installation instructions for Java on your platform to ensure that you set the PATH environment variable correctly. The steps for setting environment variables differ by the operating system and sometimes by operating system version (e.g., Windows 7 vs. Windows 8).

My focus will be setting up on Windows. For other OSs kindly google or docs.oracle for installation instructions.

JDK Installation JAVA_HOME and the bin Subdirectory(CLASS_PATH)

JAVA_HOME

32-bit JDK on Windows:

C:\Program Files (x86)\Java\jdk1.8.0_201

64-bit JDK on Windows:

C:\Program Files\Java\jdk1.8.0_201

CLASS_PATH

32-bit JDK on Windows:

C:\Program Files (x86)\Java\jdk1.8.0_201\bin

64-bit JDK on Windows:

C:\Program Files\Java\jdk1.8.0_201\bin

Navigate to the directories as above on your machine, then if you're using Windows 8.1 like me, click on the Windows Home icon,click System, Advanced System Setting andEnvironment Variables. Copy the file directories as shown above and paste them into the appropriate fields as below.

System.jpg

SysProperties.jpg

SysPropertiesII.jpg

SysPropertiesIII.jpg

Checking if Java is Successfully installed.

in CMD type: java -version

CMD.jpg

That means java was installed successfully.

TOOLS

Text Editors (Are tools that enable a programmer to write code) There a number of text editors to choose from such as Sublime Text, Notepad++, VScode, Atom etc (Just google "download[text editor name]")

Java Integrated Development Environments (IDEs)

There are many Java integrated development environments that you can use for Java programming. IDEs provide tools that support the software development process, such as editors, debuggers for locating logic errors (errors that cause programs to execute incorrectly), and more.

For this reason, I'll use only the JDK command-line tools for most of the examples.

You can download the JDK/NetBeans bundle from:

netbeans.org/downloads

Eclipse Downloads

You can download the Eclipse IDE from: eclipse.org/downloads

IntelliJ IDEA Community Edition Downloads

You can download the free IntelliJ IDEA Community Edition from:

jetbrains.com/idea/download/index.

First java program!

In your text editor, hoping you have downloaded one. Type the code below.

1   //  Welcome1.java
2   // Text-printing program.
4  public class Welcome1
5  {
6    //  main method begins execution of Java application
7 public static void main(String[] args)
8  {
9   System.out.println("Welcome to Java Programming!");
10  }   //  end method main
11 }  //  end class Welcome1

Program parts

The java file ends with the extension .java. So our java file should be saved as Welcome1.java

NOTE: Java class (files) should have the first letter Capitalised (In short java is case sensitive,welcome1 is NOT Welcome1).

public keyword- is an access specifier- means the class can be accessed by objects and methods within and by other classes, etc class -keyword- every java program should have at least one class declaration. *class body- is written within the {} left and right braces.

Our program has the main() method. This is the method that enables our java files to run. Every java program should have a main method.

public static void main(String[] args){}

static keyword - means the method can only be used by this class Welcome1.java.

void keyword- means the method can not return any value.

main- is the name of the method- A method performs specified actions.

String- is a type for string characters. [] - means an array of String type- args is the name of the array.

Performing Output

System.out.println("Welcome to Java Programming.");

System is a class - out is the object that calls System's println() method to output the string of characters ("Welcome to Java Programming.")

; Semicolon- all java statements end with a semicolon.

// end main method - this is a comment by the programmer. The java compiler and JVM - java Virtual machine does not look at characters like that. / Multiline comment*/ in java. Can be used if we have many lines of comments.

Having considered the above, we can now try to run our program.

In CMD type: javac Welcome1.java in the directory of the file that is:

C:\Users\lab-admin\Desktop\Blogposts\Java\code>javac Welcome1.java (in my case).

javac.png

javac - tells the java compiler to compile the file Welcome.java If the program is well written a .class file Welcome1.class will be produced.

The .class file contains the bytecode that the JVM runs.

Now in CMD type: java Welcome

You should see:

Welcome to Java Programming. That's it!!

Printing multiple lines

1   // MultipleLines.java
2  // Printing multiple lines with multiple statements.
3 public class MultipleLines
4 {
5    public static void main(String[] args)
6    {
7    System.out.print("Java programming\n is\ngreat\nfun!");
8    
9     }
10 }

The output should be:

Java programming

is

great

fun!

The \ backslash is treated as an escape character hence when combined with 'n' makes an escape sequence that represents a newline to the print method. So println() means print with a newline.

Until next time, Thank you.