Sie sind auf Seite 1von 26

CHAPTER III:

Selection Structure
SO BEFORE WE START YOU MUST HAVE COURAGE AND PATIENCE TO
UNDERGO THIS TOPIC. IF YOU HAVE THIS TWO YOU MAY LISTEN
ATTENTIVELY OR ELSE JUST DO WHAT YOU WANT TO DO!
CONFIRMATION DIALOG
DO YOU WANT TO CONTINUE?
public static void main(String[] args) {

int input;

input = JOptionPane.showConfirmDialog(null, "Do You want to Continue", "Confirm",

JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if (input == JOptionPane.NO_OPTION){

System.out.println("No button clicked");

else if (input == JOptionPane.YES_OPTION){

System.out.println("Yes button clicked");

else if (input == JOptionPane.CLOSE_OPTION){

System.out.println("Option Close");

}
OUTPUT
DO YOU WANT TO PROCEED?
• ConfirmDialog2b.java
package com.mkyong.confirmDialog;
import javax.swing.JOptionPane;
public class ConfirmDialog2b {
public static void main(String[] args) {
int input = JOptionPane.showConfirmDialog(null,
"Do you want to proceed?", "Select an Option...",JOptionPane.YES_NO_CANCEL_OPTION);
// 0=yes, 1=no, 2=cancel
System.out.println(input);
}
}
OUTPUT
SO ARE YOU CATCHING UP?
DEBUGGING

DEBUGGING JAVA APPLICATION WITH NEATBEANS


• Neatbeans provides an easy environment for
debugging or troubleshooting your Java
applications. With Neatbeans debugger, you can
step through code line by line while viewing status
of variables, threads and other information. No
need to add print() statement for finding problems
that occur in your apps. Instead use breakpoints.
WHAT IS BREAKPOINTS?

• Breakpoint is a marker that you can set to specify where execution should pause when your are running
your application in the IDE debugger. With Breakpoints you can :
• Monitor the values of variables
• Take control of program execution by stepping through code line by line.
• Detect if an object is created.
• Detect when the value of a variable is changed.
FOLLOWING THINGS YOU CAN PERFORM WITH
NEATBEANS DEBUGGER:

• Step through application code line by line.


• Step through JDK source code.
• Using breakpoints, execute specific parts of code at a time track value of a variable /expression.
• Fix code on the fly and apply those code changes and continue the debugging session.
• Option to suspend threads/ execution at an exception.
• Step back to the beginning of a previously called method. (Pop a call in the current call stack).
JAVA PLATFORM DEBUGGER ARCHITECTURE

Debugger Interface
LAYER 1

• JDWP-Java Debug Wire


LAYER 2
Protocol

JVM- Java Virtual Machine LAYER 3


• Layer 1 - High Level interface for
debugging
• Layer 2 – Format of Information Transfer
• Layer 3 – Low level native interface,
applies code changes at jvm level.
DEBUGGER PARAMETERS

Neatbeans debugger allows you to enable remote debugging to already running in Java application. For
this, you must run your application in debug mode, which requires below parameters.
-Xdebug –Xrunjdwp:transport=dt_socket, server=y, address=<<port number>>, suspend=n

Example:
Java Xdebug - Xrunjdwp:transport=dt_socket, server=y address=65535, suspend= n
PARAMETER DESCRIPTION

-XDebug Enables the application to be debugged.

-Xrunjdwp Loads the reference implementation of the


JDWP(Java Debug Wire Protocol), which enables
remote debugging.
Transport Name of the Transport to be used when debugging
the application.dt_socket for a socket connection.

Server Y- Application listen for a connection at the


specified address.
N- Application attempts to attach to a debugger at
the specified address.
Address Specifies a port number used for communication
ATTACHING THE DEBUGGER TO A RUNNING
APPLICATION
• Connector – Select the appropriate connection type.
• Transport – Specifies the JDPA transport protocol – automatically filled based connector.
• Host – Machines where the debugging application is running.
• Port – Number that the application listen on.
• Timeout – Durations that the debugger waits for the connection to be establish.
STILL HOLDING ON?
DEBUGGER COMMANDS
COMMANDS FOR DEBUGGING A JAVA APP

• F7 – Step Into : Executes each sources line, if it has method call, and source code is available, pointer
moves to that method and executes it, other wise pointer moves to the next line in the file.
• F8 – Step Over : Executes each source line without stepping through the individual
instructions/commands.
• F4 – Run to Cursor : Execute the program from the current line.
• F5 – Continue : Resume debugging until it searches a next breakpoint or exception or until the program
terminates normally.
FIXING CODE DURING A DEBUGGING SESSION
• Apply Code Changes – It’s a useful feature. This can save a lot of time, otherwise waiting for source to be rebuilt and
restarting the server/debugging session.
It is useful for:
1. Fine-tune the code. (fixing minor issues)
2. Change the logic within a method.
This does not work for the following changes:
1. Add/remove methods or fields.
2. Change the access modifiers of a class, method, field.
3. Refactor the class hierarchy
• In the simplest form , it should not accept skeleton changes.
• NOTE : Once restarted the server, applied code changes are not taking effect for next time because “
Apply code changes” works on particular JVM instance only.

• For fixing code on the fly while debugging session, you must attach your application source code and jar
files properly.
ADVANTAGES OF USING DEBUGGER

• Easily and quickly find and resolve the problem.


• Understand the flow of your application code is easier.
THANK YOU FOR LISTENING!

Das könnte Ihnen auch gefallen