0% found this document useful (0 votes)
18 views

Lesson 3 Java Introduction and Syntax

Ok

Uploaded by

soberanoescoton
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)
18 views

Lesson 3 Java Introduction and Syntax

Ok

Uploaded by

soberanoescoton
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/ 4

PRG 1 - Programming 1

Lesson 3: Java Introduction and Syntax

Lesson 3.1: Java Introduction

What is Java?
 Java is a popular 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 QuickStart
 In Java, every application begins with a class name, and that class must match the filename.
 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");
}
}
 Every line of code that runs in Java must be inside a class. In our example, we named the class Main. A class
should always start with an uppercase first letter.
Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The main Method


 The main() method is required and you will see it in every Java program:
public static void main(String[] args)

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


 Remember that every Java program has a class name which must match the filename, and that every program
must contain the main() method.
System.out.println()
 Inside the main() method, we can use the println() method to print a line of text to the screen:

public static void main(String[] args) {


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

Note:
 The curly braces {} marks the beginning and the end of a block of code.
 System is a built-in Java class that contains useful members, such as out, which is short for "output". The
println() method, short for "print line", is used to print a value to the screen (or a file).
 You should also note that each code statement must end with a semicolon (;).

Exercise #1: Insert the missing part of the code below to output "Hello World".

Java Output/Print

Print Text
 You learned from the previous chapter that you can use the println() method to output values or print text in
Java: System.out.println("Hello World");
 You can add as many println() methods as you want. Note that it will add a new line for each method:

Example:
System.out.println("Hello World!");
System.out.println("I am learning Java.");
System.out.println("It is awesome!");

Double Quotes
 When you are working with text, it must be wrapped inside double quotations marks “”.
 If you forget the double quotes, an error occurs:
Example:
System.out.println("This sentence will work!");

System.out.println(This sentence will produce an error);

The Print() Method


 There is also a print() method, which is similar to println().
 The only difference is that it does not insert a new line at the end of the output:
Example:
System.out.print("Hello World! ");
System.out.println("I will print on the same line.");

Print Numbers
 You can also use the println() method to print numbers.
 However, unlike text, we don't put numbers inside double quotes:
Example:
System.out.println(3);
System.out.println(358);
System.out.println(50000);

Java Comments
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution
when testing alternative code.

Single-line Comments
 Single-line comments start with two forward slashes (//).
 Any text between // and the end of the line is ignored by Java (will not be executed).
 This example uses a single-line comment before a line of code:
Example:
// This is a comment
System.out.println("Hello World");

Java Multi-line Comments


 Multi-line comments start with /* and ends with */.
 Any text between /* and */ will be ignored by Java.
 This example uses a multi-line comment (a comment block) to explain the code:
Example:
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");

Exercise #2: Insert the missing part to create two types of comments.

Lesson 3.2:

You might also like