W3-Presentation-Programming Fundamentals
W3-Presentation-Programming Fundamentals
/**
* Sample Java Program
*/
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
Now let’s discuss the each part of the
sample program above.
1. The comment:
/**
* Sample Java Program
*/
2. Class definition
public class HelloWorld
-they are needed to define the block or the beginning and end of the
program or statement
4. Main Method
public static void main (String[ ] args)
6. Output Statement
System.out.println("Hello World!");
Reminders:
1. You should always save your Java program with a filename
extension .java.
2. Your java program filename should match the name of your class
declaration. For example, public class HelloWorld should be saved
as HelloWorld.java.
B. Java Comments
- Is an understandable explanation or notes of a code in a program and it
makes the code easier to understand.
- They are very useful, especially in a big project where several
programmers are working together and sharing codes with each other.
indent
System.out.println("Hello World!");
System.out.println("Welcome to Java!");
} //end block 1
} //end block 2
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
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
- Identifiers represent the name of variable, method, class,
package and interface.
Rules for an Identifier (Take note that invalid identifier will result to syntax
error.):
• Identifiers are case sensitive in Java. For example hello and Hello are
two different an identifiers in Java.
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)
5. For constant variable, the convention changes slightly, we need to capitalize all
the letters and separate the succeeding words with underscore. For example,
MAX_CREDIT = 10;
6. Avoid using underscore and dollar ($) sigh at the start of the identifier. We
only use underscore (at the start of an identifier) in an inevitable situation
where we need to use the reserved word as an identifier. For example, _for,
_true. And in some cases you may find a dollar sign (at the beginning of the
identifier) in auto-generated identifiers.
E. Java Keywords
-Keywords are reserved words defined by Java for specific purposes.
do instanceof strictfp
F. Java Literals
- is a constant value represented directly in the code. Literals
can be assigned to any primitive type variable.
• Character Literals
- are enclosed in single quotes; for example, 'a' can be stored in a
simple variable of char type.
A character literal can be:
\t Tab
\b Backspace
\n New line
\r Carriage return
\f Form Feed
\\ Backslash character
String Literals
- are constant value consist of zero or more characters
enclosed in double quotes, for example, “Hello World”.
- may contain escape sequence character.
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.
G. Primitive Data Type
- A data type is a classification of a particular type of data.
There are eight primitive data types in Java:
byte
•byte data type is an 8-bit signed integer
•byte data type can hold values from -128 to 127 (or -27 to 27 - 1)
•The default value for byte data type is 0
•It saves memory, it only consumes eight bits as against to 32 bits
for integer.
Examples are:
byte b1 = -60;
byte b2 = 101;
short
Examples are:
short s1 = -129;
short s2 = 128;
int
Examples are:
int i1 = -32769;
int i2 = 32768;
long
Examples are:
long l1 = -32769L;
long l2 = 32768L;
float
Examples are:
float f1 = -1.2345F;
float f2 = 3.4028235E+38F;
double
Variable Data Type identifies the kind of value that you can store, like number or
text.
Declaring and Initializing Variable
To declare a variable you need to have the following:
<data type> <name> [= initial value]
Data type and name are both required and initial value for variable is optional.
Below is the example of implementation of variables in a program:
The output of the program is:
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.
Line 5 or this code
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()
Below is the implementation System.out.print() in java program: