Java Programming – Mid Term Exam
What is Java?
Java is a popular and powerful programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used
for: Mobile applications (specially Android apps); Desktop applications; Web applications; Web servers and application servers; Games; Database
connection; And much more!
Why Use Java?
▪ Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
▪ It is one of the most popular programming languages in the world
▪ It has a large demand in the current job market
▪ It is easy to learn and simple to use
▪ It is open-source and free
▪ It is secure, fast and powerful
▪ It has huge community support (tens of millions of developers)
▪ Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
▪ As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
Platform Independency – A Key Feature of Java
▪ Java is platform-independent, which means a Java program can run on any device or operating system that has a Java Virtual Machine (JVM).
▪ This is achieved through bytecode: when you compile a Java program, it is converted into platform-neutral bytecode, not machine-specific code.
▪ The JVM on each device interprets or compiles the bytecode into machine code suitable for that platform.
▪ This principle is summarized by the famous Java slogan: “Write Once, Run Anywhere (WORA).”
Java Keywords:
Java Keywords are few reserved words that have predefined meanings and can not be used as variable names. Let’s get to know some of them.
Keyword Purpose
int Declares an integer variable do Loop statement
float Declares a floating-point variable switch Multi-branch selection
double Declares a double-precision floating-point variable case Defines a branch in switch
char Declares a character variable break Exits a loop or switch
boolean Declares a boolean variable (true or false) continue Skips current loop iteration
if Conditional statement class Declares a class
else Conditional alternative public Access modifier
for Loop statement private Access modifier
while Loop statement protected Access modifier
Identifiers
Definition: An identifier is a name given to elements in Java such as variables, methods, classes, or objects. Identifiers are user-defined names and
are used to uniquely identify program elements.
Variables: A variable is a container which is used to store data. A kind of identifier. Each variable has a specific data type.
Rules for Java Identifiers and variable
▪ Can contain letters (A–Z, a–z), digits (0–9), underscores (_), and dollar signs ($).
▪ Cannot start with a digit.
▪ Cannot be a Java keyword (e.g., int, class, if).
▪ Case-sensitive (myVar and MyVar are different).
▪ No spaces or special characters other than _ and $.
Java Programming – Mid Term Exam
Data Types
Primitive Data Types: Holds a value, specifies the size and type of a variable.
Non-Primitive Data Types: Holds a reference to an object or location.
Operators: Operators are symbols used to perform specific operations on one or more operands to produce a result.
Practice those:
Java Programming – Mid Term Exam
Java User Input
What is a Java Scanner Class?
▪ A predefined class that reads data from the keyboard or a text file.
▪ Found in the [Link] package.
▪ Can read all types of data (numeric, strings and other data types).
▪ Easiest way to take input from the user.
Create an object of the Scanner class
Example
Type casting
Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting:
▪ Widening Casting (automatically) - converting a smaller type to a larger type size: byte -> short -> char -> int -> long -> float -> double
▪ Narrowing Casting (manually) - converting a larger type to a smaller size type: double -> float -> long -> int -> char -> short -> byte
Widening Casting Narrowing Casting (manually) (target-type) value
int num1 = 555; double numd = 11.1234;
double num2 = num1; // Automatic casting: int to double int numi = (int) numd; // Manual casting: double to int
[Link](num1); [Link](numd);
[Link](num2); [Link](numi);
Java Programming – Mid Term Exam
What is an error?
▪ A problem or exception that occurs during the execution of a program.
▪ Prevents the program from functioning correctly.
▪ Remains undetected until the program is compiled or run.
Type of Error
Syntax Errors
▪ Occurs when the code violates the
rules of the Java programming
language's syntax.
▪ Also known as compile-time error.
▪ Consequence: The program cannot be
successfully compiled and needs to be
fixed before it can be executed.
▪ Easy to identify because the Compiler
explicitly mentions the error when
compiling the code.
Logical Error
▪ Refers to a mistake in the program's
logic that leads to incorrect results.
▪ Can’t detect during runtime or compile
time.
▪ Hardest to identify.
Runtime Error
▪ Errors that occur while running the
program.
▪ Not caught during Compilation time.
▪ Quite common type of errors but easily
traceable.
▪ The Runtime Errors are actually
different Exceptions being caught.
N.B. You’ll learn more about Exceptions
in the later courses.
Java Programming – Mid Term Exam
Write a Java program to determine if a number is even or odd.
Increment Operator (++) Purpose: Increases a variable’s value by 1.
example:
int x = 5;
[Link](++x); // 6
[Link](x++); // 6
[Link](x); // Now x = 7
Decrement Operator (--) Purpose: Decreases a variable’s value by 1.
int y = 5;
[Link](--y); // 4
[Link](y--); // 4
[Link](y); // Now y = 3
Difference Between Syntax, Logical, and Runtime Errors
Error Type When It Occurs Detected By Description Example
Syntax During compilation Compiler (javac) Occurs when the code violates Java syntax Missing semicolon:
Error (before running) rules. int x = 5
Logical During or after Programmer (through The program runs, but the output is wrong due int avg = a + b / 2;
Error execution testing) to a mistake in logic. (wrong formula)
Runtime While the program JVM (Java Virtual Occurs due to invalid operations during int x = 10 / 0; →
Error is running Machine) program execution. ArithmeticException