0% found this document useful (0 votes)
6 views50 pages

Java Programming Basics Module

The document outlines the Intermediate Programming with Lab course at City College of Calamba, focusing on Java programming basics through six lessons. It covers Java's history, advantages, application types, and the setup of development environments like NetBeans and JDK. The module aims to equip students with practical skills in Java programming, enabling them to develop real-world applications using the concepts learned.

Uploaded by

clairetamarez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views50 pages

Java Programming Basics Module

The document outlines the Intermediate Programming with Lab course at City College of Calamba, focusing on Java programming basics through six lessons. It covers Java's history, advantages, application types, and the setup of development environments like NetBeans and JDK. The module aims to equip students with practical skills in Java programming, enabling them to develop real-world applications using the concepts learned.

Uploaded by

clairetamarez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Intermediat

e
Programmi
ng with Lab
Learning
Module
Fun
Prog

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

i. Course Code PROG102


II. Course Title Intermediate Programming with Lab
III. Module Number 1
IV. Module Title Basics of Java Programming
V. Scope of the Module This module consists of 6 lessons, namely:
Lesson 1: Introduction to Java
Lesson 2: Data Types, Expressions and
Input/Output Element
Lesson 3: Selection/Branching Statements

V. Overview of the Module A major factor in the invention of Java


programming language is to remove some of
the flaws encountered with the procedural

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
approach. In Java programming language, it
removes some of the flaws that other
programming language and it practices object-
oriented programming from the start.

In this module you will learn why Java


programming language is one of the powerful
programming languages today. Also discussed
in this module are the foundations of
Fun
programming like variables, conditions,
iterations, methods, arrays and how to use Prog
them in developing real world applications.

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.

You should be able to apply variables, flow


controls, loops, methods and arrays in
developing real world example and application

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Lesson1. Introduction to Java

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.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
History of Java
 Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to implement a virtual machine and a language that
had a familiar C-like notation but with greater uniformity and simplicity
than C/C++. The first public implementation was Java 1.0 in 1995. It
made the promise of "Write Once, Run Anywhere", with free runtimes on
popular platforms. It was fairly secure and its security was configurable, Fun
Prog
allowing for network and file access to be limited. The major web
browsers soon incorporated it into their standard configurations in a
secure "applet" configuration. popular quickly. New versions for large and
small platforms (J2EE and J2ME) soon were designed with the advent of
"Java 2". Sun has not announced any plans for a "Java 3".
 In 1997, Sun approached the ISO/IEC JTC1 standards body and later the
Ecma International to formalize Java, but it soon withdrew from the
process. Java remains a proprietary de facto standard that is controlled
through the Java Community Process. Sun makes most of its Java
implementations available without charge, with revenue being generated
by specialized products such as the Java Enterprise System. Sun
distinguishes between its Software Development Kit (SDK) and Runtime
Environment (JRE) which is a subset of the SDK, the primary distinction
being that in the JRE the compiler is not present.

Why Use Java?

 Java works on different platforms (Windows, Mac, Linux, Raspberry Pi,


etc.)
 It is one of the most popular programming language in the world
 It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 It has a huge community support (tens of millions of developers)

Application

According to Sun, 3 billion devices run Java. There are many devices where
Java is currently used. Some of them are as follows:

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
1. Desktop Applications such as acrobat reader, media player, antivirus,
etc.
2. Web Applications.
3. Enterprise Applications such as banking applications.
4. Mobile
5. Embedded System
6. Smart Card
Fun
7. Robotics Prog
8. Games, etc.

Types of Java Applications

There are mainly 4 types of applications that can be created using Java
programming:

1) Standalone Application

Standalone applications are also known as desktop applications or window-


based applications. These are traditional software that we need to install on
every machine. Examples of standalone application are Media player,
antivirus, etc. AWT and Swing are used in Java for creating standalone
applications.

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

An application that is distributed in nature, such as banking applications, etc.


is called enterprise application. It has advantages of the high-level security,
load balancing, and clustering. In Java, EJB is used for creating enterprise
applications.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
4) Mobile Application

An application which is created for mobile devices is called a mobile


application. Currently, Android and Java ME are used for creating mobile
applications
Fun
Why Java is popular
Prog
 Simple – Java removes several C/C++ features considered to be unsafe
such as pointer arithmetic, struct, typedefs etc. and the need to free
memory. This cuts down on 50% of the most common bugs.
 Object Oriented – It enforces OOP design from the start.
 Robust – Java works hard to check for problems at compile time and run
time. Type checking catches many bugs early in the development life
cycle. It handles its own memory, which eliminates worry of freeing or
corrupting memory.
 Secure – The absence of pointers prevents illegal access to memory. Java
code passes several tests before actually executing on your machine.
Bytecode verifies checks for illegal codes.
 Distributed – Java has extensive TCP/IP capabilities that have built in,
which allow easy access to information over a network
 Portable – Java implements other portability standards. For example, ints
are always ints and always 32-bit 2’s complemented integers.
 Fast – The Java interpreter can be relatively slow in executing but the JIT
compilers can speed up Java programs.
 Multithreaded – Java allows many simultaneous activities in one program
via the Thread class. The major benefits are better interactive
performance and real-time behavior. For example, images can be loaded
with one thread, while user is doing some transactions.

Goals of Java programming language

 Provide a language which is easy to program by


o Eliminating the pitfalls of other language
o Being object oriented
o Providing means to make code streamlined and clear

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
 Provide an interpreted environment
o Improved speed of development
o Code portability
 Enable users to run more than one thread of activity
 Provide a means of checking code modules that are loaded to assure
security
Fun
Prog
Java Virtual Machine

 An imaginary machine that is implemented by emulating it in software on


a real machine. Code for the JVM is stored in .class file.
 Provides hardware platform specifications to which all Java technology
code is compiled.

Garbage Collection

 Allocated memory that is no longer needed should be deallocated


 In other languages, deallocations is the programmer responsibility
 Check for and frees memory that is no longer needed and this is done
automatically

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Java Code Security

 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

 Loads all classes needed for the execution of a program


 Adds security by separating namespaces for the classes of the local file
system from those imported from network sources
 This limits any Trojan horse applications because the local classes are
always loaded first

Bytecode Verifier

 Makes 4 passes on the code to ensure that


o The code adheres to the JVM specification
o The code does not violate the system integrity
o No illegal data conversion
o The parameter types for all operational code are correct

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Interpreter

 an interpreter is a computer program that directly executes, i.e. performs,


instructions written in a programming or scripting language, without
requiring them previously to have been compiled into a machine
language program. Fun
Selecting an Editor
Prog
Code editors or source code editors are the software that is designed
specifically to help developers in coding. These are actually the
text editors with some additional functionalities to manage and edit code. It
can be standalone or it can be a part of an Integrated Development
Environment (IDE).

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.

In this course, we will be using NetBeans

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
What is NetBeans

NetBeans is an integrated development environment (IDE) for


Java. NetBeans allows applications to be developed from a set of modular
software components called modules. ... In addition to Java development, it
has extensions for other languages like PHP, C, C++, HTML5, and JavaScript.

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

Note. the higher version of the JDK, the better.

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:

 The JVM is the Java platform component that executes programs.


 The JRE is the on-disk part of Java that creates the JVM.
 The JDK allows developers to create Java programs that can be executed
and run by the JVM and JRE.

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.

The JRE can be used as a standalone component to simply run Java


programs, but it's also part of the JDK. The JDK requires a JRE because
running Java programs is part of developing them.

Figure below shows how the JDK fits into the Java application development
lifecycle.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Setting up Environment

How to Download JDK

Step 1. Go to Java SE download site


@ [Link]

Step 2. In the website, click the JDK Download as shown below

Step 3. Choose the JDK for your operating. If you are using windows 64 bit,
you can the red box as below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Step 4. Accept License Agreement.

Step 5. Click the download button. You should see in your screen the
download file indicated in the line as below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

How to Install JDK

Step 1. Navigate to folder where you downloaded the JDK installer

Step 2. Double click the file and follow the steps.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Step 3. Click Next twice to install. The setup for Oracle JDK 14 using installer
is quick and easy, but it doesn’t update the relevant system environment
variables and do file associations.

Step 4. Use your "File Explorer", navigate to "C:\Program Files\Java" to


inspect the sub-directories.
Fun
Prog
Installing NetBeans

Step 1. Navigate to this site ([Link] If this


site is not working, search NetBeans in your preferred browser. Here I am
using Google Chrome.

Step 2. Choose the one in red box below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Note. If your purpose in downloading NetBeans is only for Java Development,


Java SE is enough. You can download Apache Tomcat if you want to create
java web application.

Step 3. Navigate to the download file and double click to install. Follow the
steps until you see the finish button as below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Note.

Fun
Prog

Step 4. Verify if NetBeans installed by navigating in your program files or


searching in your directory files. You should see something as below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Setting up and Running the Java Console Application
Follow steps below.
1. Start NetBeans IDE.
2. In the IDE, choose File > New Project, as shown in the figure below.

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):

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
o In the Project Name field, type HelloWorldApp.
o Leave the Use Dedicated Folder for Storing Libraries checkbox
unselected.
o In the Create Main Class field, type [Link].

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.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Adding Code to the Generated Source File


Because you have left the Create Main Class checkbox selected in the New
Project wizard, the IDE has created a skeleton main class for you. You can
add the "Hello World!" message to the skeleton code by replacing the line:

// TODO code application logic here

with the line:

[Link]("Hello World!");

Save the change by choosing File > Save.

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;

/**

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
*
* @author <your name>
*/
public class HelloWorldApp {

/**
* @param args the command line arguments
*/ Fun
public static void main(String[] args) {

}
[Link]("Hello World!");
Prog
}

Compiling and Running the Program

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.

To run the program:

 In you the menu, choose Run > Run Project.


The next figure shows what you should now see. This is called the output
window

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog
Summary
In this lesson you learned why Java programming language is very popular.
You also learned the advantage of Java programming language by
understanding the different security features of the language.

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.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Lesson 2. Data Types, Expressions and
Input/Output Statements

Lesson Objectives: Fun


At the end of this lesson, you will be able to:
 Learn the basic structure of java application Prog
 Learn how to declare and initialize variables
 Learn the different types of operators
 Learn what is type conversion and type casting
 Learn how to use input and output statements in solving basic problem

Discussion:

A Basic Java Application


Create a Java Application by following the steps below.
Step 1. Open NetBeans IDE
Step 2. Clik File  New Project

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Step 3. Choose Java Application and click Next

Fun
Prog

Step 4. In the project name, type Console. Leave all the same and click Finish

You should see by now your Console project editor as below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

 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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
 String args[] – this declares a parameter named args, which is an array
of instances of the class. [Link] receives any command-line
arguments present when the program is executed
 [Link] – display the string in the screen. Here, it displays “
my first java program”

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

Step 6. Run the program (three ways)


Option 1. click the run icon as shown in the red box below

Option 2. Navigate the Run menu and choose run project as shown below

Option 3. press F6

You can now see your output as shown below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Note. if you cannot see the output window, you can navigate to Window
Output as shown below

You can also run your code in the command window.


Run the code using command prompt

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Compile [Link]
 javac [Link]
Run the application
 java test

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

Primitive Data Types

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
 Logical - boolean
 Textual - char
 Integral – byte, short, int, long
 Floating – double, float

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;

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Modify your class Console as shown below. Note that we removed all the
lines with comments on it.

Fun
Prog

Run the program and you will see the output as below

Java Operators

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Java Assignment Operators

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Example: Modify your Console class with the code as shown below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

When you run the program, you should see the output as shown below

Accept Data using Keyboard

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
 Most of the time, user needs to input data. java have two ways
1. Using Scanner class
2. Using BufferedReader class

Using Scanner class


Modify your Console class as shown below Fun
Prog

When you run the program, your output should be shown as below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog
Using BufferedReader class. Modify the Console class as shown below.
Pay attention to the red underline.

Run the program. It will ask you to type your name, age, up to char data.
Your output should be shown as below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog
Type Casting
 Type casting is used to convert an object or variable of one type into
another.
Syntax:
dataType variableName = (dataType) variableToConvert;

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).

Example: Widening. Modify your Console class as below

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

Running the program will show the output as below

Example: Narrowing. Modify the Console class as below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

When you run the program, your output should be shown as below

Summary

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
In this lesson, you learned the basic structure of Java application. Also, you
have learned how to declare and initialize variables, the use of operators as
well as conversion and type casting which is important in any system
development.

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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Lesson 3. Selection / Branching Statements
Fun
Lesson Objectives: Prog
At the end of this lesson, you will be able to:
 Understand the use of selection / branching statement in Java
programming.
 Learn how to use selection / branching statements in solving basic
problem

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){

//Block of statements here


//These statements will only execute if the condition is true
}

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Example. Modify the Console class as shown below

Fun
Prog

Run the program and your output should be as shown below

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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
}
else {
//Statements inside body of else
}

Example. Modify your Console code with the red box below Fun
Prog

When you run your program, your output is : Passed.


Try to change your grade value to 50 and run the program again. This time
your output is failed because 50 is less than 60 which is your condition if it is
passed or failed.

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.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Syntax

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.
}

Example. Modify the console application as shown below.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
In the code, the grade value of 88 fall in the third condition. So when you run
the program, you will see the output as shown below.

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.

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog

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

switch (variable or an integer expression)


{
case constant:
// Statements
;
case constant:
// Statements

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
;
default:
// Statements if the choice is not available above. Work like else
;
}

Example. Modify the console application as shown below. Fun


Prog

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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
In this lesson, you learned different form of selection statements, their use
and application.

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

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba
Fun
Prog
References
 Brett D. McLaughlin, Gary Pollice, and Dave West, Head First Object-
Oriented Analysis and Design 1st Ed. (2006)
 Matt Weisfeld Weisfeld, The Object-Oriented Thought Process
(Developer's Library) 4th Ed. (2013)

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]

City College of Calamba


Dalubhasaan ng Lungsod ng Calamba

You might also like