Java Programming: A Simple Welcome Program and Compilation Process, Slides of Java Programming

An introduction to java programming with a simple 'welcome to java!' program. It covers creating and editing the source code using notepad and wordpad, compiling and running the program, and tracing its execution. The document also explains the concept of packages, reserved words, modifiers, statements, blocks, classes, methods, and the main method.

Typology: Slides

2011/2012

Uploaded on 07/13/2012

aim.high
aim.high 🇮🇳

4.2

(6)

48 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: A Simple Welcome Program and Compilation Process and more Slides Java Programming in PDF only on Docsity!

Advance Computer Programming^ Introduction to Java

docsity.com

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!"); } }

2

Listing 1.

docsity.com

Creating and Editing Using WordPad^ To use WordPad, type

write Welcome.java from the DOS prompt.

4 docsity.com

Creating, Compiling, and Running Programs

5

Source Code Create/Modify Source Code Compile Source Codei.e., javac Welcome.java

Bytecode Run Byteode i.e., java Welcome

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 …3 ldc #3 <String

"Welcome to

Java!

"> 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 JVMto read and interpret, not for you to understand)

docsity.com

Trace a Program Execution

7

//This program prints Welcome to Java!public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!"); } }

Execute statement

animation

docsity.com

Trace a Program Execution

8

//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 theconsole

docsity.com

Compiling and Running Java fromTextPad

(^10) docsity.com

Compiling and Running Java fromJBuilder

(^11) docsity.com

Anatomy of a Java Program • Comments• Package• Reserved words• Modifiers• Statements• Blocks• Classes• Methods• The main method

(^13) docsity.com

Comments

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

(^14) docsity.com

Reserved Words

Reserved words or keywords are words that have aspecific meaning to the compiler and cannot be usedfor other purposes in the program. For example,when the compiler sees the word class, itunderstands that the word after class is the namefor the class. Other reserved words in Listing 1.1 arepublic, static, and void. Their use will be introducedlater.

(^16) docsity.com

Modifiers Java uses certain reserved words called modifiersthat specify the properties of the data, methods,and classes and how they can be used. Examplesof modifiers are public and static. Othermodifiers are private, final, abstract, andprotected. A public datum, method, or class canbe accessed by other programs. A private dat ormethod cannot be accessed by other programs.

(^17) docsity.com

Blocks

19

A pair of braces in a program forms a block that groupscomponents of a program.

public class Test {

public static void main(String[] args) {

System.out.println("Welcome to Java!"); } }

Class block

Method block

docsity.com

Classes

The class is the essential Java construct. A class is atemplate or blueprint for objects. To program inJava, you must understand classes and be able towrite and use them. For now, though, understandthat a program is defined by using one or moreclasses.

(^20) docsity.com