Basic Java1 ICT 10
Basic Java1 ICT 10
Basic Java1 ICT 10
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
Example:
• 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.
• 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 main() method is required and you will see it in every Java program:
Inside the main() method, we can use the println() method to print a
line of text to the screen:
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("It is awesome!")
Double Quotes
When you are working with text, it must be wrapped inside double quotations
marks "".
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 is a comment
System.out.println("Hello World");
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
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”);