Java Programming Tutorial | Introduction | Lesson #1

in #programming6 years ago

Java-duke.jpg
Image Source
Hello Steemians, This is an introduction for those who want to learn Java.

I will focus more on the langauge for now because I believe there are those who also wan't to learn but don't have a clue what a programming langauge is.

What is Java


Download-Java-For-Mac-and-Windows-free.png
Image Source

Brief history of Java


Most people view Java as a programming language for the internet applications. However Java can be a stand-alone program without the reference of the internet. The history goes back to 1991 when James Gosling and his team at Sun Microsystem began designing the first java application for home appliances like televisions and toasters. This language was designed to work on different processors.

Compiling and running a program


FARhm.png
Image Source
The language a computers can understand is called machine language or machine code. When you run a program written in high-language, which is human-readable source code like mycode.java, you first need to compile it before you can run it. The compiler translates codes written in high-language into byte code (mycode.class)

There's a special interpreter that translates your byte code into machine language, this interpreter is called Java Virtual Machine a.k.a JVM. It is the JVM that runs your java code.

Getting Java


Installing the Java SE Development Kit (JDK)


The Java Development kit is available for free from the Oracle Website
Install the latest version which is version 9 at the writting of this.

Click here for the Installation Documentation guide.

You can write your codes using a text editor such as:
Sublime Text
Notepad++

Intergrated Development Kit or IDE's can save you some time from coding everything from scratch when you familiar with programming. As a begineer I would recommend using a text editor to how to write the structure of a class from a scratch. IDE's such as Netbeans Eclipse BlueJ can be downloaded for free.

Java program is written as pieces of code in a single file ending with .java

class HelloWorld {
This is where you write your codes inside these curly brackets
}

The class name should be same as your file name ending with .java

HelloWorld.java

Java names are case-sensitive. HelloWorld.java is not the same as Helloworld.java.
Java applications could have one or more methods inside the curly brackets in a file and to run a java program you invoke or call main method that has the following syntax:

public static void main(String[] args)

Le's not worry about the method now, I will explain later in the next tutorials.
When you run a program from a command-line you first compile it using javac for example

javac HelloWorld.java

This will create a file with identical names but with the .class extension.
Run this file without including the .class extension.

java HelloWorld

Lets start with our first program.
Open your favourite TextEditor
Copy and Paste the following code.

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Save as HelloWorld.java

Using your command-line type javac followed by the name of the file.

javac HelloWorld.java

Then give ive the following command-line when there are no syntax-errors

java HelloWorld

Output:

Hello World!

Time For FUN


Copy and paste the following code to your editor:

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
class MyProgram extends JPanel {

public void paint(Graphics g) {
    g.drawOval(100,50,200,200);
    g.fillOval(155,100,10,20);
    g.fillOval(230,100,10,20);
    g.drawArc(150,160,100,50,180,180);  
}
public static void main(String[] args) {
    
    
    JFrame frame = new JFrame();
    frame.setTitle("Smile");
    frame.setSize(400,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new MyProgram();
    frame.add(panel);       
    frame.setVisible(true);
                
}

}

There's a suprise for you when you run the program :-)

That's it for now,
Follow @syzmic for more Java lessons and don't forget to hit that upvote button.

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 71219.01
ETH 3807.66
USDT 1.00
SBD 3.50