




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















docsity.com
public static void main(String[] args) {
System.out.println("Welcome to Java!"); } }
2
docsity.com
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
(^10) docsity.com
(^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
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