Basic-Syntax-and-Programming-Fundamentals
Basic-Syntax-and-Programming-Fundamentals
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.
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
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
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);