Knowledge Base
Knowledge Base
Key Concepts:
System.out.println("Hello, World!");
How It Works:
1. Save in Hello.java.
3. java Hello runs the bytecode on the JVM, printing "Hello, World!".
Basic Syntax:
Every Java program has at least one class, and to run it, that class
must have a main method. Each statement ends with ;. Curly braces
define blocks of code (classes, methods, loops, etc.).
Escape Sequences:
\n for newline, \t for tab, etc. If you do:
This prints:
mathematica
Line 1
Line 2 Indented
Debugging Techniques: If you see a compile error like ; expected,
you forgot a semicolon. If you see cannot find symbol, maybe you
spelled a variable name incorrectly.
Example Explanation:
If we write:
It prints "Hello World" (with a tab space) on one line, then "New line here" on
the next line. The \t adds a horizontal tab space, \n moves to the next line.
Arithmetic Operators:
+ addition, - subtraction, * multiplication, / division, % remainder.
Integer division truncates decimals. For example, 5/2 is 2, not 2.5. To
get a decimal, cast one operand to double: (double)5/2 = 2.5.
String Operations:
Example Explanation:
Consider:
int x=5,y=2;
System.out.println(x/y); // prints 2
Type Casting:
int n=7;
Scanner and I/O (Chapter 3): Use Scanner to read user input:
Q: How do you ensure a decimal result if dividing two ints? A: Cast one
to double, e.g. (double)5/2.
Example Explanation:
Suppose:
int count=sc.nextInt();
String sentence=sc.nextLine();
Here, count is read as an int. nextLine() afterwards ensures the next full line
read is cleanly captured, not just the leftover newline.
This checks score ranges in order. The first true condition executes.
switch:
switch(day) {
Loops (Ch.6):
int i=0;
while(i<5){
System.out.println(i);
i++;
for(int j=0;j<5;j++){
System.out.println(j);
int input;
do {
input=sc.nextInt();
} while(input<=0);
String Methods:
int num;
do {
num=sc.nextInt();
} while(num<=0);
for(int k=10;k>0;k--){
Example Explanation:
int num;
do {
num=sc.nextInt();
} while(num<=0);
If user enters -5 first, loop repeats. Once a positive number is given, the loop
exits.
Arrays (Ch.7)
Key Concepts:
Array Creation:
Traversing Arrays:
double avg=(double)sum/arr.length;
int max=arr[0];
Example Explanation:
int count=0;
if(val>50) count++;
This loop checks each element and increments count if it’s greater than 50.
Reading JavaDocs:
The Java standard library documentation shows you what methods are
available, their parameters, and return types.
Writing Javadoc:
/**
* @param r radius
* @return area as double
*/
Example Explanation:
Using Arrays.toString():
int[] arr={3,1,4};
System.out.println(Arrays.toString(arr));
// Outputs: [3, 1, 4]
Example:
return a+b;
Recursion (Ch.8):
Factorial example:
if(n<=1)return 1;
return n*factorial(n-1);
}
Example Explanation:
Class Design:
A class groups variables (fields) and methods that logically belong
together.
Constructors:
this.name=name;
this.health=100;
if(h>=0&&h<=100)this.health=h;
Example Explanation:
Method Overloading:
Example Explanation:
If you call print(5), it calls the int version. If you call print("Hi"), it calls the
string version.
String is Immutable:
String s="cat";
Big O Notation: Just know O(n) means linear growth, O(n^2) means
quadratic growth.
Example Explanation:
If you have:
String s="Hello";
String upper=s.toUpperCase();
Inheritance (Ch.11)
Key Concepts:
Inheritance Basics:
this.grade=grade;
@Override
}
}
Polymorphism:
Person p=new Student("Bob",90);
Example Explanation:
Overriding equals:
@Override
Student other=(Student)o;
This ensures two Students are equal if they have same name and grade.
Autoboxing/Unboxing:
Integer obj=10; // autobox int to Integer
Example Explanation:
int value;
do {
value=sc.nextInt();
} while(value<=0);
Q: Overriding vs Overloading:
Q: Abstract classes?
5. Use a class Rational to represent the fraction of how many salaries are
above average compared to total.
Step-by-Step:
int N;
do {
N=sc.nextInt();
} while(N<=0);
Read array:
for(int i=0;i<N;i++){
Calculate average:
float avg=calculateAverage(salaries);
System.out.println("Average: "+avg);
Trace Execution:
For code snippets, run through them line by line. Keep track of variable
values.