201 Lecture 2 2024
201 Lecture 2 2024
Java Syntax
Every line of code that runs in Java must be inside a class. We will name the class Main in the
coming example below. A class should always start with an uppercase first letter.
The name of the java file must match the class name. When saving the file, save it using the class
name and add ".java" to the end of the filename.
The main Method
The main() method is required and you will see it in every Java program:
Any code inside the main() method (between the curly braces {}) will be executed. Don't worry
about the keywords before and after main..
For now, just remember that every Java program has a class name which must match the
filename, and that every program must contain the main() method System.out.println()
Inside the main() method, we can use the println() method to print a line of text to the screen:
System.out.println("Hello world");
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
String:
A string represents a sequence of characters for example "Javatpoint", "Hello world", etc. String
is the class of Java.
One of the ways to create a string and store a value in it is shown below:
String str = "You're the best";
Here, String type variable str has the value "You're the best".
Array:
An array is a data type which can store multiple homogenous variables i.e., variables of same
type in a sequence. They are stored in an indexed manner starting with index 0. The variables
can be either primitive or non-primitive data types.
Java Variables
A variable is the name of a reserved area allocated in memory. In other words, it is a name of the
memory location. It is a combination of "vary + able" which means its value can be changed
e.g. int data=50;//Here data is variable.
Types of Variables
There are three types of variables in Java:
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can create a
single copy of the static variable and share it among all the instances of the class. Memory
allocation for static variables happens only once when the class is loaded in the memory.
Example to understand the types of variables in java
Reserved Words
Which Words are reserved in Java?
Keywords are special identifiers that are reserved by the compiler. You can use a keyword only
as a keyword, not as an identifier name or function name. Case is significant in keywords, so
"exit" is a reserved word, but "EXIT" and "Exit" are not.
Type names, shown in CAPITALS below, are also reserved by the compiler. You can use a type
name only as a type name, not as an identifier name or function name. Unlike keywords, type
names are not case-sensitive, so "CHAR" and "char" are both reserved words.
The Boolean constants TRUE and FALSE are also reserved by the compiler and are case
insensitive.
The following are reserved words in the 4Test language:
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
• Java Operator Precedence
Java Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
Java Arithmetic Operator Example
Java Arithmetic Operator Example: Expression
The Java right shift operator >> is used to move the value of the left operand to right by the
number of bits specified by the right operand.
Java Right Shift Operator Example
Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn't check the second condition if the first condition is false. It
checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
Syntax Error
Logical Error
Run Time Error
Syntax Error
The syntax error occurs when one of the java rules is compromised. The line of code that has the
error will be underline with a red line. Some of the rule that triggers syntax errors includes;
misspelled of java key words, termination at the end of line, inappropriate use of brackets,
coding out of main method or class, a wrong identifier, wrong data type and etc.
Example
Explanation;
Line 3. A variable X is declared as type int but the value initialized to it was of type double.
Line 4. A variable Y is declared as double but a string value is initialized to it.
Line 5. A variable Z is declared as int data type. Similarly, int value is saved in it but the error
is that the line is not terminated with semicolon.
Line 6. The data type is correct, the value initialized to the data type is correct, the line has been
terminated but the variable is not correct
Logical Error
Logical errors are an error that yields incorrect output; this type of errors allows the programs to
execute but gives out the incorrect results. It usually happens when someone use greater than
instead of less than, plus instead of minus, ++ instead of - -, and etc. so the compiler is not smart
enough to know that user is not meant the type of operations he/she used.
Example: “the below example is the simulation of the Nigerian System of voting. According to
the law only citizen that their age is greater or equal to 18 can vote. But in this example a person
of 10years is allow to vote, simply because we made logical errors, we use less than instead of
greater than.”
Output
Runtime Errors
Runtime Errors occur during the execution of a program, due to lack of system resources, or due
to irrelevant input by the user. The compiler has no idea whatsoever how to detect these kinds of
errors. For example, dividing a number by 0, accessing an element from an array that is out of
range, trying to convert an invalid string to an integer, out of memory error, etc.
Example; “In the example, we divide the number by 0 which is not possible, as a result it gives
us runtime error”.
Output