Java Programming Basics Module
Java Programming Basics Module
e
Programmi
ng with Lab
Learning
Module
Fun
Prog
VI. Module Objectives As for the outcome of the module, you are
expected to create an application applying the
concepts discussed in the lessons of this
module.
Lesson Objectives:
At the end of this lesson, you will be able to:
Fun
Explain the importance of Java
Explain why Java is a popular programming language
Explain the advantage of Java to other programming languages
Prog
Understand how Java Virtual Machine works
Install NetBeans
Discussions
What is Java
Is a programming language and computing platform first released by Sun
Microsystems in 1995.
Is an object-oriented programming language developed by James Gosling.
Unlike conventional languages which are generally designed either to be
compiled to native (machine) code, or to be interpreted from source code
at runtime, Java is intended to be compiled to a bytecode, which is then
run (generally using JIT compilation) by a Java Virtual Machine.
The language itself borrows much syntax from C and C++ but has a
simpler object model and fewer low-level facilities. Java is only distantly
related to JavaScript, though they have similar names and share a C-like
syntax.
Java is a programming language that produces software for multiple
platforms. When a programmer writes a Java application, the compiled
code (known as bytecode) runs on most operating systems (OS), including
Windows, Linux and Mac OS. Java derives much of its syntax from the C
and C++ programming languages.
Application
According to Sun, 3 billion devices run Java. There are many devices where
Java is currently used. Some of them are as follows:
There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application
2) Web Application
An application that runs on the server side and creates a dynamic page is
called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are
used for creating web applications in Java.
3) Enterprise Application
Garbage Collection
Performs 3 tasks
1. Loads code – perform by class loader
2. Verifies code – perform by bytecode verifier
3. Executes code – perform by runtime interpreter
Fun
Prog
Class loader
Bytecode Verifier
In Java you can use one of the following editors, to name a few. Notepad or
Notepad++
Context
NetBeans
jEdit
TextPad
Eclipse
IntelliJ
Android Studio
Notepad++
Note that there are other editors out there that you can use.
Fun
Required Software. The Java SE Development Kit (JDK) 8 is required
to install NetBeans IDE. You can download the latest update of JDK 8 at Prog
[Link]
The PHP and C/C++ NetBeans bundles only require the Java Runtime
Environment (JRE) 8 to be installed and run
The Java Development Kit (JDK) is one of three core technology packages
used in Java programming, along with the JVM (Java Virtual Machine) and the
JRE (Java Runtime Environment). It's important to differentiate between
these three technologies, as well as understanding how they're connected:
Developers new to Java often confuse the Java Development Kit and the Java
Runtime Environment. The distinction is that the JDK is a package of tools
for developing Java-based software, whereas the JRE is a package of tools
for running Java code.
Figure below shows how the JDK fits into the Java application development
lifecycle.
Setting up Environment
Step 3. Choose the JDK for your operating. If you are using windows 64 bit,
you can the red box as below
Step 5. Click the download button. You should see in your screen the
download file indicated in the line as below.
Step 3. Navigate to the download file and double click to install. Follow the
steps until you see the finish button as below.
Fun
Prog
Fun
Prog
3. In the New Project wizard, expand the Java category and select Java
Application as shown in the figure below. Then click Next.
4. In the Name and Location page of the wizard, do the following (as
shown in the figure below):
Fun
Prog
5. Click Finish.
The project is created and opened in the IDE. You should see the following
components in the screenshot as shown below:
The Projects window, which contains a tree view of the components of the
project, including source files, libraries that your code depends on, and so
on.
The Source Editor window with a file called HelloWorldApp open.
The Navigator window, which you can use to quickly navigate between
elements within the selected class.
[Link]("Hello World!");
The file should look something like the following code sample.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
/**
* @param args the command line arguments
*/ Fun
public static void main(String[] args) {
}
[Link]("Hello World!");
Prog
}
Because of the IDE's Compile on Save feature, you do not have to manually
compile your project in order to run it in the IDE. When you save a Java
source file, the IDE automatically compiles it.
The Compile on Save feature can be turned off in the Project Properties
window. Right-click your project, select Properties. In the Properties window,
choose the Compiling tab. The Compile on Save checkbox is right at the top.
Note that in the Project Properties window you can configure numerous
settings for your project: project libraries, packaging, building, running, etc.
Java output is a class file so you need a third party software to run it.
Fortunately, java provides Java Virtual Machine to run the class program.
Here you have learned how JVM works.
And lastly, you have learned how to install Netbeans, one of the most
popular IDE that you can use to create application in Java.
Discussion:
Fun
Prog
Step 4. In the project name, type Console. Leave all the same and click Finish
The red box indicates your project name and its corresponding code
and libraries.
The green box indicates the current code that is open. This is where
you do your development
Step 5. Modify the code (in green) by adding the code below in red box
Description
Package - used to group related classes. Think of it as a folder in a file
directory. We use packages to avoid name conflicts, and to write a
better maintainable code
import - is used to import built-in and user-defined packages into your
java source file so that your class can refer to a class that is in another
package by directly using its name.
[Link] – this the name of the source file
/* */ - this is a multiple line comments. The contents are ignored by the
compiler. If you want a single line then you can use //
class Console – the keyword class declares that a new class is being
defined and Console is the name of the class
{} – the entire class definition, including all its members, will be between
{ and }
public – this keyword is an access specifier, which allows the main()
method to be accessed by the code outside the class
static – this keyword allows main() to be called without having to create a
particular instance of the class
void – this keyword tells the compiler that the main() does not return a
value
main() – this is a method called when a Java application begins. The
second { and } indicates the main() block
Note. Fun
main() method is simply a starting place. A program may have one or
more classes, but only one of which will need to have a main() to get Prog
things started.
When java source code is compiled, each individual class is put into its
own output file named after the class and using the .class extension
Option 2. Navigate the Run menu and choose run project as shown below
Option 3. press F6
Note. if you cannot see the output window, you can navigate to Window
Output as shown below
Identifiers
Fun
are names given to a variable, class or method
you can use letters, digits, dollar signs, and underscores in an identifier,
Prog
but the first character MUST be a letter, an underscore (_) or a dollar sign
($)
have no maximum length
Java keywords
the java keywords cannot be used for a variable, class, or method.
Aside from the keywords mentioned above, you cannot use true, false and
null
Fun
Prog
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
String - stores text, such as "Hello". String values are surrounded by
double quotes
int - stores integers (whole numbers), without decimals, such as 123 or
-123
float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
boolean - stores values with two states: true or false
Syntax:
type variable = value
int age = 50;
Fun
Prog
Run the program and you will see the output as below
Java Operators
Example: Modify your Console class with the code as shown below
When you run the program, you should see the output as shown below
When you run the program, your output should be shown as below
Run the program. It will ask you to type your name, age, up to char data.
Your output should be shown as below
Example:
int result = (int)(x / y);
Notes
There are two casting directions: narrowing (larger to smaller type) and
widening (smaller to larger type). Widening can be done automatically (for
example, int to double), but narrowing must be done explicitly (like double to
int).
When you run the program, your output should be shown as below
Summary
In this lesson you also learned how to display information as well as how to
get input from the user in the form of Scanner and BufferedReader class.
Fun
Prog
Discussion:
Selection/Branching Statements
selection statements allow your program to choose different paths of
execution based upon the outcome of an expression or the state of a
variable
Java supports 2 selection statements : if and switch
IF Statement
The statements inside the body of “if” only execute if the given condition
returns true. If the condition returns false then the statements inside “if” are
skipped.
Syntax:
if (condition){
Fun
Prog
In the code, grade value is 90. Since it is >= 60, then it will display the
message “Passed”
If else statement
If condition returns true then the statements inside the body of “if” are
executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are
skipped and the statements in “else” are executed.
Syntax
if(condition) {
// Statements inside body of if
Example. Modify your Console code with the red box below Fun
Prog
else..if statement
The else..if statement is useful when you need to check multiple conditions
within the program, nesting of if-else blocks can be avoided using else..if
statement.
if (condition1) {
//These statements would execute if the condition1 is true
}
else if(condition2) {
}
//These statements would execute if the condition2 is true
Fun
else if (condition3) {
}
//These statements would execute if the condition3 is true Prog
.
.
else
{
//These statements would execute if all the conditions return false.
}
Fun
Nested If..else statement
Prog
When an if else statement is present inside the body of another “if” or “else”
then this is called nested if else.
Syntax
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Example. Modify the Console class as shown below. Pay attention to the red
box.
The code in red box is called nested if because it is in between if and else
statement. This is not else if. They are different. When you run the program,
you should see the output as shown below
Switch Statement
The switch case statement is used when we have multiple options and we
need to perform a different task for each option.
Syntax
Your menu value of X does not exist on the case value (A, B, C). In this
scenario the output is what you declared in the default statemen.
Run the application. Your output is shown below.
Summary
Also in this lesson you learned how to use switch, a form of branching
statement that you can use as alternative, rather than using selection
statements. Fun
Prog
Web References
[Link]
[Link]
ud283
[Link]
[Link]
J3a_OOPBasics.html
[Link]
[Link]
[Link]
java-certification/
[Link]
[Link]
[Link]
NetBeans_HowTo.html
[Link]
[Link]
[Link]
[Link]