1.03k likes | 1.04k Views
Welcome back to an advanced Java programming course covering fundamental topics to developing comprehensive projects. Learn Java syntax, object-oriented programming, GUI programming, and more. Dive into Java's history, characteristics, and benefits for robust, secure, architecture-neutral, high-performance applications. Discover the architecture-neutral and dynamic features of Java, including its distributed, interpreted, and robust nature. Explore the object-oriented techniques and benefits that Java offers for developing interactive applications.
E N D
Welcome Back!!! Advanced Java ProgrammingCSE 7345/5345/ NTU 531 Session 2
Office Hours: by appt 3:30pm-4:30pm SIC 353 Chantale Laurent-Rice Welcome Back!!! trice75447@aol.com claurent@engr.smu.edu
Introduction • Chapter 1 • Course Objectives • Organization of the Book
Objectives • Upon completing this chapter, you will understand • Create, compile, and run Java programs • Primitive data types
Book Chapters • Part I: Fundamentals of Programming • Chapter 1 Introduction to Java • Chapter 2 Primitive Data Types and Operations • Chapter 3 Control Statements • Chapter 4 Methods • Chapter 5 Arrays
Book Chapters, cont. • Part II: Object-Oriented Programming • Chapter 6 Objects and Classes • Chapter 7 Strings • Chapter 8 Class Inheritance and Interfaces • Chapter 9 Object-Oriented Software Development
Book Chapters, cont. • Part III: GUI Programming • Chapter 10 Getting Started with GUI Programming • Chapter 11 Creating User Interfaces • Chapter 12 Applets and Advanced GUI
Book Chapters, cont. • Part IV: Developing Comprehensive Projects • Chapter 13 Exception Handling • Chapter 14 Internationalization • Chapter 15 Multithreading • Chapter 16 Multimedia • Chapter 17 Input and Output • Chapter 18 Networking • Chapter 19 Java Data Structures
Chapter 1 • Objectives: • Get an overall perspective of what capabilities and features are encompassed by Java and its development kit. • Take a first look at Java syntax. • Getting Input from Input Dialog Boxes • Style and Documentation Guidelines
What Is Java? • History • Characteristics of Java
What is Java? • An Object-Oriented Programming Language developed at Sun Microsystems • A Virtual Machine (run-time environment) that can be embedded in web browsers (e.g. Netscape Navigator, Microsoft Internet Explorer and IBM WebExplorer) and operating systems. • A set of standardized Class libraries (packages), that support: • Creating graphical user interfaces • Communicating over networks • Controlling multimedia data
History • James Gosling and Sun Microsystems • Oak • Java, May 20, 1995, Sun World • HotJava • The first Java-enabled Web browser • JDK Evolutions • J2SE, J2ME, and J2EE (not mentioned in the book)
Java is simple Object-Oriented Distributed Interpreted Robust Secure Architecture-neutral Portable High-performance Multithreaded dynamic Characteristics of Java
Java is Simple • Java is not just a language for use with the Internet. • It is a full featured Object-Oriented Programming Language (OOPL). • Java is a bit easier than the popular OOP language C++. • Java uses automatic memory allocation and garbage collection.
What is Object-Oriented Programming? • Think of OOP as a set of implementation techniques that • Can be done in any programming language • Are very difficult to do in most programming languages • OOP provides great flexibility, modularity, and reusability.
Java is Distributed • Distributed computing involves several computers working together on a network. • Java’s concurrency features make is unique for developing many interactive and networked applications.
Java is Interpreted • Java Virtual Machine: • Java is compiled to byte-codes whose target architecture is the Java Virtual machine (JVM). • The virtual machine is embeddable within other environments, e.g. web browser and operating systems. • Utilize a byte-code verifier when reading in byte-codes. The class loader is employed for “classes” loaded over the network (enhances security)
Java Virtual Machine • JVM Environment Java Source code .java Java byte-code .class java javac Java VM
Java is Robust • Robust means reliable. • No programming language can ensure complete reliability. • Java puts a lot of emphasis on early checking for possible errors, because Java compilers can detect many problems that would first show up at execution time in other languages. • Java has a runtime exception-handling feature to provide programming support for robustness.
Java Is Architecture-Neutral • Java is interpreted. • JIT compiler • Just-in-time compilers • This provides • Improved performance • Better match to specific hardware
JIT Compiler • JIT- takes byte-codes and change it to machine code. J.I.T. Compiler JVM Running Applet or Application .class file machine code
JIT Compiler • Because of the need for architecture independence, performance tuning must be performed on the client-side. • This client-side compilation is known as Just-In-Time (JIT) compilation.
Portable, Dynamic, Multithreaded, and Extensible .class files • Java runtime based on architecturally-neutral byte-codes (per class). • Multithreading is a program’s capability to perform several tasks simultaneously. interpreted Java Runtime loaded classes (byte-codes) call Native .dll Native .dll
Advantages • Byte-code is a compact machine language form. In Java the target machine is the Java Virtual Machine (VM). • These byte-codes are thus portable across architecture boundaries. • Applets and Applications have “class” files loaded on their behalf in order to execute.
JDK Versions • JDK 1.02 (1995) • JDK 1.1 (1996) • Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998) • Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000) • Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
JDK Editions • Java Standard Edition (J2SE) • J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) • J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. • Java Micro Edition (J2ME). • J2ME can be used to develop applications for mobile devices such as cell phones. This book uses J2SE to introduce Java programming.
Java IDE Tools • Forte by Sun MicroSystems • Borland JBuilder • Microsoft Visual J++ • WebGain Café • IBM Visual Age for Java • IBM Eclipse
Getting Started with Java Programming • A Simple Java Application • Compiling Programs • Executing Applications
Command Line Example 1.1 //This application program prints Welcome //to Java! package chapter1; public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Source Run
Creating and Compiling Programs • On command line • javac file.java
Executing Applications • On command line • java classname
Command line Example javac Welcome.java java Welcome output:...
Compiling and Running a Program Where are the files stored in the directory?
There are three forms of comments in Java. 1- int i = 10; // i is used as a counter 2- The multiline comment /* This is a comment */ This form of comment may also extend over several lines as shown here: /* This is a longer comment that extends over five lines. */
There are three forms of comments in Java. 3-This is the documentation comment. /** This is a Java documentation comment */ The advantage of documentation comments is that tools can extract them from source files and automatically generate documentation for your programs. The JDK has a tool named javadoc that performs this function.
Package • The second line in the program (package chapter1;) specifies a package name, chapter1, for the class Welcome. • Forte compiles the source code in Welcome.java, generates Welcome.class, and stores Welcome.class in the chapter1 folder.
Blocks A pair of braces in a program forms a block that groups components of a program.
Block Styles Use end-of-line style for braces.
main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; }
Displaying Text in a Message Dialog Box • you can use the showMessageDialog method in the JOptionPane class. • JOptionPane is one of the many predefined classes in the Java system, which can be reused. Source Run
The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2", JOptionPane.INFORMATION_MESSAGE));
A simple Java Applet import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Graphics; /* <applet code="FirstApplet" width=200 height=200> </applet> */ public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("This is my first applet!", 20, 100); } } See word doc for output.
Integral Literals • Integral literals may be expressed in decimal, octal, or hexadecimal. • The default is decimal. • To indicate octal, prefix the literal with 0 (zero) • To indicate hexadecimal, prefix the literal with 0x or 0X; • the hex digits may be upper-or lowercase.
Integral Literals For example: The value twenty-eight may be expressed the following ways: 28 034 0x1c 0x1C 0X1c 0X1C
Integral Literals • By default, an integral literal is a 32-bit value. • To indicate a long (64-bit) literal, append the suffix L to the literal expression. • The suffix can be lowercase, but then it looks so much like a one that makes it confusing.
Literals • Literals are explicit data values that appear in your program. • A literal is a value specified in the program source, as opposed to one determined at runtime. Literals can represent primitive or string variables, and may appear on the right side of assignments or in method calls. You cannot assign a value into a literal, so they cannot appear on the left of an assignment. For example: int x = 25;
Boolean Literals • The only literals of boolean type are true and false. For example: 1. boolean isBig = true; 2. boolean isLittle = false;
char Literals A char literal can be expressed by enclosing the desired character in single quotes, For example: char c = ' w ';
Chapter 1 Topic Summary • Java is many things • A concurrent object-oriented programming language • A virtual machine and Web-aware run-time • A powerful and stable set of class libraries.
Chapter 2Liang, Nutshell Objectives: • Introduce Programming with an Example • Identifiers, Variables, and Constants • Primitive Data Types • byte, short, int, long, float, double, char, boolean • Expressions • Operators, Precedence, Associativity, Operand Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -, • Syntax Errors, Runtime Errors, and Logic Errors