Comprog 4 - Programing Fundamentals
Comprog 4 - Programing Fundamentals
Programming Fundamentals
In this module we are going to discuss the basic parts of Java program
and coding guidelines to write readable programs.
/**
* Sample Java Program
*/
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
Always remember that Java code is case sensitive. Now let’s discuss
the each part of the sample program above.
1. The comment:
/**
* Sample Java Program
*/
Computer Programming 2 1
Week 3-4 Programming Fundamentals
2. Class definition
public class HelloWorld
3. Left curly brace (or opening curly brace) after class declaration
{
A set of curly braces { } is needed for every class. Curly braces are needed to
define the block or the beginning and end of the program or statement. The
opening curly brace indicates the beginning of the block of the statement.
2
MODULE OF INSTRUCTION
4. Main Method
public static void main (String[ ] args)
This part defined the main method of the program. The main method is
needed to start the execution of the program or it is executed first therefore
you need to have at least one method named main. You will not be able to run
a Java program without a main method. The static and void keyword will be
discussed later. The String args[] represents an array of String parameter, this
will also further discuss later. The public keyword is also the same as the
public defined in a class definition, it is an access modifier that sets the
accessibility of the method, in this case our main method is accessible
anywhere, this will be discuss further later.
5. Left curly brace (or opening curly brace) after class main method
{
6. Output Statement
System.out.println("Hello World!");
The right curly brace (}) represents the end of the block of codes. The
two right curly braces are used to end the main method and class definition.
Reminders:
Computer Programming 2 3
Week 3-4 Programming Fundamentals
B. Java Comments
2. Multi-line comments.
This comment is used for block commenting or series of multiple lines
of code. It starts with /* then followed by the text you want to comment and
then ends with */. All text inside the /* */ will be treated as comments. For
example:
/* This multi-line comment,
it can support multiple
line of codes. */
3. Documentation Comments
It is almost the same as multi-line comment that covers a block of
codes commenting but has a special purpose. Documentation comments are
used by the JDK java doc tool to generate HTML documentation for your
Java programs.
/**
* The HelloWorld program is an application that \n
* simply displays "Hello World!".
4
MODULE OF INSTRUCTION
*
* @author Juan Dela Cruz
* @version 1.0
*/
Coding Guidelines:
1. In block, you should place opening curly brace in line with the statement.
For example:
public static void main(String[] args){
2. Always indent the statements after the begin block, for example:
public class HelloWorld{ //begin block 1
public static void main(String[] args){ //begin block 2
System.out.println("Hello World!");
System.out.println("Welcome to Java!");
} //end block 1
} //end block 2
Computer Programming 2 5
Week 3-4 Programming Fundamentals
3. Closing curly brace should be vertically aligned with the statement that
defines the block (they should be on the same column number). For
example,
public class HelloWorld{ //begin block 1
public static void main(String[] args){ //begin block 2
System.out.println("Hello World!");
System.out.println("Welcome to Java!");
} //end block 1
} //end block 2
D. Identifiers
Rules for an Identifier (Take note that invalid identifier will result to syntax
error.):
▪ Identifier must start with a letter, an underscore or a dollar sign.
▪ Identifier must only contain letters, numbers, underscores or dollar
signs.
▪ Special characters, such as a semicolon, period, whitespaces, slash or
comma are not allowed to be used as Identifier.
▪ Identifiers are case sensitive in Java. For example hello and Hello are
two different an identifiers in Java.
▪ Java Keywords is not allowed to use as an identifier. We are going to
identify all the Java reserved words later.
Coding Guidelines:
1. Use meaningful, descriptive or straight forward identifier, for example, if
you have a method that computes for the grade, name it computeGrade.
6
MODULE OF INSTRUCTION
2. Avoid using abbreviations for identifiers and use complete words to make
it readable.
3. In naming classes you should capitalize the first letter and for methods and
variables the first letter should be in lowercase. For example:
public class Hello (class identifier Hello starts with a capital letter)
public void main (method declaration main starts with small letter)
4. For multi-word identifiers use camel case. Camel case may start with a
capital letter or with a lowercase letter and all the succeeding words must
start with a capital letter. For example,
HelloWorld (this is a class identifier)
computeGrade (this is method identifier)
firstName (this is a variable identifier)
E. Java Keywords
Computer Programming 2 7
Week 3-4 Programming Fundamentals
In addition to the keywords listed above true, false and null are also
reserved words. The keywords goto and const are keywords reserved in
other programming languages like C, C++, c#, etc. but currently not used
in Java. There will be a discussion of each keyword as we go along the
way.
F. Java Literals
Integer Literal
An integer literal can be stored to an integral type variable. It can be a
decimal, binary, octal or hexadecimal constant. There is a format that we need
to follow in order to use integer literal. To represent a binary integer using a
prefix 0b or 0B (zero B), to represent hexadecimal integer use a prefix 0x or
0X (zero X), for octal use a prefix 0 (zero) and decimal has no prefix.
The following are the examples of integer literal in a program:
System.out.println(42); //Displays 42
System.out.println(0b101010); //Displays 42
System.out.println(0x2A); //Displays 42
System.out.println(052); //Displays 42
By default, the integer literal data type is int. An int value is between
-2147483648 and 2147483647. In case, that you want to use long type literal
you need to append the letter "L" or "l" on it. For example, to use
21474836470 in a Java program, you have to write it as 21474836470L,
because if you didn’t append “L” on the literal you will get an error since the
value exceeds the range for int value. We should use the L suffix in long type
integer literal because l (lowercase L) can easily be confused with 1 (the digit
one).
The following are the examples of integer literal with long data type in
a program:
System.out.println(21474836470L); // Displays 21474836470
System.out.println(0b101010L); // Displays 42
System.out.println(0237777777766L); //Displays 21474836470
System.out.println(0x4FFFFFFF6L); // Displays 21474836470
8
MODULE OF INSTRUCTION
Floating-Point Literals
Floating-point literal is an integer literal followed by a decimal point.
By default, the floating-point literal data type is double, but if you want to
explicitly express that the floating point literal is a double type value you can
append d or D on it and in case that you need to use type float value you need
to append f or F on it. For example, you can use 3.1416f or 3.1416F for a float
value, and 3.1416, 3.1416d or 3.1416D for a double value.
Boolean Literals
Boolean literals have only two possible values, true and false.
Character Literals
Character literals are enclosed in single quotes; for example, 'a' can be
stored in a simple variable of char type.
\t Tab
\b Backspace
\n New line
\r Carriage return
Computer Programming 2 9
Week 3-4 Programming Fundamentals
\f Form Feed
\' Single quote character
\" Double quote character
\\ Backslash character
String Literals
String literals are constant value consist of zero or more characters
enclosed in double quotes, for example, “Hello World”.
String literals may contain escape sequence character. When an escape
sequence is used in a print statement, it will be evaluated according to its
purpose. For example, if you want to print a text that is enclosed in double
quotes you need to use the escape sequence \". The code below demonstrates
the printing of text enclosed in double quotes:
System.out.println("Escape sequence \"double quote\" demo.");
The code above will display:
Escape sequence "double quote" demo.
byte
short
10
MODULE OF INSTRUCTION
• short data type can hold values from -32768 to 32767 (or -215
to 215 - 1)
• The default value for short data type is 0
• Like bytes, it also saves memory and better alternative to int
data types, particularly if your data falls within the specified
range.
• Examples are:
short s1 = -129;
short s2 = 128;
int
long
float
double
Computer Programming 2 11
Week 3-4 Programming Fundamentals
boolean
char
12
MODULE OF INSTRUCTION
H. Variables
Data type and name are both required and initial value for variable is optional.
Computer Programming 2 13
Week 3-4 Programming Fundamentals
is a variable declaration with initial value. The word meter is the variable
name, int is the data type and 2 is the initial value.
is also a variable declaration and it has a variable name of centimeter and int
data type and has no initial value.
will just print the value inside the println. The + sign in the code is used to
combine or concatenate the values in between the symbol. In this case the
statement will print: 2 m = 200 cm.
Printing Variable
There are two statements that you can use to display or output the value from
a variable, these are the following:
▪ System.out.println()
▪ System.out.print()
The two statements are both used to display value, the only difference
is the System.out.println() appends newline after it display the value while the
other one does not.
14
MODULE OF INSTRUCTION
Computer Programming 2 15