Java Programming: Creating and Running a Simple Java Program, Slides of Computer Engineering and Programming

Instructions on creating and running a simple java program using notepad, wordpad, and various ides such as textpad, jbuilder, and netbeans. It covers creating and modifying source code, compiling it using javac, and running the bytecode using java. The document also explains the concept of trace a program execution and compiling and running java from the command window.

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Advance Computer Programming
Introduction to Java
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Java Programming: Creating and Running a Simple Java Program and more Slides Computer Engineering and Programming in PDF only on Docsity!

Advance Computer Programming

Introduction to Java

A Simple Java Program

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Listing 1.

Creating and Editing Using WordPad

To use Word Pad , type w rite Welcom e.java from the DOS prom pt.

Creating, Compiling, and Running Programs

Source Code

Create/Modify Source Code

i.e., javac Welcome.java^ Compile Source Code

Bytecode

i.e., java Welcome^ Run Byteode

Result

If compilation errors

If runtime errors or incorrect result

public class Welcome { public static void main(String[] args) { }^ System.out.println("Welcome^ to Java!"); }

… Method Welcome() 0 aload_0 … Method void main(java.lang.String[]) 0 getstatic #2 … Java!^ 3 ldc #3 <String">^ "Welcome to 5 invokevirtual #4 … 8 return

Saved on the disk

stored on the disk

Source code (developed by the programmer)

Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)

Trace a Program Execution

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Execute statement

animation

Trace a Program Execution

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

animation

print a message to the console

Compiling and Running Java from

TextPad

Compiling and Running Java from

JBuilder

Anatomy of a Java Program

  • Comments
  • Package
  • Reserved words
  • Modifiers
  • Statements
  • Blocks
  • Classes
  • Methods
  • The main method

Comments

In Java, comments are preceded by two slashes (//) in a line, or enclosed between /* and / in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /, it scans for the next / and ignores any text between / and */.

Reserved Words

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Listing 1.1 are public, static, and void. Their use will be introduced later.

Modifiers

Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private dat or method cannot be accessed by other programs.

Blocks

A pair of braces in a program forms a block that groups

components of a program.

public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Class block Method block

Classes

The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. For now, though, understand that a program is defined by using one or more classes.