Java Fundamentals
Java Fundamentals
Fundamentals
OBJECTIVES
At the end of the lesson, the student should be
able to:
• Identify the basic parts of a Java program
• Differentiate among Java literals, primitive
data types, variable types ,identifiers and
operators
• Develop a simple valid Java program using
the concepts learned in this chapter
DISSECTING MY FIRST JAVA
PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
1 }
2 }
DISSECTING MY FIRST JAVA PROGRAM
1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
● indicates the name of the class which is Hello
● In Java, all code should be placed inside a class
declaration
● 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). We will
be covering packages and access specifiers later.
DISSECTING MY FIRST JAVA PROGRAM
1 }
2 }
● 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.
• 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
Java 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 exmaple of a
C style or multiline comments
*/
Java Comments
• Special Javadoc Comments
– Special Javadoc comments are used for generating an HTML
documentation for your Java programs.
– You can create javadoc comments by starting the line with /**
and ending it with */.
– Like C-style comments, it can also span lines.
– It can also contain certain tags to add more information to your
comments.
– For example:
/** This is an example of special java doc
comments used for \n generating an
html
documentation. It uses tags like:
@author Florence Balagtas
@version 1.2
*/
JAVA STATEMENTS
• Statement
–one or more lines of code
terminated by a semicolon.
–Example:
System.out.println(“Hell
o world”);
JAVA BLOCKS
• 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.
– Example:
public static void main( String[]
args ){
System.out.println("Hello");
System.out.println("world”);
}
Java Statements and Blocks
Coding Guidelines
1. In creating blocks, you can place the opening curly
brace in line with the statement. 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
)
{
JAVA STATEMENTS AND BLOCKS
CODING GUIDELINES
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");
}
JAVA IDENTIFIERS
• Identifiers
– are tokens that represent names of variables,
methods, classes, etc.
– Examples of identifiers are: Hello, main,
System, out.
• Unicode character
– a 16-bit character set that replaces the 8-bit
ASCII character set.
– Unicode allows the inclusion of symbols and
special characters from other languages.
Java Literals: Character
• To use a character literal, enclose the character
in single quote delimiters.
• For example
– the letter a, is represented as ‘a’.
– special characters such as a newline character, a
backslash is used followed by the character code. For
example, ‘\n’ for the newline character, ‘\r’ for the
carriage return, ‘\b’ for backspace.
Java Literals: String
• An example is,
boolean result = true;
• For example,
String message=“Hello world!”;
Primitive Data Types: Integral – byte,
short, int & long
• Integral data types in Java uses three forms –
decimal, octal or hexadecimal.
• Examples are,
2 //The decimal value 2
077 //The leading 0 indicates an octal value
0xBACC //The leading 0x indicates a hex value
• Integral types has int as default data type.
• You can define its long value by appending the
letter l or L.
• For example:
10L
Primitive Data Types: Integral – byte,
short, int & long
• Integral data type have the
following ranges:
Primitive Data Types: Integral – byte,
short, int & long
• Coding Guidelines:
– In defining a long value, a lowercase L is not
recommended because it is hard to distinguish
from the digit 1.