This document provides an introduction to Java programming. It discusses the background and history of Java, the Java runtime environment including the Java Virtual Machine, and the strengths of Java. It also outlines the basic components of a Java program such as comments, classes, and methods. The document demonstrates simple Java programs for console and window applications that perform input and output using methods like println and dialog boxes. It concludes with examples of common mistakes in Java code.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
22 views
01 - Introduction To Java
This document provides an introduction to Java programming. It discusses the background and history of Java, the Java runtime environment including the Java Virtual Machine, and the strengths of Java. It also outlines the basic components of a Java program such as comments, classes, and methods. The document demonstrates simple Java programs for console and window applications that perform input and output using methods like println and dialog boxes. It concludes with examples of common mistakes in Java code.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34
Chapter 1
Introduction to Java Programming
Learning Objectives To learn the background of Java To understand the Java runtime environment To know the strengths of Java To learn the basic components of Java program To work with simple input and output of Java program
Chapter 1: Introduction to Java Programming 2
Learning Outcomes Able to explain the background about Java Able to explain the Java development environment Able to list the strengths of Java Understand the basic components of Java program Able to write Java program with simple input and output statements
Chapter 1: Introduction to Java Programming 3
About Java Developed my James Gosling and released by Sun Microsystems in 1995 Java 2 released in December 1998, with features for enterprise and mobile applications. It was then renamed to Java EE, Java ME and Java SE in 2006. In November 2006, Sun released Java as free and open source software and made the core code available under free software / open source distribution terms in 2007. Sun was later acquired by Oracle Corporation in 2009- 2010.
Components of Java Program Comments Reserved words Modifiers Statements Blocks Classes Methods
Chapter 1: Introduction to Java Programming 9
Java Console Application Example 1.1 Simple Java console application
Chapter 1: Introduction to Java Programming 10
Comments /* and */ – the enclosed text is treated as a comment // – the rest of the line is treated as a comment /** and / – the enclosed text is treated as a comment, is used to automatically generate documentation Examples: // This application prints welcome to Java /* This application prints welcome to Java */
Chapter 1: Introduction to Java Programming 11
Reserved Words Words that have a specific meaning to the compiler Cannot be used for other purposes in the program It is always spelled in lowercase letters
Chapter 1: Introduction to Java Programming 12
Access Modifiers/Specifiers Modifiers specify how attributes, methods, and classes can be used or accessed. Examples of modifiers: public static private final abstract protected
Chapter 1: Introduction to Java Programming 13
Statements A statement represents an action or a sequence of actions Every statement in Java must end with a semicolon (;) Examples: System.out. println("Welcome to Java") int x = 5;
Chapter 1: Introduction to Java Programming 14
Blocks Formed by the braces ({ }) Used to group statements together Help compiler to identify components of a program Each block begins with an open brace ({) and ends with a closing brace (}) Blocks can be nested – that is, one block can be placed within another
Chapter 1: Introduction to Java Programming 15
Classes A class is a template or blueprint for objects A program is defined by using one or more classes Every Java program has at least one, and programs are contained inside a class definition enclosed in blocks The class can contain data declarations and method declarations
Chapter 1: Introduction to Java Programming 16
Methods A method is a collection of statements that perform a sequence of operations in order to provide certain functionality. A method can be used even without fully understanding the details of how it works. It is used by invoking a calling statement with arguments. The arguments are enclosed within parenthesis. Example: System.out.println() - a predefined method in the standard Java language
Chapter 1: Introduction to Java Programming 17
println Method Example 1.2 println method
Chapter 1: Introduction to Java Programming 18
print Method Example 1.3 print method
Chapter 1: Introduction to Java Programming 19
Escape Sequence Escape Description Sequence \n Newline. Position the cursor at the beginning of the next line. \t Horizontal tab. Move the cursor to the next tab stop \r Carriage return. Position the cursor to the beginning of the current line; do not advance to the next line. \’ Single quote. Print a single quote character. \\ Backslash. Print a backslash character. \" Double quote. Print a double quote character. \f Form feed.
Chapter 1: Introduction to Java Programming 20
Newline and Horizontal tab Escape Sequence Example 1.4
Chapter 1: Introduction to Java Programming 21
Quotes and backslash Escape Sequence Example 1.5
Chapter 1: Introduction to Java Programming 22
Java Window Application: Message Dialog It is used to output a simple message in a window frame It consists of The message title, default is “Message” The message The icon, default is information icon The “OK” button To use message box, you need to import the library class – javax.swing.JOptionPane Example: JOptionPane.showMessageDialog(null,message);
Chapter 1: Introduction to Java Programming 23
Java Window Application: Message Dialog Example 1.6
Chapter 1: Introduction to Java Programming 24
Java Window Application: Message Dialog Type Java provides various message dialog type: Message Dialog type Description JOptionPane.ERROR_MESSAGE Displays a dialog that indicates an error to the application user. JOptionPane.INFORMATION_MESSAGE Displays a dialog with an informational message to the application user. JOptionPane.WARNING_MESSAGE Displays a dialog that warns the application user. JOptionPane.QUESTION_MESSAGE Displays a dialog that poses a question to the application user. JOptionPane.PLAIN_MESSAGE Displays a dialog that simply Chapter 1: Introduction to Java Programming contains a message with no 25 Java Window Application: Message Dialog Type Example 1.7
Chapter 1: Introduction to Java Programming 26
Java Window Application: Message Dialog Type Output for Example 1.7
Chapter 1: Introduction to Java Programming 27
Java Window Application: Input Dialog It is used to read input in string It consists of The dialog box title “Input” The question The input box The question icon The “OK” and “Cancel” buttons Example: JOptionPane.showInputDialog(“Enter name”);
Chapter 1: Introduction to Java Programming 28
Java Window Application: Input String Example 1.8
Output
Chapter 1: Introduction to Java Programming 29
Java Window Application: Input Integer Example 1.9
Output
Chapter 1: Introduction to Java Programming 30
Common Mistakes Correct Incorrect public class Example{} Public Class Example {} public static void main Public Static void main (String args[ ]) (String args[ ]) public static void main (String args[ ]); System.out.println(); system.out.println(); System.Out.Println(); import javax.swing.*; Import javax.swing.*; import Javax.Swing.*; import.javax.swing.*;
Chapter 1: Introduction to Java Programming 31
Common Mistakes Correct Incorrect System.out.println( System.out.println( “first\n\tsecond"); "first/n/tsecond"); JOptionPane.showMessageDialog Joptionpane.ShowMessageDialog( (null, "Welcome to Java"); null, "Welcome to Java"); JOptionPane.showDialogMessage (null, "Welcome to Java"); JOptionPane.showInputMessage( null, "Welcome to Java"); JOptionPane.showMessageDialog(" Welcome to Java"); //missing null int n=Integer.parseInt(s); int n=Int.parseInt(s); int n=Integer.parse(s);
Chapter 1: Introduction to Java Programming 32
Summary Java is an object-oriented programming language that is compiled and interpreted by Java Virtual Machine to enable write once, run anywhere Java Runtime Environment (JRE) is a bundle of programs that allow for developing and running of Java program The strengths of Java include portability, reusability, dynamic, multithreaded, secured, robust, high performance and distributed The basic components of Java program consists of comment, class, method, modifier, statement, and block.
Chapter 1: Introduction to Java Programming 33
Summary The use of print and println methods with the escape sequence enable output to be printed with column and in new line. A message can be printed in different message icon and message title. The Input Dialog takes input as string. Any number input requires conversion to be made with the correct conversion method.