You are on page 1of 23

Java Language

Construct
Lesson 2
Content

• Java Source File Structure


• Java Keywords
• Identifier
• Literals
• Variables & Data Types
• Variable Declaration & Initialization

Computer Programming 2 | Philip A. Vega 2


Take Away Points

At the end of the session you will able to:


• identify a properly-constructed Java source file
• identify all Java Keywords
• distinguish between legal and illegal identifiers
• write correct Java literals
• enumerate all the primitive data types and their ranges
• properly declare and initialize variables

Computer Programming 2 | Philip A. Vega 3


Java Source File
Structures
Lesson 2
Java Source File Structures

/*
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
*/
}

Computer Programming 2 | Philip A. Vega 5


Java Source File Structures

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. */
}

Computer Programming 2 | Philip A. Vega 6


Java Source File Structures

/*
* 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
*/
}

Computer Programming 2 | Philip A. Vega 7


Java Source File Structures

/*
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 */
}

Computer Programming 2 | Philip A. Vega 8


Java Source File Structures

/*
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
*/
}

Computer Programming 2 | Philip A. Vega 9


Java Source File Structures

/*
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
*/
}

Computer Programming 2 | Philip A. Vega 10


Java Keywords
Lesson 2
Java Keywords

Computer Programming 2 | Philip A. Vega 12


Identifiers

• An identifier is the name given by a


programmer to a variable, statement label,
method, class, and interface.

• An identifier must begin with a letter, $ or _

• Subsequent characters must be letters,


numbers, $ or _

• An identifier must not be a Java keyword

• Identifiers are casesensitive

Computer Programming 2 | Philip A. Vega 13


Literals

• A literal is a representation of a value of a particular type

Computer Programming 2 | Philip A. Vega 14


Variables and Data
Types
Lesson 2
Variables and Data Types

• A variable is a named storage location used to represent data that


can be changed while the program is running.

• A data type determines the values that a variable can contain and
the operations that can be performed on it

Computer Programming 2 | Philip A. Vega 16


Data Types

Categories Of Data Types:


1. Primitive data types
2. Reference data types

Computer Programming 2 | Philip A. Vega 17


Primitive Data Types

• Primitive data types represent atomic values and are built-in to


Java.
• Java has 8 primitive data types.

Computer Programming 2 | Philip A. Vega 18


Reference Data Types

• Reference data types represent objects

• A reference serves as a handle to the object, it is a way to get to the


object.

• Java has 3 reference data types


1. Class
2. Array
3. Interface

Computer Programming 2 | Philip A. Vega 19


Variable Declaration and
Initialization

• Declaring a variable with primitive data type.

• Declaring a variable with reference data type.

Computer Programming 2 | Philip A. Vega 20


Computer Programming 2 | Philip A. Vega 21
Summary

• A Java source file can include package, import, and class


declarations in that order
• The main() method is the start of execution of a Java application
• Each Java statement is terminated by a ;
• Identifiers are case sensitive
• Java keywords cannot be used as identifiers
• Each variable must be declared with a data type
• There are 8 primitive data types: boolean, char, byte, short, int,
long, float, double
• There are 3 reference data types: class, array, interface

Computer Programming 2 | Philip A. Vega 22


That All for Now!!

High thoughts must have high


language…

Aristophanes

Computer Programming 2 | Philip A. Vega 23

You might also like