0% found this document useful (0 votes)
11 views

Introduction To Computing Using Java

This document introduces the first Java program. It demonstrates a simple Java class with a main method that prints 'Welcome to Java!'. The document explains how to compile and run the program, and covers basic Java code elements like classes, methods, and comments.

Uploaded by

Nico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Introduction To Computing Using Java

This document introduces the first Java program. It demonstrates a simple Java class with a main method that prints 'Welcome to Java!'. The document explains how to compile and run the program, and covers basic Java code elements like classes, methods, and comments.

Uploaded by

Nico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to Computing

Using Java

First Java Program


The First Java Program

Type all carefully and save it to a


class Welcome { file named Welcome.java
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

Java program source files (.java)


class Welcome { contain definition of classes

/* The Welcome Program


-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

Curly braces pair enclose a block


class Welcome { of code, class Welcome here
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
} Don’t miss me!
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

Curly braces pair enclose a block


class Welcome { of code, method main( ) here
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
} Don’t miss me!
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

This is a block of comments, for


class Welcome { human, not for computer
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/ It explains to you what happens
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

/* and */ pair encloses a comment block


class Welcome {

/* The Welcome Program


-------------------
Illustrates a simple program displaying
a message.
*/ Don’t miss me!
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

This is a method of the class


class Welcome { Welcome, named main( )
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program
There MUST be a pair of
parentheses following ALL
class Welcome {
method names
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
} Don’t miss me!
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

Let's leave these first.


class Welcome {

/* The Welcome Program


-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

Standard properties of the


class Welcome { main( ) method
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

A statement (instruction) to display


class Welcome { a message
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

After every statement, there must


class Welcome { be a semi-colon!
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!") ;
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program
How to ask the computer to act
according to the instructions in
class Welcome { this program?
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/

public static void main (String [ ] args) {


System.out.println("Welcome to Java!");
}
}
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
The First Java Program

 Change to the directory containing the file


Welcome.java

 Type
javac Welcome.java
It generates a new file Welcome.class

 Type (without .class)


java Welcome

 What’s the result?

1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK


The First Java Program

Welcome

main Java Virtual Machine

Welcome to Java!
Message sender

1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK


What has happened?

Java Program [Welcome.java]

Compile Java Compiler


[javac]

Java Byte Code [Welcome.class] Translate native code

Java Virtual Machine (JVM) [java]


It locates the class method main() from the class
Welcome and starts execution from there
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
Big Program For Simple Task?

 Yes, it just displays a little message.

 When writing business letters, we conform to


grammar and format.

 Likewise for programming!

 For more complicated tasks, such overheads are


relatively trivial.
1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK
Software Life Cycle

Computer programming is not just writing


programs after learning a language.
requirements program
conception
specification design design
analysis
70% of the software
cost is related to coding
software maintenance
in the operation phase.
Well-designed and actual
constructed software is
easier to maintain. operation debugging testing program

1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK


End Note

Readings and References


– Chapter 1

1b 1st Java Prog Intro to Computing using Java, CS&E, CUHK

You might also like