0% found this document useful (0 votes)
3 views47 pages

Basic-Syntax-and-Programming-Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views47 pages

Basic-Syntax-and-Programming-Fundamentals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

GETTING TO

KNOW YOUR
PROGRAMMING
ENVIRONMENT
OBJECTIVES
At the end of the lesson, student should be able to:
• Create a Java Program using text editor and console in the
Windows environment.
• Differentiate between syntax errors and runtime errors
• Create a Java program using NetBeans
DEFINITIONS
Console
- This is where you type in commands
- Ex: Command Prompt (Windows)
Text Editor
- Ex: Notepad, Wordpad
IDE
- Integrated Development Environment
- A programming environment integrated into a software
application that provides a GUI builder, a text or code
editor, a compiler and/or interpreter and a debugger.
SOFTWARE REQUIREMENTS
• NETBEANS IDE 8.2 OR HIGHER
- This would be the Integrated Development Environment
(IDE) that will be used in doing the laboratory exercises. To
download and for more information, visit http://www.netbeans.org/
• JAVA DEVELOPMENT KIT (JDK) 8.0 OR HIGHER
- This consists of the Java Runtime Environment plus
developer tools for compiling, debugging, and running
applications written in the Java language. For Solaris, Windows,
and Linux, you can download the JDK for your platform from
http://java.sun.com/. For Mac OS X, you can download the JDK
from Apple’s Developer Connection site starting
at:http://developer.apple.com/java (you must register to download
the JDK).
FIRST PROGRAM
1. Open Netbeans.
2. Create a new Java Project by selecting New Project under the File menu.
3. In the New Project dialog box, select “Java” under the list of categories then
selecct “Java Application” under the list of Projects. Click the “Next” button.
4. In the Project Name textbox, type “MyFirstProgram”. Click
finish.
5. The newly created “MyFirstProgram” project folder is located at the
Files/Project group. The main class of the project namely
“MyFirstProgram.java” is automatically generated and opened.
6. In the MyFirstProgram.java, the public class MyFirstProgram is
automatically generated along with its main method. Inside the main
method, type the statement System.out.println(“Hello World!”);
7. Run your program by selecting the “Run Project” under the Run menu or by
clicking the “Run Project” icon in the menu toolbar.
8. If the program does not encounter any error or bug, the output window will
appear and would display the output of the program.
ANATOMY OF A JAVA
PROGRAM
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks
CLASS NAME
- Every Java program must have at least one class.
Each class has a name. By convention, class
names start with an uppercase letter. In this
example, the class name is MyFirstProgram.

// This program prints Hello World!


public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
MAIN METHOD
- Line 2 defines the main method. In order to run a
class, the class must contain a method named main.
The program is executed from the main method.

// This program prints Hello World!


public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
STATEMENT
- A statement represents an action or a sequence of
actions. The statement System.out.println(“Hello
World!") in the program, a statement to display the
greeting Hello World!.

// This program prints Hello World!


public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
STATEMENT TERMINATOR
- Every statement in Java ends with a semicolon (;).

// This program prints Hello World!


public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
RESERVED WORDS
- Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be
used for other purposes in the program. For
example, when the compiler sees the word class, it
understands that the word after class is the name
for the class.
// This program prints Hello World!
public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
BLOCKS
- A pair of braces in a program forms a block that
groups components of a program.

public class MyFirstProgram {


public static void main(String[] args) { Class block
System.out.println("Hello World!"); Method block
}
}
SPECIAL SYMBOLS
Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.

// This program prints Hello World!


public class MyFirstProgram {

public static void main(String[] args) {


System.out.println("Hello World!");
}
}
NOTE:
1. The main method of the main class of a java project is the start of execution
when running the program; thus, initial code must be placed inside the main
method.
2. Statements are lines of codes in the program. Java statements always end
with a semicolon (;). In this example, the System.out.println(“Hello World!!!”);
is an output statement that displays the sentence “Hello World!!!”.
3. Blocks are group of statements which are often enclosed with a pair of curly
braces {}.
4. Bug is an error in the program.
5. Syntax Error is a bug that is encountered when the structure of a statement
does not conform with the java syntax. Syntax errors are detected during the
compilation of the program when being run i.e., syntax errors prevent
programs from being run until fixed.
6. Syntax is the set of rules followed in the structure of a statement.
7. Runtime Error is a bug that is encountered during the runtime or the actual
execution of the program. The error does not fall in the syntax of the
statements, rather, it is on the program flow which is only detected when the
program is running.
class

method

Code statement
CLASS
- Every Java program must have at least one class. Each class
has a name. By convention, class names start with an uppercase
letter. In this example, the class name is Main.
MAIN METHOD
- The program is executed from the main method. A class may
contain several methods. The main method is the entry point
where the program begins execution.
- A method is a construct that contains statements. The main
method in this program contains the System.out.println statement.
This statement displays the string Hello World!!! on the console.
String is a programming term meaning a sequence of characters.
A string must be enclosed in double quotation marks. Every
statement in Java ends with a semicolon (;), known as the
statement terminator.
- Reserved words, or keywords, have a specific meaning to the
compiler and cannot be used for other purposes in the program.
For example, when the compiler sees the word class, it
understands that the word after class is the name for the class.
Other reserved words in this program are public, static, and void.
PROGRAMMING
ERRORS
Syntax Errors
• Detected by the compiler
Runtime Errors
• Causes the program to abort
Logic Errors
• Produces incorrect result
ERRORS: SYNTAX ERRORS
- Errors that are detected by the compiler are called syntax
errors or compile errors. Syntax errors result from errors in
code construction, such as mistyping a keyword, omitting some
necessary punctuation, or using an opening brace without a
corresponding closing brace.
EXAMPLE OF SYNTAX
ERROR
EXAMPLE OF SYNTAX
ERROR

1. The keyword void is missing before main in line 2.


2. The string Welcome to Java should be closed with a closing
quotation mark in line 3
ERRORS: RUNTIME
ERRORS
- Runtime errors are errors that cause a program to terminate
abnormally. They occur while a program is running if the
environment detects an operation that is impossible to carry
out. Input mistakes typically cause runtime errors.
EXAMPLE OF
RUNTIME ERROR
EXAMPLE OF
RUNTIME ERROR
APPROPRIATE COMMENTS
AND COMMENT STYLES
- Include a summary at the beginning of the program that
explains what the program does, its key features, and any
unique techniques it uses. In a long program, you should also
include comments that introduce each major step and explain
anything that is difficult to read. It is important to make
comments concise so that they do not crowd the program or
make it difficult to read.
- Comments serve as the programmer’s guide or notes in the
program. Comments can be written anywhere in the program,
but it is ignored during the program execution and does not
affect the program flow. There are three ways to write
comments in Java:
THREE WAYS OF
COMMENTS
1. C++ Style Comments – Comments start “//”. All the text after //
are treated as comments. This style is often used for singe-
line comments. For example:
//This is a C++ style line comments
2. C – Style Comments – Also called multiline comments, it
starts with a “/*” and ends with a “*/”. All text in between the
two delimiters are treated as comments. Unlike C++ style
comments, it can span multiple lines. For example:
/*this is an example
of a C-style or
multiline comments*/
THREE WAYS OF
COMMENTS
3. Special Javadoc Comments – Special Javadoc comments are used
for generating an HTML documentation for a Java program. Javadoc
comments can be created by starting with “/**”, suceeding lines start
with “*” and the end is “*/”. Like C-style comments, it can also span
lines. It can also contains certain tags to add more information to the
comments. For example,
/**
* This is an example of special java doc comments used for \n
* generating an html documentation. It used tags like:
* @author Florence Balagtas
* @version 1.2
*/
BLOCK STYLES
- A block is a group of statements surrounded by braces. There
are two popular styles, next-line style and end-of-line style, as
shown below.

- The next-line style aligns braces vertically and makes


programs easy to read, whereas the end-of-line style saves
space and may help avoid some subtle programming errors.
Both are acceptable block styles. The choice depends on
personal or organizational preference. You should use a block
style consistently—mixing styles is not recommended.
IDENTIFIERS
- Identifiers are the names that identify the elements
in a program.
- Represents names of classes, methods, variables,
etc.
- Identifiers must begin with either a letter, an
underscore “_”, or a dollar sign “$”. Letters may be
lower or uppercase. Identifiers must not contain
spaces and cannot use Java keywords like class,
public, void, etc. Java identifiers are case-sensitive.
EXAMPLE JAVA
IDENTIFIERS:
• legal:olivia second_place _myName
TheCure ANSWER_IS_42 $variable

• illegal: me+u :-)


side-swipe hi there ph.d
belles's 2%milk
kelly@yahoo.com
KEYWORDS
- An identifier that you cannot use, because it already has a reserved meaning in the
Java language. Keywords cannot be used as names for variables, classes, methods,
etc.

Complete list of Java keywords:


abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
LITERALS
- Literals are tokens that do not change or are constant. The
different types of literals in Java are: Integer Literals, Floating –
Point Literals, Boolean Literals, Character Literals and String
Literals.

INTEGER LITERALS
- Integer literals come in different formats: decimal (base10),
hexadecimal (base 16), and octal (base 8). In using integer
literals, special notation should be followed.
- By default, an integer literal is a decimal integer number. The
decimal number is written as it is. To denote an octal integer
literal, use a leading 0 (zero), and to denote a hexadecimal
integer literal, use a leading 0x or 0X (zero X).
- Example: int i = 123;
- System.out.println(156);
LITERALS
FLOATING – POINT LITERALS
- Floating-point literals are written with a decimal point. By
default, a floating-point literal is treated as a double type
value. For example, 5.0 is considered a double value, not a
float value. You can make a number a float by appending the
letter f or F.
- Floating point literals can be also expressed in standard or
scientific notations. For example, 583.45 is in standard
notation, while 5.8345e2 is in scientific notation.
- A float value has 7 to 8 number of significant digits and a
double value has 15 to 17 number of significant digits.
- Example: System.out.println(15.6);
- System.out.println(9.6F);
LITERALS
BOOLEAN LITERALS
- Boolean literals have only two values, true or false.
- Example: boolean b1 = true;
CHARACTER LITERALS
- Stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes.
- Example: char myLetter = 'D';
STRING LITERALS
- Stores text, such as "Hello". String values are surrounded by
double quotes.
- Example: String s = "hello";
- System.out.println("This is some text");
DATA TYPES
- Data types are used to specify the type of data or value a
variable would hold.
There are eight primitive data types in Java:
NUMBERS
Primitive number types are divided into two groups:
Integer types stores whole numbers, positive or
negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long. Which type you
should use, depends on the numeric value.
Floating point types represents numbers with a
fractional part, containing one or more decimals. There
are two types: float and double.
INTEGER TYPES
Byte
The byte data type can store whole numbers from -128 to 127.
This can be used instead of int or other integer types to save
memory when you are certain that the value will be within -128
and 127:
Example: byte myNum = 100;
System.out.println(myNum);
Short
The short data type can store whole numbers from -32768 to
32767:
Example: short myNum = 5000;
System.out.println(myNum)
INTEGER TYPES
Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is the
preferred data type when we create variables with a numeric
value.
Example: int myNum = 100000;
System.out.println(myNum);
Long
The long data type can store whole numbers from -
9223372036854775808 to 9223372036854775807. This is used
when int is not large enough to store the value. Note that you
should end the value with an "L":
Example: long myNum = 15000000000L;
System.out.println(myNum);
FLOATING POINT
TYPES
You should use a floating point type whenever you need a number
with a decimal, such as 9.99 or 3.14515.
The float and double data types can store fractional numbers.
Note that you should end the value with an "f" for floats and "d" for
doubles:
Float Example
float myNum = 5.75f;
System.out.println(myNum);

Double Example
double myNum = 19.99d;
System.out.println(myNum);
JAVA BOOLEAN DATA
TYPES
Boolean Types
Very often in programming, you will need a data type that can only
have one of two values, like:
• YES / NO
• ON / OFF
• TRUE / FALSE
For this, Java has a boolean data type, which can only take the
values true or false:
Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
JAVA CHARACTERS
Characters (Char)
The char data type is used to store a single character. The
character must be surrounded by single quotes, like 'A' or 'c':
Example:
char myGrade = 'B';
System.out.println(myGrade);

You might also like