How are comments made in Java?
The “//” marks the beginning of a comment. The compiler ignores everything from the double-slash to the end of the line.
Although comments are not required, they are very /important to programmers.
Most programs are much more complicated than this example, and comments help explain what’s going on.
What is a class header?
The line “public class HelloWorld” is known as a “class header”, and it marks the beginning of a class definition.
One of the uses of a class is to serve as a container for an application.
For now, just remember that a Java program must have at least one class definition.
The class header is the first line of code in a Java class and it defines the name of the class.
All Java classes must start with a capital letter
Class header has to have the same name as folder - “public class (name of class)”
- Class has to start with an uppercase letter!
- No spaces in names
- Can’t begin with number or include special characters
What is public?
“public” is a Java key word, and it must be written all in lowercase letters.
It is known as an “access specifier”, and it controls where the class may be accessed from.
The “public” specifier means access to the class is unrestricted. In other words, the class is “open to the public”
public means code can be accessed by any code in a package
What is class?
“class”, which must also be written in lowercase letters, is a Java key word that indicates the beginning of a class definition.
What is “Hello World”?
“HelloWorld” is the class name. This name was made up by the programmer.
The class could have been called “Pizza”, or “Dog”, or anything else the programmer wanted.
Programmer-defined names may be written in lowercase letters, uppercase letters, or a mixture of both.
In a nutshell, this line of code tells the compiler that a publicly accessible class named “HelloWorld” is being defined.
What does it mean to be case sensitive?
Java is a case-sensitive language. That means it regards uppercase letters as being entirely different characters than their lowercase counterparts. The word Public is not the same as public, and Class is not the same as class. Some words in a Java program must be entirely in lowercase, while other words may use a combination of lower and uppercase characters.
What is {?
The line with the “{“ in the program is called a “left brace”, or an “opening brace”, and is associated with the beginning of the class definition.
All of the programming statements that are part of the class are enclosed in a set of braces.
/If you glance at the last line of the program, you will see the closing brace. Everything between the two braces is the body of the class named “Simple”.
NOTE! You will get a syntax error if you forget to place a closing brace. Every opening brace must have a closing brace.
Everything between these braces is the body of the “main” method.
{} = curly braces, () = parentheses, [] = square brackets
{}: represents the container (class) defined by the opening and closing of the container and “public static void main” (main method) is the container inside the container - the point of entry of application - tells the compiler to compile anything inside main method
{}: after the header
What is a method header and a method?
The line “public static void main(String[] args)” is known as a method header, and it marks the beginning of a method.
A method can be thought of as a group of one or more programming statements that collectively have a name.
When creating a method, you must tell the compiler several things about it. That is why this line contains contains so many words.
At this point, the only thing you should be concerned about is that the name of the method is “main”, and the rest of the words are required for the method to be properly defined.
Every Java application must have a method named ‘main’. The main method is the starting point of an application. Main method - “public static void main(String[] args)” - container inside a container (starting point for application) - where you write the code you want the computer to run (source code)
Program should always have a main method named main
What does the line System.out.println do? What is a string literal?
The line “System.out.println(“HelloWorld!”);” displays a message on the screen.
The message, “HelloWorld!” is printed without the quotation marks. In programming terms, the group of characters inside the quotation marks is called a “string literal”.
NOTE! This is the only line in the program that causes anything to be printed on the screen.
The other lines, like “public class HelloWorld” and “public static void main(String[ ] args)”, are necessary for the framework of your program, but they do not cause any screen output.
What is a program?
Remember, a program is a set of instructions for the computer. If something is to be displayed on the screen, you must use a programming statement for that purpose.
When and why do we use semicolon?
Notice that at the end of the line “System.out.println(“HelloWorld!”);” is a semicolon. Just as a period marks the end of a sentence, a semicolon marks the end of a statement in Java.
NOTE! Places not to place a semicolon:
- Comments
- Class headers
- After braces
What are print and println?
The print and println methods are used to display text output. They are part of the Java API, which is a collection of pre-written classes and methods for performing specific operations.
What is console output?
In this lesson we will learn how to write programs that produce output on the screen.
The simplest type of output that a program can display on the screen is “console output”.
Console output is merely plain text. Performing output in Java, as well as many other tasks, is accomplished by using the Java API.
What is the Java API?
The term API stands for “Application Programmer Interface”.
The API is a standard library of pre-written classes for performing specific operations.
These classes and their methods are available to all Java programs.
The “print” and “println” methods are part of the API and provide ways for output to be displayed on the standard output device.
What is System?
System is a class that is part of the Java API.
The System class contains objects and methods that perform system-level operations, such as sending output to the console.
What is out?
The out object has methods, such as “print” and “println”, for performing output on the system console, or standard output device.
The “out” object is a member of the System class. It provides methods for sending output to the screen.
What type of relationship do System, out and print have?
hierarchical relationship, This hierarchy explains why the statement that executes println is so long. The sequence System.out.println specifies that println is a member of out, which is a member of System.
What is print and println in term of System.out.println?
The “print” and “println” methods are members of the “out” object. They actually perform the work of writing characters on the screen.
What is an argument?
The value that is to be displayed on the screen is placed inside the parentheses. This value is known as an “argument”.
What is important to know about the println method?
An important thing to know about the println method is that after it displays its message, it advances the cursor to the beginning of the next line. The next item printed on the screen will begin in this position
What is the print method?
The print method, which is also part of the System.out object, serves a purpose similar to that of println to display output on the screen. The print method, however, does not advance the cursor to the next line after its message is displayed
Explain what happens here:
System.out.print(“Programming is “);
System.out.println(“great fun!”);
An important concept to understand about these two lines of code is that, although the output is broken up into two programming statements, this program will still display the message on one line. The data that you send to the print method is displayed in a continuous stream. Sometimes this can produce less-than-desirable results.
Does the layout of the actual output match the arragnment of the strings in the source code?
The layout of the actual output looks nothing like the arrangement of the strings in the source code.
First, even though the output is broken up into four lines in the source code, it comes out on the screen as one line.
Second, notice that some of the words that are displayed are not separated by spaces. The strings are displayed exactly as they are sent to the print method. If spaces are to be displayed, they must appear in the strings.
What are two ways to add spaces or indent lines?
There are two ways to fix this program. The most obvious way is to use println methods instead of print methods.
Another way is to use escape sequences to separate the output into different lines.
An escape sequence starts with the backslash character (), and is followed by one or more control characters.
It allows you to control the way output is displayed by embedding commands within the string itself.
The escape sequence that causes the output cursor to go to the next line is \n.