Java Unit1 Notes
Java Programming - Diploma K Scheme Unit 1: Basic Syntactical Constructs
1.1 Java Features and Programming
Environment
Java is platform-independent, object-oriented, robust, secure.
JDK (Java Development Kit) contains compiler and libraries.
JVM executes bytecode.
IDEs like Eclipse, NetBeans used.
1.2 Defining a Class, Creating Object,
Accessing Class Members
Class: Blueprint of objects, contains fields and methods.
Object: Instance of class.
Members: Fields and methods.
Example:
class Student {
int id;
String name;
}
Student s = new Student();
[Link] = 101;
[Link] = "Pooja";
1.3 Java Tokens, Data Types, Constants,
Operators, Decision Making, Loops
Tokens: Keywords, Identifiers, Literals, Operators.
Data Types: Primitive (int, float, char) & Reference (String, Array).
Constants: final keyword.
Operators: Arithmetic, Relational, Logical, Assignment.
Decision Making: if, if-else, else-if ladder.
Loops: for, while, do-while.
Example (Decision Making):
int marks = 65;
if (marks >= 75) [Link]("Distinction");
else if (marks >= 60) [Link]("First Class");
else [Link]("Fail");
1.4 Arrays, Strings, StringBuffer, Vectors,
Wrapper Classes
Arrays
1D and 2D arrays store elements of same type.
Example:
int[] arr = {10, 20, 30};
int[][] mat = {{1,2},{3,4}};
Strings
Immutable objects.
Methods: length(), charAt(), substring(), toUpperCase(), equals().
StringBuffer
Mutable strings, faster for modification.
Methods: append(), insert(), delete(), reverse().
Vectors
Dynamic array, grows automatically.
Methods: add(), remove(), get(), size().
Wrapper Classes
Primitive ↔ Object conversion.
Autoboxing & unboxing.
Classes: Integer, Float, Double, Character, Boolean.
1.5 Constructors and Methods
Constructors
Special methods to initialize objects.
Types: Default, Parameterized, Copy.
Rules: Name same as class, no return type, called automatically.
Example:
class Student {
int id;
String name;
Student() { id=1; name="Pooja"; }
Student(int i, String n) { id=i; name=n; }
}
Methods
Block of code performing task.
Types: static, non-static.
Method Overloading: same name, different parameters.
Nesting: one method calls another.
Example:
void show() { display(); }
int add(int a, int b) { return a+b; }
Command Line Arguments
Passed via main(String[] args).
Example: java Demo Pooja 25 → args[0]="Pooja", args[1]="25".
Garbage Collection
Automatic memory cleanup.
[Link]() requests garbage collection.
finalize() called before destruction.
Access Modifiers
Modifier Same Class Package Subclass Others
public ✔ ✔ ✔ ✔
private ✔ ✖ ✖ ✖
protected ✔ ✔ ✔ ✖
default ✔ ✔ ✖ ✖
20-Mark Theory Answers
Constructors: definition, types, example, rules.
Methods: definition, types, method overloading, examples.
Nesting methods, command line arguments, garbage collection, access modifiers with
table.
Viva Questions & Answers
Q1: What is a constructor? → Special method to initialize objects. Q2: Types of constructors? →
Default, Parameterized, Copy. Q3: What is method overloading? → Same method name,
different parameters. Q4: Difference between constructor and method. Q5: Nesting of methods?
→ One method calls another. Q6: Command line arguments? → Values passed to main() from
console. Q7: Garbage collection? → Automatic removal of unused objects. Q8: finalize()
method? → Called before object destruction. Q9: Access modifiers? → public, private,
protected, default. Q10: Default vs Protected? → Default: same package, Protected: package +
subclass.
Programs included:
Decision Making, Loops, Array Sum & Average, String & StringBuffer, Vector, Wrapper
classes, Constructor examples, Method overloading & nesting.