Java Programming Comprehensive Study Guide - Blue Crest College Department of Blue Crest College Sierra Leone
Introduction to Java Programming at Blue Crest College
Institutional Context: These notes originate from the Faculty IT Department at Blue Crest College, Sierra Leone, specifically from April 28, 2023.
Flavors of Java:
Core Java (J2SE): Focuses on software development. It is the foundation for all interview questions related to the language.
Advanced Java (J2EE): Used for developing Web Applications.
Android Java (J2ME): Designed for Mobile Application development.
Features and Rationale for Learning Java:
Simple and Easy: Designed to be learned and used with ease.
Open Source: The language is freely available for use and modification.
Platform Independent: Programs can run on various operating systems without modification.
Secure: Security is high due to the use of bytecode.
Embedded: Suitable for embedded systems.
Compiled and Interpreted: A Java program is first compiled into bytecode. This bytecode is then given to an interpreter, and the Java Virtual Machine (JVM) understands the bytecode to run the program successfully.
Robust: Features a garbage collector that automatically destroys unused objects in memory. It also boasts a large library and framework support.
/
/
Defining Java and Its History
Definition: Java is a class-based, high-level, object-oriented programming language.
Development and Ownership:
Developed by James Gosling and his associates in 1995.
The first version (JDK 1.0) was released on January 23, 1996, by Sun Microsystems.
Java was a product of Sun Microsystems until 2010, after which it became an Oracle-based product.
The version JDK 16 was released on March 16, 2021, by Oracle.
Etymology and Facts:
The language was initially named "Oak."
The name "Java" refers to a type of coffee in Indonesia.
There is no official full form for the name "Java."
Basic Syntax and Components
The Main Method Structure:
Keywords in the Main Method:
Public: Access modifier meaning anyone can access the class or method.
Static: Allows the execution of the class without creating an object.
Void: Indicates the method will not return any values.
String: The 'S' is capitalized because String is a class. In "String args[]", it refers to command line arguments being passed as parameters.
Array[]: Indicates an array variable.
Comments:
Single-line: Denoted by .
Multi-line: Denoted by .
Installation and Environment Setup
Prerequisites: Install the latest version of the Java JDK and the Eclipse IDE.
Download Steps:
Open a browser and search for "Java JDK download."
Navigate to the first link (www.oracle.com).
Select the version for Windows and download the .exe file.
Run the downloaded .exe file and follow the "next" prompts; the install takes approximately 10 to 15 seconds.
Setting Environment Variables:
Click the Windows button and search for "System Environment Variables."
Click "Environment Variables," select the "Path" variable, and click "Edit."
Navigate to "This PC" -> "C: Drive" -> "Program Files" -> "Java" -> "jdk 20."
Copy the file path up to the "bin" folder.
In the Environment Variables window, click "New" and paste the path.
Create a second variable by clicking "New." Name the variable "JAVA_HOME" (all caps) and for the value, paste the same path but excluding the "bin" folder.
Eclipse IDE Setup:
Search for "Eclipse IDE download" and download the x86-64 version.
Create a folder on the desktop named "java."
When launching Eclipse, select that folder as the workspace. All subsequent programs will save to the "java full course" folder on the desktop.
Compilation and Execution Workflow
Process Flow:
First.java: The source code file.
Javac: The Java compiler compiles the source code.
First.class: The resulting bytecode file.
JVM: The Java Virtual Machine executes the bytecode.
Output: The final result of the program.
Java Data Types
Definition: Data types specify the sizes and values stored in variables.
Primitive Data Types:
Boolean: For true/false values.
Character:
char.Numeric Integral:
byte,short,int,long.Numeric Floating-point:
float,double.
Non-Primitive Data Types:
Includes Classes, Interfaces, Strings, and Arrays.
Variables, Keywords, and Identifiers
Variables: Named memory locations stores different values.
Declaration Requirements: Must specify the
datatypeand thedata_name.Types of Variables:
Local Variables: Declared inside a method.
Instance Variables: Declared inside a class but outside methods.
Static Variables: Declared with the
statickeyword.Final Variables: Values that cannot be changed.
Example Code for Variables:
Keywords: Reserved words with predefined meanings in the compiler. They are case-sensitive (e.g.,
intis valid, butINTis not;aandAare different).Common Keywords:
abstract,assert,boolean,break,byte,case,catch,char,class,const,continue,default,do,double,else,enum,extends,final,finally,float,for,goto,if,implements,import,instanceof,int,interface,long,native,new,package,private,protected,public,return,short,static,strictfp,super,switch,synchronized,this,throw,throws,transient,try,void,volatile,while.
Identifiers: Names given to variables, methods, or classes (e.g.,
int a;,void m1;,class A;).
Input and Output Operations
Input: Handled via the
Scannerclass found injava.util.Scanner.Syntax:
Scanner Methods:
nextInt(): for integer values.nextLine(): for string values.nextDouble(): for double values.
Output: Handled via the
Systemclass found injava.lang.System.Syntax:
Sample Addition Program:
Control Flow: Conditional Statements
if statement: Used to test a single condition.
Syntax:
Program Example (Password Check):
if-else statement: Executes two different statements based on a single condition.
Syntax:
else-if statement: Used for multiple conditions involving one
ifblock, multipleelse-ifblocks, and a finalelseblock.Grading Example:
marks >= 60 && marks <= 100-> First Division.marks >= 45 && marks < 60-> Second Division.marks >= 33 && marks < 45-> Third Division.Otherwise -> Failed.
Nested if-else: Defining an
if-elseblock inside anotherif-elseblock.Example (Maximum of Three Numbers): Uses nested logic to compare
num1,num2, andnum3to find the largest value.
Control Flow: Looping Statements
Loop Definition: Used to repeat certain statements multiple times.
for loop:
Syntax:
Example: Printing a multiplication table by iterating from to \text{i<=10}.
while loop:
Syntax:
Example: Logic provided checks if a number is even () or odd within a while loop until a logic break occurs.
do-while loop:
Syntax:
Note: Executes the body at least once before checking the condition.
for-each loop:
Syntax:
Example: Iterating through an array .
Jump Statements and Switch
Jump Keywords:
Break: Exits the loop immediately.
Continue: Skips the current iteration and continues with the next.
Return: Exits from a method.
Switch Statement: A multiple-choice selection statement.
Syntax:
Operators, Methods, and Arrays
Operators:
Arithmetic: Addition, subtraction, etc.
Relational: Comparisons (e.g., greater than).
Logical: AND, OR, NOT.
Increment/Decrement: Pre-increment (), Post-increment (), Pre-decrement (), Post-decrement ().
Assignment: Used to assign values.
Ternary: Shorthand for if-else.
Methods: Blocks of code that take input, process it, and provide output.
Benefits: Code reusability; they only run when called.
Pre-defined Methods:
print(),sort(),nextInt(),sleep(),concat().User-defined Methods: Created by the programmer (e.g.,
add(),sum(),learn()).
Arrays: Objects containing similar data types in contiguous memory.
Index: Always starts at 0.
Syntax: or .
Types: 1D Array and 2D Array.
Strings: A pre-defined class that can be used as a data type. They represent a sequence of characters.
Immutability: Strings in Java are immutable, meaning they cannot be changed once created.
Overview of Object-Oriented Programming (OOPs)
Key Concepts:
Class: A virtual blueprint or group of elements with common properties and behavior.
Object: A real-world entity based on a class.
Access Specifiers: Control visibility.
Constructor: Special method to initialize objects.
Keywords:
superandthis.Blocks: Instance blocks and Static blocks.
Core Pillars: Encapsulation, Abstraction, Inheritance, Polymorphism.
Advanced Topics: Exception Handling, File Handling, Multithreading, Packages, Collections.