0% found this document useful (0 votes)
8 views7 pages

Javascript

Uploaded by

bwiseth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Javascript

Uploaded by

bwiseth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Introduction

What is Java?
Java is a popular and powerful programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

 Mobile applications (specially Android apps)


 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection
 And much, much more!

Why Use Java?


 Java works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc.)
 It is one of the most popular programming languages in the world
 It has a large demand in the current job market
 It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 It has huge community support (tens of millions of developers)
 Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
 As Java is close to C++ and C#, it makes it easy for programmers to
switch to Java or vice versa

Java Example
Java is often used in everyday programming tasks, like saying hello to a
user:
ExampleGet your own Java Server
public class Main {

public static void main(String[] args) {

String name = "John";

System.out.println("Hello " + name);

Get Started With Java


At W3Schools, you can try Java without installing anything.

Our Online Java Editor runs directly in your browser, and shows both the
code and the result:

Code:

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

Result:
Hello World

This editor will be used in the entire tutorial to demonstrate the different
aspects of Java.

Java Install
However, if you want to run Java on your own computer, follow the
instructions below.
Some PCs might have Java already installed.

To check if you have Java installed on a Windows PC, search in the start
bar for Java or type the following in Command Prompt (cmd.exe):

C:\Users\Your Name>java -version

If Java is installed, you will see something like this (depending on version):

java version "22.0.0" 2024-08-21 LTS


Java(TM) SE Runtime Environment 22.9 (build 22.0.0+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 22.9 (build 22.0.0+13-LTS, mixed mode)

If you do not have Java installed on your computer, you can download it
at oracle.com.

Note: In this tutorial, we will write Java code in a text editor. However, it
is possible to write Java in an Integrated Development Environment, such
as IntelliJ IDEA, Netbeans or Eclipse, which are particularly useful when
managing larger collections of Java files.

Java Quickstart
In Java, every application begins with a class name, and that class must
match the filename.

Let's create our first Java file, called Main.java, which can be done in any
text editor (like Notepad).

The file should contain a "Hello World" message, which is written with the
following code:

Main.java

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

Don't worry if you don't understand the code above - we will discuss it in
detail in later chapters. For now, focus on how to run the code above.
Save the code in Notepad as "Main.java". Open Command Prompt
(cmd.exe), navigate to the directory where you saved your file, and type
"javac Main.java":

C:\Users\Your Name>javac Main.java

This will compile your code. If there are no errors in the code, the
command prompt will take you to the next line. Now, type "java Main" to
run the file:

C:\Users\Your Name>java Main

The output should read:

Hello World

Java Syntax
In the previous chapter, we created a Java file called Main.java, and we
used the following code to print "Hello World" to the screen:

ExampleGet your own Java Server


Main.java

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

Example explained
Every line of code that runs in Java must be inside a class. The class
name should always start with an uppercase first letter. In our example,
we named the class Main.

Note: Java is case-sensitive. MyClass and myclass would be treated as


two completely different names.

The name of the Java file must match the class name. So if your class is
called Main, the file must be saved as Main.java. This is because Java
uses the class name to find and run your code. If the names don't match,
Java will give an error and the program will not run.
When saving the file, save it using the class name and add .java to the
end of the filename. To run the example above on your computer, make
sure that Java is properly installed: Go to the Get Started Chapter for how
to install Java. The output should be:

Hello World

The main Method


The main() method is required in every Java program. It is where the
program starts running:

public static void main(String[] args)

Any code placed inside the main() method will be executed.

For now, you don't need to understand the keywords public, static,
and void. You will learn about them later in this tutorial. Just
remember: main() is the starting point of every Java program.

System.out.println()
Inside the main() method, we can use the println() method to print a
line of text to the screen:

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

System.out.println("Hello World");

Statements
A computer program is a list of "instructions" to be "executed" by a
computer.

In a programming language, these programming instructions are


called statements.
The following statement "instructs" the compiler to print the text "Java is
fun!" to the screen:

ExampleGet your own Java Server


System.out.println("Java is fun!");

It is important that you end the statement with a semicolon ;.

If you forget the semicolon (;), an error will occur and the program will not
run:

Example
System.out.println("Java is fun!")

Result:
error: ';' expected

Tip: You can think of a statement like a sentence in English. Just as


sentences end with a period ., Java statements end with a semicolon ;.

Many Statements
Most Java programs contain many statements.

The statements are executed, one by one, in the same order as they are
written:

Example
System.out.println("Hello World!");

System.out.println("Have a good day!");

System.out.println("Learning Java is fun!");

Example explained
From the example above, we have three statements:

1. System.out.println("Hello World!");
2. System.out.println("Have a good day!");
3. System.out.println("Learning Java is fun!");
The first statement is executed first (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the
screen).
And at last, the third statement is executed (print "Learning Java is fun!"
to the screen).

You might also like