02 Java Language Constructs
02 Java Language Constructs
Construct
Lesson 2
Content
/*
Declaration Order * Created on Jul 14, 2005
*
* First Java Program
*/
package com.jds.sample;
1. Package declaration import java.util.*;
used to organize a collection of /**
classes. * @author JDS
*/
public class JavaMain {
2. Import statement public static void main(String[] args) {
used to reference classes and // print a message
declared in other packages. System.out.println("Welcome to Java!");
}
3. Class declaration }
a Java source file can have several
classes but only one public class is class Extra {
allowed /*
* class body
*/
}
Comments /*
* Created on Jul 14, 2005
1. Single Line Comment *
* First Java Program
// insert comments here */
2. Block Comment package com.jds.sample;
/* import java.util.*;
* insert comments here /**
*/ * @author JDS
3. Documentation Comment */
public class JavaMain {
/** public static void main(String[] args) {
* insert documentation // print a message
*/ System.out.println("Welcome to Java!");
}
Whitespace }
class Extra {
Tabs and spaces are ignored by the /*
compiler. Used to improve readability * class body
of code. */
}
/*
* Created on Jul 14, 2005
*
Class * First Java Program
*/
• Every java program includes at least one class package com.jds.sample;
definition. import java.util.*;
/**
• The class is the fundamental component of all * @author JDS
Java programs. */
public class JavaMain {
public static void main(String[] args) {
• A class definition contains all the variables // print a message
and methods that make the program work. System.out.println("Welcome to Java!");
}
• This is contained in the class body indicated }
by the opening and closing braces.
class Extra {
/*
* class body
*/
}
/*
Braces * Created on Jul 14, 2005
*
* First Java Program
• Braces are used for grouping statements or
*/
block of codes. package com.jds.sample;
import java.util.*;
• The left brace ( { ) indicates the beginning of a /**
class body, which contains any variables and * @author JDS
methods the class needs. */
public class JavaMain {
• The left brace also indicates the beginning of a public static void main(String[] args) {
method body. // print a message
System.out.println("Welcome to Java!");
}
• For every left brace that opens a class or }
method you need a corresponding right brace (
} ) to close the class or method.
class Extra {
/*
• A right brace always closes its nearest left * class body
brace */
}
/*
main() method * Created on Jul 14, 2005
*
* First Java Program
*/
This line begins the main() method. This
package com.jds.sample;
is the line at which the program will import java.util.*;
begin executing. /**
* @author JDS
*/
public class JavaMain {
String [ ]args public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
Declares a parameter named args, which }
is an array of String. It represents
class Extra {
command-line arguments. /*
* class body
*/
}
/*
Java statement * Created on Jul 14, 2005
*
* First Java Program
• A complete unit of work in a Java */
program. package com.jds.sample;
import java.util.*;
• A statement is always terminated with a /**
* @author JDS
semicolon and may span multiple lines in
*/
your source code. public class JavaMain {
System.out.println( ) public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
This line outputs the string “Welcome to }
Java!” followed by a new line on the
class Extra {
screen. /*
* class body
*/
}
• A data type determines the values that a variable can contain and
the operations that can be performed on it
Aristophanes