Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Analysis of a basic java program
1. Analysis of a basic java program:
HelloWorld
Sujit Kumar
Zenolocity LLC
2. HelloWorld.java
package com.bootcamp.basic.java;
import java.lang.String; //default import, no need
public class HelloWorld {
public static void main(String[] args) {
String firstName = args[0];
String lastName = args[1];
System.out.println(“Hello, how are you?” +
firstName + “ “ + lastName);
} // end of main
} // end of class
3. 3 Sections in a Java Class
• The order of these 3 sections cannot change.
• Package – optional, if you don’t provide it, it uses
the default package (has no name).
• Imports – is also optional, you use it only when
you have to import classes from other packages.
Everything from java.lang package is imported by
default.
• Class Definition - between { and }, uses the java
keyword class and the name of a class.
The class has a main method – special method,
start of program execution.
4. Java is Case Sensitive
• Filename should be the same as class name.
• Java is case sensitive, so case of the file should
match the case of the class.
• Examples:
if class is HelloWorld, file should be
HelloWorld.java
if class is helloWorld, file should be
helloWorld.java
5. The main method
• Special method, start of program execution.
• Has restrictions on signature of the main method,
needs to be public and static and take an array of
Strings as input.
• These array of Strings are parameters passed to
your program & may not be used at all.
• An array is a collection of elements accessed with
an index, first index starts from 0.
• Keywords: public is an access modifier, static
indicates a class variable/method, void meets
nothing is returned from the method.
6. Main Method
• It contains a set of statements executed from top
to bottom. These statements are the logic of your
program.
• Every statement ends with a semi-colon.
• You can have program flow control statements
like if, while & for loops inside the main method.
• System.out.println is used to print to the console
(screen) and ends with a new line.
System.out.print does not print a new line.
7. Comments
• Single line comments start with //
• Multi-line comments:
Start with /* and end with */
• Comments are never compiled and are meant
to convey information to the reader of your
programs (other programmers).
9. How to compile & run the program
from the command line?
• Compilation via javac.
• javac HelloWorld.java
The above command produces HelloWorld.class.
• Execution:
java HelloWorld
• Execution with parameters:
java HelloWorld <space separated arguments>
Example:
java HelloWorld Sujit Kumar
10. How to compile & run from Eclipse
• Compile: Menu – Project – Build Automatically
• Run:
Right Click on the open java program
Run As – Java Application
• Run with arguments
Right Click on the open java program
Run As – Open Run Dialog
11. JDK 1.6 and 1.7 APIs
• http://docs.oracle.com/javase/6/docs/api/
• http://docs.oracle.com/javase/7/docs/api/