Java Programming_ a Comprehensive Guide + 10 Marks
Java Programming_ a Comprehensive Guide + 10 Marks
Guide
Chapter 1: Introduction to Java Programming
1.1 Review of Object-Oriented Concepts
● Encapsulation: Wrapping data (variables) and methods within a single unit, typically
a class.
● Inheritance: Allowing a new class to inherit properties and methods of an existing
class.
● Polymorphism: The ability to use a single interface or method in multiple forms.
● Abstraction: Hiding implementation details and exposing only the necessary
functionality.
Java is known for its unique features, often called Java buzzwords:
1. Platform Independence: Java programs can run on any platform with a Java Virtual
Machine (JVM), making it platform-independent.
2. Portability: Bytecode generated by the Java compiler can be executed on any
platform.
3. Threads: Java provides built-in support for multithreading, enabling concurrent
execution of code.
The Java Virtual Machine (JVM) is the cornerstone of Java's platform independence. It
converts bytecode into machine code for the host system. The key components of the JVM
include:
Example:
The System.out class is used for console output in Java. Common methods include:
Example:
System.out.println("Hello, Java!");
System.out.printf("Formatted output: %d", 100);
A simple Java program consists of basic elements such as a class, the main method, and a
few statements.
Example:
public class SimpleProgram {
public static void main(String[] args) {
System.out.println("This is a simple Java program.");
}
}
1. Primitive Data Types: Includes byte, short, int, long, float, double, char,
and boolean.
2. Reference Data Types: Includes arrays, classes, and interfaces.
2.2 Variables
Variables in Java are containers for storing data values. Types of variables:
● Local Variables: Declared inside a method and accessible only within that method.
● Instance Variables: Declared within a class but outside methods, and they are
non-static.
● Static Variables: Declared with the static keyword, shared among all instances of
the class.
Example:
Example:
import java.io.*;
public class ConsoleInput {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.println("Hello, " + name);
}
}
2.5 Operators
● Arithmetic Operators: +, -, *, /, %
● Relational Operators: ==, !=, >, <, >=, <=
● Logical Operators: &&, ||, !
● Assignment Operators: =, +=, -=, etc.
● Bitwise Operators: &, |, ^, ~, etc.
Example:
Static members belong to the class rather than to any specific instance. They are accessed
using the class name.
Example:
Example:
_________________________________________________________
10-Marks Questions
1. Java Program to Take Input, Perform Arithmetic Operations, and Display Results
● User Input: Using the Scanner class to take input from the user.
● Arithmetic Operations: Performing addition, subtraction, multiplication, and division.
● Formatted Output: Using System.out.printf() to display results in a structured
and readable format.
Code:
import java.util.Scanner;
Key Points:
1. Scanner Class: Used for input. The nextInt() method reads integers from the
user.
2. Arithmetic Operations:
○ a + b: Adds the numbers.
○ a - b: Subtracts the second number from the first.
○ a * b: Multiplies the numbers.
○ (double) a / b: Divides the numbers and converts the result to a
floating-point value.
3. Formatted Output: The System.out.printf() method allows formatting with
placeholders (%d for integers, %.2f for floating-point numbers).
Example Execution:
Enter two numbers: 10 3
Sum: 13
Difference: 7
Product: 30
Quotient: 3.33
Example:
class Employee {
private String name; // Private variable
○
2. Inheritance: Allowing a class to inherit properties and methods from another class.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
○
3. Polymorphism: Ability to take multiple forms (method overloading and overriding).
Example:
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
}
○
4. Abstraction: Hiding implementation details and showing only the essential features.
Example:
abstract class Vehicle {
abstract void start();
}
Code Example:
class Animal {
void sound() {
System.out.println("This is a general animal sound.");
}
}
Key Points:
Example Execution:
Dog barks.
This program shows the difference between String (immutable) and StringBuffer
(mutable). It also highlights methods for string manipulation and compares their
performance.
Code:
public class StringDemo {
public static void main(String[] args) {
// String Example (Immutable)
String str = "Hello";
str = str + " World"; // Creates a new object
System.out.println("String: " + str);
// StringBuffer Example (Mutable)
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World"); // Modifies the existing object
System.out.println("StringBuffer: " + sb);
// Comparing Performance
long startTime = System.nanoTime();
String test = "A";
for (int i = 0; i < 1000; i++) {
test += "B"; // Creates a new object each time
}
long endTime = System.nanoTime();
System.out.println("String Time: " + (endTime - startTime) + " ns");
startTime = System.nanoTime();
StringBuffer testBuffer = new StringBuffer("A");
for (int i = 0; i < 1000; i++) {
testBuffer.append("B"); // Modifies the same object
}
endTime = System.nanoTime();
System.out.println("StringBuffer Time: " + (endTime - startTime) + " ns");
}
}
Key Points:
Example Execution:
String: Hello World
StringBuffer: Hello World
String Time: 15000000 ns
StringBuffer Time: 500000 ns