☕ Java Fundamentals & Program Development
Class 1 Complete Notes | From Basics to Mini Project
Programming, Algorithms & Source Code
- Programming: Writing instructions that tell a computer how to perform a task.
- Algorithm: A step-by-step, language-independent procedure to solve a problem (like a recipe idea).
- Program: The implementation of an algorithm in a specific language (like the recipe written in English).
- Source Code: Human-readable text written by the programmer (saved as
.javafiles).
Compiler, JDK, JRE & JVM
| Component | Contains | Used For |
|---|---|---|
| JVM | Bytecode interpreter | Executing bytecode ("Write Once, Run Anywhere") |
| JRE | JVM + Libraries | Running Java programs |
| JDK | JRE + Compiler (javac) + Tools | Developing and compiling programs |
Flow: Main.java → (javac) → Main.class (bytecode) → (JVM) → Output
Syntax vs Semantics & Types of Errors
- Syntax: Grammar rules of the language (e.g., missing semicolon).
- Semantics: The actual meaning/logic of the code (e.g., wrong formula).
| Error Type | Detected When | Example |
|---|---|---|
| Syntax | Compile time | Missing ; or invalid variable name |
| Runtime | While running | Dividing by zero (ArithmeticException) |
| Logical | Checking output | Wrong formula (Compiles & runs, but gives wrong answer) |
GUI vs CLI
- GUI (Graphical User Interface): Interact using windows, icons, menus, and a mouse. (Easy for beginners).
- CLI (Command Line Interface): Interact by typing text commands into a terminal. (Faster for developers, used for compiling/running).
Text Editors vs IDEs
| Feature | Text Editor (Notepad) | IDE (IntelliJ, Eclipse) |
|---|---|---|
| Edit code | Yes | Yes |
| Compile & Run | Manually via CLI | Built-in button |
| Error detection | No | Yes, live underlining |
| Auto-complete | Limited | Yes, smart suggestions |
Popular Java IDEs
- IntelliJ IDEA: Popular, smart code completion, very beginner-friendly (Community Edition is free).
- Eclipse: Long-established, free, open-source, widely used in universities.
- VS Code: Lightweight editor that becomes a Java IDE after installing the Java Extension Pack.
Hello World Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Line-by-Line Breakdown
public class Main: All Java code must be inside a class. If public, file name must match (Main.java).public static void main(String[] args): The entry point.public(accessible anywhere),static(runs without creating an object),void(returns nothing),String[] args(command-line arguments).System.out.println(): Prints text to the console and moves to a new line. (Useprint()to stay on the same line).
; | Java is case-sensitive | Blocks use {} | Text uses ""
Variables & Naming Rules
A variable is a named memory location that stores a value. Java is statically typed (must declare type).
Syntax: dataType variableName = value;
Rules: Can contain letters, digits, _, $. Cannot start with a digit. Cannot be a keyword. Use camelCase by convention.
Primitive vs Reference Data Types
| Type | Stores | Size | Example |
|---|---|---|---|
int | Whole numbers | 4 bytes | int age = 25; |
double | Decimals (15 digits precision) | 8 bytes | double price = 99.75; |
float | Decimals (7 digits precision) | 4 bytes | float temp = 36.5f; (Needs f) |
char | Single character | 2 bytes | char grade = 'A'; (Single quotes) |
boolean | true or false | 1 bit | boolean pass = true; |
String | Text (Class, not primitive) | Varies | String name = "Ali"; (Double quotes) |
Constants (final keyword)
A constant's value cannot be changed after assignment. Use the final keyword. Convention: UPPERCASE_WITH_UNDERSCORES.
final double PI = 3.14159;
final int MAX_MARKS = 100;
// PI = 3.14; ERROR: cannot assign a value to final variable
Getting User Input (Scanner)
import java.util.Scanner; // 1. Import
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // 2. Create object
System.out.print("Enter your age: ");
int age = input.nextInt(); // 3. Read input
System.out.println("You are " + age + " years old.");
input.close();
}
}
nextInt(), the Enter key (newline) stays in the buffer. If you use nextLine() next, it will read an empty string. Fix: Add an extra input.nextLine(); to consume the leftover newline.
Arithmetic, Relational & Logical Operators
- Arithmetic:
+,-,*,/(Integer division drops decimals!),%(Modulus/Remainder). - Relational:
==(Equal),!=(Not equal),>,<,>=,<=. (Returnsboolean). - Logical:
&&(AND),||(OR),!(NOT). - Increment/Decrement:
x++(post-increment: use then increase),++x(pre-increment: increase then use).
Automatic Conversion (Widening)
Java automatically converts a smaller type to a larger type without data loss.
Order: byte → short → int → long → float → double
int a = 10;
double b = 3; // 3 becomes 3.0 automatically
double result = a / b; // 10.0 / 3.0 = 3.333...
Explicit Conversion (Narrowing / Casting)
Converting a larger type to a smaller type must be done manually using a cast. Data may be lost (decimals are truncated, not rounded).
double price = 99.99;
int rounded = (int) price; // 99 (decimal part is cut off)
// int x = 7.5; ERROR: possible lossy conversion
int y = (int) 7.5; // OK, y = 7
Casting to Fix Integer Division
If you divide two integers, Java performs integer division. Cast before dividing to get a decimal result.
int total = 17;
int count = 5;
double wrong = total / count; // 3.0 (Integer division happened first!)
double correct = (double) total / count; // 3.4 (Cast applied first)
Quick Exercise Formulas
- Area of Circle:
PI * radius * radius - Area of Rectangle:
length * width - Simple Interest:
(principal * rate * time) / 100 - Celsius to Fahrenheit:
(celsius * 9.0 / 5) + 32(Use9.0to avoid integer division!)
Student Marks Calculator
Accepts name and 3 subject marks, then calculates total, average, and pass/fail result.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = input.nextLine();
System.out.print("Enter marks for subject 1: ");
int m1 = input.nextInt();
System.out.print("Enter marks for subject 2: ");
int m2 = input.nextInt();
System.out.print("Enter marks for subject 3: ");
int m3 = input.nextInt();
int total = m1 + m2 + m3;
double average = total / 3.0; // 3.0 forces decimal division
// Ternary operator (short if/else)
String result = (average >= 40) ? "Pass" : "Fail";
System.out.println("\n------ Result Sheet------");
System.out.println("Name : " + name);
System.out.println("Total : " + total);
System.out.printf("Average: %.2f%n", average); // 2 decimal places
System.out.println("Result : " + result);
input.close();
}
}
📌 Quick Revision Summary
🔄 Flow: .java (Source) → javac → .class (Bytecode) → JVM → Output
📦 JDK ⊃ JRE ⊃ JVM
🐛 Errors: Syntax (Compile), Runtime (Crash), Logical (Wrong Output)
🚪 Entry Point:
public static void main(String[] args)🔢 Types: int, double, float, char, boolean, String
⌨️ Input:
Scanner (import java.util.Scanner;)➗ Division: int/int drops decimals. Cast to double or use .0!
🔄 Casting: Widening (Auto, Safe) | Narrowing (Manual, Data Loss)
📝 Practice Questions
- Q1: Differentiate between JDK, JRE and JVM.
- Q2: What is the difference between a compile-time error and a logical error? Give one example of each.
- Q3: Why is
maindeclaredstatic? - Q4: What is the output of
System.out.println(7 / 2);? How would you get3.5? - Q5: What is the value of
xandyafterint x = 5; int y = x++ + ++x;? - Q6: Write a program that reads a number of days and prints the equivalent number of weeks and remaining days (Hint: use
/and%).
❓ Frequently Asked Questions (FAQ)
Expert ICT, Computer Science, Coding & Digital Marketing Classes
Learn ICT, Computer Science, Programming, Web Development and Digital Marketing with practical, career-focused guidance.
💬 Join WhatsApp ▶ Watch Tutorials🚀 Learn Today. Build Tomorrow.
Welcome to our Sri Lanka Online ICT & Computer Classes community! We provide Online Tuition Classes, Home Visit Classes and Individual / Group Classes for students and professionals. Whether you are a school student, university student, beginner programmer or entrepreneur, we help you develop practical IT skills for education and career growth.
💡 Explore Our Training & Services
🎓 University Project GuidanceTechnical guidance for university assignments and final-year IT projects.
|
🤖 AI, Coding & SoftwareDevelop programming knowledge and practical software development skills.
|
🏫 School ICT ClassesInteractive ICT and Computer Science lessons for school students.
|
🌐 Web Design & DevelopmentBuild websites and database-driven applications for individuals and businesses.
|
📢 Digital MarketingGrow your brand online with social media and digital marketing support.
|
💻 Computer & IT SkillsBuild essential computer skills from absolute beginner to advanced level.
|
📚 Learn With Your Curriculum
Support for Sri Lankan Local Curriculum, Cambridge and Pearson Edexcel programs.
🇱🇰 Sri Lankan Local Curriculum
|
📘 Pearson Edexcel
|
📗 Cambridge Curriculum
|
💻 Programming & Software Development
🛠️ Programming Languages & Technologies
| ✅ Python Programming | ✅ C Programming | ✅ Java Programming |
| ✅ PHP Programming | ✅ JavaScript | ✅ HTML & CSS |
| ✅ jQuery & AJAX | ✅ ASP.NET | ✅ VB.NET |
| ✅ VB6 | ✅ MySQL | ✅ MS SQL Server |
| ✅ MS Access | ✅ Oracle Database | ✅ Database Design |
| ✅ SQL Queries | ✅ Database Programming | ✅ Software Development |
🌐 Web Design & Development
- ✅ Website Design
- ✅ Website Development
- ✅ WordPress Website Development
- ✅ HTML / CSS / JavaScript
- ✅ PHP & MySQL
- ✅ WordPress SEO
- ✅ Website Maintenance
- ✅ Web Applications
- ✅ Database-Driven Websites
🎓 University & Final Year IT Projects
🧑💻 Project Development
|
📄 Documentation & Practical
|
🧠 AI & Smart Applications
|
📊 Database, Cloud & IT Training
| ✅ Oracle Database | ✅ MySQL | ✅ Microsoft SQL Server |
| ✅ Microsoft Access | ✅ Database Design | ✅ SQL Queries |
| ✅ AWS Cloud | ✅ Linux | ✅ UNIX |
| ✅ Windows Server | ✅ DevOps | ✅ SRE |
| ✅ Automation | ✅ Software Testing | ✅ Freelancing Skills |
📱 Digital Marketing & Social Media
| ✅ Digital Marketing | ✅ Facebook Page Setup & Management |
| ✅ Instagram Marketing | ✅ TikTok Marketing |
| ✅ YouTube Channel Setup | ✅ Social Media Management |
| ✅ Content Creation | ✅ SEO |
| ✅ Facebook Ads | ✅ Instagram Reels |
| ✅ LinkedIn Branding | ✅ Social Media Campaign Planning |
| ✅ Hashtag Strategy | ✅ Insights & Engagement |
🖥️ Microsoft Office & Digital Skills
- ✅ MS Word
- ✅ MS Excel
- ✅ MS PowerPoint
- ✅ Excel Formulas
- ✅ Excel Automation & Macros
- ✅ Computer Fundamentals
- ✅ Digital Literacy
- ✅ Multimedia Presentations
⭐ Why Join Our Classes?
|
🌍
Online & Home Visit Classes
|
👥
Individual & Group Classes
|
🗣️
English / Sinhala / Tamil
|
📚
School & University Support
|
|
📝
Past Papers & Model Papers
|
💻
Practical Programming
|
🚀
Beginner to Advanced Skills
|
🎯
Career-Focused Learning
|
📞 Ready to Start Learning?
Book your online class or contact us for ICT training, programming courses, university project guidance and digital marketing services.
📱 Call / WhatsApp: +94 729622034
📧 Email: itclasssl@gmail.com
💻 Skype: ITClassSL
💬 Contact Us on WhatsApp🌐 Our Online Learning Platforms
Explore tutorials, notes, project guides and learning resources.
ITClassSL — ICT Training | Computer Classes | Programming | University Project Guidance | Digital Marketing
Learn • Practice • Build • Grow 🚀