SlideShare a Scribd company logo
1 of 8
UNIT OF COMPETENCY: PERFORM OBJECT-ORIENTED ANALYSIS AND DESIGN IN
JAVA TECHNOLOGY
ELEMENT PERFORMANCE CRITERIA
Italicized terms are elaborated in the Range of Variables
1 Apply Basics of Java
language
1.1 Executable Java applications are created in accordance
with Java framework
1.2 Java packages are imported to make them accessible in the
code
1.3 Working with Java Data types is demonstrated in
accordance with Java framework
1.4 Using Operators and Decision Constructs is
demonstrated in accordance with Java framework
1.5 Creating and Using Arrays is demonstrated in accordance
with Java framework
1.6 Using Loop Constructs is demonstrated in accordance
with Java framework
VARIABLE RANGE
1. Executable Java
applications
 Hello World
 Hello with name
 Hello with name and date
1. Working with Java
Data Types
 Declare and initialize variables
 Differentiate between object references and primitive
variables
 Read and write to object fields
 Explain an object’s lifecycle (creation, dereference, and
garbage collection)
 Call methods on objects
 Manipulate data using StringBuilder class and its methods
 Create and manipulate Strings
2. Using Operators and
Decision Constructs
 Use Java operators
 Use parenthesis to override operator precedence
 Test equality between strings and other objects using ==
and equals()
 Create and use if-else constructs
 Use a switch statement
3. Creating and Using
Arrays
 Declare, initialize, and use a one-dimensional array
 Declare, initialize, and use a multi-dimensional array
 Declare and use an ArrayList
4. Using Loop Constructs  Create and use while loops
 Create and use for loops including the enhanced for loop
 Create and use do-while loops
 Compare loop constructs
 Use break and continue
Unit of Competency Learning Outcome Methodology
Assessment
Approach
1. Perform object-
oriented analysis
and design in Java
technology
1.1 Apply basics of Java
language
 Lecture/
Discussion
 Hands on
 Exercises
 Demonstration
 Practical exam
 Interviews/
questioning
Objectives:
At the end of the lesson the student should be able to:
Identify the basic parts of a Java Program
Execute basic Java Application
Writing a Java hello world program
1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Save the file as HelloWorld.java (note that the extension is .java) under a directory,
Every Java program starts from the main() method. This program simply prints “Hello world” to
screen.
Description
The java command starts a Java application. It does this by starting the Java Runtime
Environment (JRE), loading the specified class, and calling that class's main() method. The
method must be declared public and static, it must not return any value, and it must accept
a String array as a parameter.
Compiling it
Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the
following command to change the current directory to the one where the source file is stored:
cd C:Java
And type the following command:
javac HelloWorld.java
5. Running it
It’s now ready to run our first Java program. Type the following command:
java HelloWorld
That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is
no .java or .class extension). You would see the following output:
What we have learnt so far
Throughout this tutorial you have learnt the following things:

o JDK is the Java SE Development Kit that contains tools and libraries for Java
development.
o JRE is the Java Runtime Environment that enables running Java programs on your
computer.
o JVM is the Java Virtual Machine that actually executes Java programs. With JVM,
programs written in Java can run on multi-platforms (thus Java is called cross-platform
language).
o How to install JDK and configure environment variables.
o javac.exe is the Java compiler. It translates Java source code into bytecode.
o java.exe is the JVM launcher which we use to run our program.
o Every Java program starts from the main() method.
o When compiling, the compiler generates a .class file from a .java file.
http://www.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for-
beginners?utm_campaign=javatipseveryday&utm_medium=email&utm_source=getresponse
Configuring Sublime Text 2 editor to compile and run Java programs
1. Create a batch script file called runJava.bat with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1.......
IF EXIST %~n1.class (
DEL %~n1.class
)
javac %~nx1
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
java %~n1
)
The purpose of this script is to call Java compiler (javac) to compile the source file. Then if the
compilation succeeds (identified by checking if the .class file generated), call the Java launcher
(java) to run the program.
2. Save the runJava.bat under JDK’s bin directory, e.g. c:Program FilesJavajdk1.8.0bin.
3. Locate and open the JavaC.sublime-build file under this directory:
4.Replace the text “javac” by “runJava.bat”:
5.Then press Ctrl + B again, you would see the following result:
Dissecting my first Java program
public class Hello {
/**
* My first java program
*/
public static void main(String[] args) {
//prints the string "Hello world" on screen
System.out.println("Hello world!");
}
}
public class Hello indicates the name of the class which is Hello. In Java, all code should be
placed inside a class declaration. We do this by using the class keyword. In addition, the class
uses an access specifier public, which indicates that our class in accessible to other classes
from
other packages (packages are a collection of classes).
a curly brace { indicates the start of a block.
Java comment. A comment is something used to document a part of a code. It is not part of the
program itself but used for documentation purposes. It is good programming practice to add
comments to your code.
A comment is indicated by the delimiters “/*” and “*/”. Anything within these delimiters
are ignored by the Java compiler, and are treated as comments.
public static void main(String[] args) {
indicates the name of one method in Hello which is the main method. The main method
is the starting point of a Java program. All programs except Applets written in Java start
with the main method. Make sure to follow the exact signature.
The next line is also a Java comment,
//prints the string "Hello world" on screen
Now, we learned two ways of creating comments. The first one is by placing the
comment inside /* and */, and the other one is by writing // at the start of the
comment.
System.out.println("Hello world!");
prints the text “Hello World!” on screen. The command System.out.println(), prints the
text enclosed by quotation on the screen.
The last two lines which contains the two curly braces is used to close the main method
and class respectively.
Coding Guidelines:
1. Your Java programs should always end with the .java extension.
2. Filenames should match the name of your public class. So for example, if the name
of your public class is Hello, you should save it in a file called Hello.java.
3. You should write comments in your code explaining what a certain class does, orwhat a
certain method do.
Java Comments
Comments are notes written to a code for documentation purposes. Those text are not
part of the program and does not affect the flow of the program.
Java supports three types of comments: C++-style single line comments, C-style
multiline comments and special javadoc comments.
C++-Style Comments
C++ Style comments starts with //. All the text after // are treated as comments. For
example,
// This is a C++ style or single line comments
C-Style Comments
C-style comments or also called multiline comments starts with a /* and ends with a */.
All text in between the two delimeters are treated as comments. Unlike C++ style
comments, it can span multiple lines. For example,
/* this is an example of a C style or multiline comments */
Java Statements and blocks
A statement is one or more lines of code terminated by a semicolon. An example of a
single statement is,System.out.println(“Hello world”);
A block is one or more statements bounded by an opening and closing curly braces that
groups the statements as one unit. Block statements can be nested indefinitely. Any
amount of white space is allowed. An example of a block is,
public static void main( String[] args ){
System.out.println("Hello");
System.out.println("world");
}
Coding Guidelines:
1. In creating blocks, you can place the opening curly brace in line with the statement,
like for example,
public static void main( String[] args ){
or you can place the curly brace on the next line, like,
public static void main( String[] args )
{
2. You should indent the next statements after the start of a block,for example,
public static void main( String[] args ){
System.out.println("Hello");
System.out.println("world");
}

More Related Content

What's hot (20)

Java basic
Java basicJava basic
Java basic
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java features
Java featuresJava features
Java features
 
Unit2 java
Unit2 javaUnit2 java
Unit2 java
 
Mpl 1
Mpl 1Mpl 1
Mpl 1
 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
Java programming(unit 1)
Java programming(unit 1)Java programming(unit 1)
Java programming(unit 1)
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Introduction to java technology
Introduction to java technologyIntroduction to java technology
Introduction to java technology
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to Java
 
Java programming course for beginners
Java programming course for beginnersJava programming course for beginners
Java programming course for beginners
 
Java & advanced java
Java & advanced javaJava & advanced java
Java & advanced java
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 features
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Java notes
Java notesJava notes
Java notes
 
java tutorial for beginner - Free Download
java tutorial for beginner - Free Downloadjava tutorial for beginner - Free Download
java tutorial for beginner - Free Download
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Core java online training
Core java online trainingCore java online training
Core java online training
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 

Similar to Perform Object-Oriented Analysis and Design in Java

Similar to Perform Object-Oriented Analysis and Design in Java (20)

Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
Java intro
Java introJava intro
Java intro
 
R:\ap java\class slides\chapter 1\1 2 java development
R:\ap java\class slides\chapter 1\1 2 java developmentR:\ap java\class slides\chapter 1\1 2 java development
R:\ap java\class slides\chapter 1\1 2 java development
 
1 2 java development
1 2 java development1 2 java development
1 2 java development
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Introduction
IntroductionIntroduction
Introduction
 
Basics of java 1
Basics of java 1Basics of java 1
Basics of java 1
 
OOP with Java
OOP with JavaOOP with Java
OOP with Java
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 
Java introduction
Java introductionJava introduction
Java introduction
 
01slide
01slide01slide
01slide
 
Java introduction
Java introductionJava introduction
Java introduction
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
Java
JavaJava
Java
 
OOPS JAVA.pdf
OOPS JAVA.pdfOOPS JAVA.pdf
OOPS JAVA.pdf
 
Core java-introduction
Core java-introductionCore java-introduction
Core java-introduction
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
 
Java platform
Java platformJava platform
Java platform
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Perform Object-Oriented Analysis and Design in Java

  • 1. UNIT OF COMPETENCY: PERFORM OBJECT-ORIENTED ANALYSIS AND DESIGN IN JAVA TECHNOLOGY ELEMENT PERFORMANCE CRITERIA Italicized terms are elaborated in the Range of Variables 1 Apply Basics of Java language 1.1 Executable Java applications are created in accordance with Java framework 1.2 Java packages are imported to make them accessible in the code 1.3 Working with Java Data types is demonstrated in accordance with Java framework 1.4 Using Operators and Decision Constructs is demonstrated in accordance with Java framework 1.5 Creating and Using Arrays is demonstrated in accordance with Java framework 1.6 Using Loop Constructs is demonstrated in accordance with Java framework VARIABLE RANGE 1. Executable Java applications  Hello World  Hello with name  Hello with name and date 1. Working with Java Data Types  Declare and initialize variables  Differentiate between object references and primitive variables  Read and write to object fields  Explain an object’s lifecycle (creation, dereference, and garbage collection)  Call methods on objects  Manipulate data using StringBuilder class and its methods  Create and manipulate Strings 2. Using Operators and Decision Constructs  Use Java operators  Use parenthesis to override operator precedence  Test equality between strings and other objects using == and equals()  Create and use if-else constructs  Use a switch statement
  • 2. 3. Creating and Using Arrays  Declare, initialize, and use a one-dimensional array  Declare, initialize, and use a multi-dimensional array  Declare and use an ArrayList 4. Using Loop Constructs  Create and use while loops  Create and use for loops including the enhanced for loop  Create and use do-while loops  Compare loop constructs  Use break and continue Unit of Competency Learning Outcome Methodology Assessment Approach 1. Perform object- oriented analysis and design in Java technology 1.1 Apply basics of Java language  Lecture/ Discussion  Hands on  Exercises  Demonstration  Practical exam  Interviews/ questioning
  • 3. Objectives: At the end of the lesson the student should be able to: Identify the basic parts of a Java Program Execute basic Java Application Writing a Java hello world program 1 2 3 4 5 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } Save the file as HelloWorld.java (note that the extension is .java) under a directory, Every Java program starts from the main() method. This program simply prints “Hello world” to screen. Description The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter.
  • 4. Compiling it Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored: cd C:Java And type the following command: javac HelloWorld.java 5. Running it It’s now ready to run our first Java program. Type the following command: java HelloWorld That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output: What we have learnt so far Throughout this tutorial you have learnt the following things:  o JDK is the Java SE Development Kit that contains tools and libraries for Java development. o JRE is the Java Runtime Environment that enables running Java programs on your computer. o JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language). o How to install JDK and configure environment variables. o javac.exe is the Java compiler. It translates Java source code into bytecode. o java.exe is the JVM launcher which we use to run our program. o Every Java program starts from the main() method. o When compiling, the compiler generates a .class file from a .java file. http://www.codejava.net/java-core/how-to-write-compile-and-run-a-hello-world-java-program-for- beginners?utm_campaign=javatipseveryday&utm_medium=email&utm_source=getresponse
  • 5. Configuring Sublime Text 2 editor to compile and run Java programs 1. Create a batch script file called runJava.bat with the following content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @ECHO OFF cd %~dp1 ECHO Compiling %~nx1....... IF EXIST %~n1.class ( DEL %~n1.class ) javac %~nx1 IF EXIST %~n1.class ( ECHO -----------OUTPUT----------- java %~n1 ) The purpose of this script is to call Java compiler (javac) to compile the source file. Then if the compilation succeeds (identified by checking if the .class file generated), call the Java launcher (java) to run the program. 2. Save the runJava.bat under JDK’s bin directory, e.g. c:Program FilesJavajdk1.8.0bin. 3. Locate and open the JavaC.sublime-build file under this directory: 4.Replace the text “javac” by “runJava.bat”: 5.Then press Ctrl + B again, you would see the following result:
  • 6. Dissecting my first Java program public class Hello { /** * My first java program */ public static void main(String[] args) { //prints the string "Hello world" on screen System.out.println("Hello world!"); } } public class Hello indicates the name of the class which is Hello. In Java, all code should be placed inside a class declaration. We do this by using the class keyword. In addition, the class uses an access specifier public, which indicates that our class in accessible to other classes from other packages (packages are a collection of classes). a curly brace { indicates the start of a block. Java comment. A comment is something used to document a part of a code. It is not part of the program itself but used for documentation purposes. It is good programming practice to add comments to your code. A comment is indicated by the delimiters “/*” and “*/”. Anything within these delimiters are ignored by the Java compiler, and are treated as comments.
  • 7. public static void main(String[] args) { indicates the name of one method in Hello which is the main method. The main method is the starting point of a Java program. All programs except Applets written in Java start with the main method. Make sure to follow the exact signature. The next line is also a Java comment, //prints the string "Hello world" on screen Now, we learned two ways of creating comments. The first one is by placing the comment inside /* and */, and the other one is by writing // at the start of the comment. System.out.println("Hello world!"); prints the text “Hello World!” on screen. The command System.out.println(), prints the text enclosed by quotation on the screen. The last two lines which contains the two curly braces is used to close the main method and class respectively. Coding Guidelines: 1. Your Java programs should always end with the .java extension. 2. Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called Hello.java. 3. You should write comments in your code explaining what a certain class does, orwhat a certain method do. Java Comments Comments are notes written to a code for documentation purposes. Those text are not part of the program and does not affect the flow of the program. Java supports three types of comments: C++-style single line comments, C-style multiline comments and special javadoc comments. C++-Style Comments C++ Style comments starts with //. All the text after // are treated as comments. For example, // This is a C++ style or single line comments C-Style Comments C-style comments or also called multiline comments starts with a /* and ends with a */. All text in between the two delimeters are treated as comments. Unlike C++ style comments, it can span multiple lines. For example, /* this is an example of a C style or multiline comments */
  • 8. Java Statements and blocks A statement is one or more lines of code terminated by a semicolon. An example of a single statement is,System.out.println(“Hello world”); A block is one or more statements bounded by an opening and closing curly braces that groups the statements as one unit. Block statements can be nested indefinitely. Any amount of white space is allowed. An example of a block is, public static void main( String[] args ){ System.out.println("Hello"); System.out.println("world"); } Coding Guidelines: 1. In creating blocks, you can place the opening curly brace in line with the statement, like for example, public static void main( String[] args ){ or you can place the curly brace on the next line, like, public static void main( String[] args ) { 2. You should indent the next statements after the start of a block,for example, public static void main( String[] args ){ System.out.println("Hello"); System.out.println("world"); }