Lesson 4 - Java Environment

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Java Environment

Part II
Lesson 4
NEIL A. MUTIA, MIT
Creating and Running a Java Program in
Android Phone
 There’s a lot of free application available in the Google
Play Store that allows aspiring programmers to create,
compile, and execute a Java Program.
Using the AIDE
Java Program Basic Structure
 Here are the most important points to note about the
Java programs:
 You have to keep in mind that, Java code is case
sensitive.
 To write a Java program, you must have to define class
first.
 The name of the class in Java (which holds the main
method) is the name of the Java program, and the same
name will be given in the filename.
Java Program Basic Structure
This must be the exact
filename when saving the
program, Hello.java

This is the
This is a print statement, use
main
to print the value inside this
method
symbols (“ ”)
Java Program Basic Structure
public class Hello  This creates a class called Hello.
 All class names must not contain spaces
 The public word means that it is accessible from any other classes.
/* Comments */ The compiler ignores comment block. Comment can be used anywhere in the
Or program to add info about the program or code block, which will be helpful for
// Comment developers to understand the existing code in the future easily.
Braces Two curly brackets {...} are used to group all the commands, so it is known that the
commands belong to that class or method.
public static void main  When the main method is declared public, it means that it can also be used by
code outside of its class, due to which the main method is declared public.
 The word static used when we want to access a method without creating its
object, as we call the main method, before creating any class objects.
 The word void indicates that a method does not return a value. main() is declared
as void because it does not return a value.
 main is a method; this is a starting point of a Java program.
 You will notice that the main method code has been moved to some spaces left.
It is called indentation which used to make a program easier to read and
understand.
Java Program Basic Structure
String[] args It is an array where each element of it is a string, which has been named as "args". If
your Java program is run through the console, you can pass the input parameter, and
main() method takes it as input.
System.out.println(); This statement is used to print text on the screen as output, where the system is a
predefined class, and out is an object of the PrintWriter class defined in the system.
The method println prints the text on the screen with a new line. You can also
use print() method instead of println() method. All Java statement ends with a
semicolon.
Saving a Java Program

1. Click the word File on the Menu


Saving a Java Program
2. Click the word Save As..
Saving a Java Program
3. Look for a directory on your computer where you want to save your program.
Make sure that the name of the class must match with the Filename you provide.
Don’t forget to put .java as file extension of your program.
Example: Hello.java
Compiling a Java Program
After saving the program, do not forget the directory where you save your program.
In my case, I save it on WINDRIVER E:\Folder A

To execute my program, I need to open CMD.


Compiling a Java Program
In put the necessary command to get in to the directory where you saved your java
Program. Since it is on the WINDRIVER E:\Folder A then I need to input these
Commands on the CMD.
Compiling a Java Program
At this point, I am already here on the directory. Now, I will input the compiling
command for java. The command will be the javac followed by a single space and
the exact name of the program. For example: javac Hello.java.
Compiling a Java Program
If no errors found, then this how your CMD looks like. From here, you should input the
command for execution following this pattern, java followed by a single space and the
name of the program without the file extension. For example: java Hello

This is the output


of the program
Example of Comment in a program
INLINE COMMENT

BLOCK COMMENT

You might also like