Java basics
This lesson focuses on:
- The fundamental elements of a Java program
- Printing text
- Including comments in Java code
The fundamental elements of a Java program
The source code of every Java program has to have a few fundamental elements.
Every Java program should have:
-
A class declaration
A class is a grouping of related variables and functions (methods) that is used to achieve something. All the source code for a Java program will be placed within the class definition.
Syntax for declaring a class:
class nameOfClass{ code that will run the program goes here }NOTE: Class names should be descriptive and reflect the central purpose of a program.
Example:
class PrintText{ }In the above example a class named PrintText is declared.
-
A main() method
A method is a grouping of code that executes when it is called. The main() method is what makes a Java program work. When you insert the main() method into a Java program, it has to be used with a few special keywords, and has to contain a certain parameter.
Example:
class PrintText{ public static void main(String[] args){ } }The above example is a 'bare bones' Java program. It does not do anything, but only contains the fundamental elements needed to create a Java program.
Printing text
To print text in a Java program, you can use either the System.out.print() method to print a single line of text or the System.out.println() method to print a single line of text followed by a line break.
Syntax:
System.out.print("textToPrint");
System.out.println("textToPrint");
Example:
class PrintText{
public static void main(String[] args){
System.out.println("Here is some text");
System.out.print("Here is some more text");
}
}
Output:
Here is some more text
The above code uses the System.out.println() method to print out one line of text followed by a line break, and then the System.out.print() method to print out another line of text.
NOTE: Every line of code in a Java program must end with a semicolon. If you don't end a line of code with a semicolon, an error will be generated!
Including comments in Java code
Comments in Java are declared so that code would be easier to understand and to navigate. Comments are not seen within code and can be placed anywhere within it. You can have single line comments and multi line comments.
Single line comments
Single line comments in Java are declared with two / symbols.
Example:
class PrintText{
public static void main(String[] args){
//print a single line of text
//followed by a line break
System.out.println("Here is some text");
//print another line of text
//with no line break afterwards
System.out.print("Here is some more text");
}
}
Output:
Here is some more text
NOTE: Single line comments can span only a single line.
Multi line comments
Multi line comments in Java are declared with a starting /* and an ending */
Example:
/*
This is a multi-line comment
This program will print
two lines of text
This multi-line comment
contains seven lines
*/
class PrintText{
public static void main(String[] args){
System.out.println("Here is some text");
System.out.print("Here is some more text");
}
}
Output:
Here is some more text




