Basic Java1 ICT 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

A PROGRAM IS A SEQUENCEof instructions that a computer can

execute to perform some task. A simple enough idea, but for


the computer to make any use of the instructions, they must be
written in a form that the computer can use. This means that
programs have to be written in programming languages.
Programming languages differ from ordinary human languages in
being completely unambiguous and very strict about what is and is
not allowed in a program. The rules that determine what is allowed
are called the syntax of the language. Syntax rules specify the basic
vocabulary of the language and how programs can be constructed using
things like loops, branches, and subroutines. A syntactically
correct program is one that can be successfully compiled or
interpreted; programs that have syntax errors will be rejected (hopefully
with a useful error message that will help you fix the problem).

It’s not enough to write a program that will run—you want a


program that will run and produce the correct result! That is, the
meaning of the program has to be right. The meaning of a program
is referred to as its semantics. A semantically correct program is
one that does what you want it to.

A program can be syntactically and semantically correct but still be


a pretty bad program. Using the language correctly is not the same
as using it well. For example, a good program has “style.” It is
written in a way that will make it easy for people to read and to
understand. It follows conventions that will be familiar to other
programmers. And it has an overall design that will make sense to
human readers. The computer is completely oblivious to such
things, but to a human reader, they are paramount. These aspects
of programming are sometimes referred to as pragmatics.

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

Basics of Java Program


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

Example:

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 name of the java file must match the class name. When saving the
file, save it using the class name and add ".java" to the end of the
filename

The output should be:


Hello World

• 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).

Don't worry too much about System, out and println(). Just know that you need
them together to print stuff to the screen.

You should also note that each code statement must end with a semicolon (;).

• You can add as many println() methods as you want. Note that it will
add a new line for each method

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 "".

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 outpu

System.out.print("Hello World! ");

System.out.print("I will print on the same line.");

Output:
Hello World! I will print on the same line.

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:

// 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:

* The code below will print the words Hello World


to the screen, and it is amazing */

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

Sample Quiz:
1. Rewrite the following erroneous codes:

A. Program code 1
public class Main {
public static void main(String[] args)
system.out.println("Hello World")
}

B. Program code 2

Public class Main {


Public static void main(String[] args) ;{
system.out.println “(Hello Grade 10 ICT)”;
}
}

2. What are the output of the following codes? Assume that these code
segments are part of a complete syntax. Write NA if the code has
errors.

a. System.out.print(“Hello Students”);

b. System.out.print("Its Showtime”);
System.out.print(“sa GMA na”);

c. System.out.print(3rd Quarter na);

You might also like