Java
What You Will Get :
java learningjava development kitjava vs pythonjava language
java basicsjava virtual machine
Introduction to Java
Welcome to the world of Java programming!
Programming languages enable humans to write instructions that a computer can perform. With precise instructions, computers coordinate applications and systems that run the modern world.
Sun Microsystems released the Java programming language in 1995. Java is known for being simple, portable, secure, and robust. Though it was released over twenty years ago, Java remains one of the most popular programming languages today.
One reason people love Java is the Java Virtual Machine, which ensures the same Java code can be run on different operating systems and platforms. Sun Microsystems’ slogan for Java was “write once, run everywhere”.
Programming languages are composed of syntax, the specific instructions which Java understands. We write syntax in files to create programs, which are executed by the computer to perform the desired task.
Java runs on different platforms, but programmers write it the same way. Let’s explore some rules for writing Java.
The
HelloWorld
concept is: Hello World Printer. Other class concepts could be: Bicycle, or: Savings Account.
We marked the domain of this concept using curly braces:
{}
. Syntax inside the curly braces is part of the class.
Each file has one primary class named after the file. Our class name:
HelloWorld
and our file name: HelloWorld. Every word is capitalized.
Inside the class we had a
main()
method which lists our program tasks:
Like classes, we used curly braces to mark the beginning and end of a method.
public
, static
, and void
are syntax we’ll learn about in future lessons. String[] args
is a placeholder for information we want to pass into our program. This syntax is necessary for the program to run but more advanced than we need to explore at the moment.
Our program printed “Hello World” with the line:
println
is short for “print line”. We’ll use System.out.println()
whenever we want a program to write a message to the screen.Commenting Code
Writing code is an exciting process of instructing the computer to complete fantastic tasks.
Code is also read by people, and we want our intentions to be clear to humans just like we want our instructions to be clear to the computer.
Fortunately, we’re not limited to writing syntax that performs a task. We can also write comments, notes to human readers of our code. These comments are not executed, so there’s no need for valid syntax within a comment.
When comments are short we use the single-line syntax:
//
.
// This is the example of the single line comment.
When comments are long we use the multi-line syntax:
/*
and */
.
/* Hello Developer,
and now this is the example of multi line comment,
means comments in more than one line. */
Here’s how a comment would look in a complete program:
Semicolons and Whitespace
As we saw with comments, reading code is just as important as writing code.
We should write code that is easy for other people to read. Those people can be co-workers, friends, or even yourself!
Java does not interpret whitespace, the areas of the code without syntax, but humans use whitespace to read code without difficulty.
System.out.println("Orange");System.out.println("Banana");System.out.println("Pineapple");
As You can see Above code is not easily readable,so we give Whitespaces in this code after every semicolons so use of Whitespace is to make code easily readable.
System.out.println("Orange");
System.out.println("Banana");
System.out.println("Pineapple");
They will print the same text to the screen, but which would you prefer to read? Imagine if it was hundreds of instructions! Whitespace would be essential.
Java does interpret semicolons. Semicolons are used to mark the end of a statement, one line of code that performs a single task.
The only statements we’ve seen so far are
System.out.println("Your message!");
.
Let’s contrast statements with the curly brace,
{}
. Curly braces mark the scope of our classes and methods. There are no semicolons at the end of a curly brace.Compilation: Catching Errors
Java is a compiled programming language, meaning the code we write in a .java file is transformed into byte code by a compiler before it is executed by the Java Virtual Machine on your computer.
A compiler is a program that translates human-friendly programming languages into other programming languages that computers can execute.
The compiling process catches mistakes before the computer runs our code.
The Java compiler runs a series of checks while it transforms the code. Code that does not pass these checks will not be compiled.
For example, with a file called Start.java, we could compile it with the terminal command: javac Start.java
A successful compilation produces a .class file: Plankton.class, that we execute with the terminal command: java Start
An unsuccessful compilation produces a list of errors. No .class file is made until the errors are corrected and the compile command is run again.
No comments: