0% found this document useful (0 votes)
3 views3 pages

Java Ultimate Study Notes

The document provides comprehensive Java study notes covering key concepts such as integer vs double division, floating point precision, boolean logic, conditional statements, loops, methods, constructors, arrays, and random number generation. It emphasizes best practices like using a tolerance for comparing doubles, the importance of operator precedence, and the behavior of break statements in loops. Additionally, it offers exam strategies to focus on logic and details to avoid common mistakes.

Uploaded by

junior7benzy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Ultimate Study Notes

The document provides comprehensive Java study notes covering key concepts such as integer vs double division, floating point precision, boolean logic, conditional statements, loops, methods, constructors, arrays, and random number generation. It emphasizes best practices like using a tolerance for comparing doubles, the importance of operator precedence, and the behavior of break statements in loops. Additionally, it offers exam strategies to focus on logic and details to avoid common mistakes.

Uploaded by

junior7benzy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Ultimate Study Notes (Deep Understanding

Version)

1. Integer vs Double (Core Foundation)

Java treats integers and decimals differently. When both numbers are integers, Java performs integer
division, which removes the decimal part. For example, 7 / 2 equals 3, not 3.5. This is because Java
truncates the decimal.

To get a decimal result, at least one value must be a double. For example, 7 / 2.0 results in 3.5. This is
called type promotion, where Java converts the integer to a double.

Exam tip: If you see division and expect decimals, check if both values are integers. That is a common
trick question.

2. Floating Point Precision

Decimal numbers such as 0.1, 0.2, and 0.3 cannot be stored exactly in binary. This leads to small
rounding errors. As a result, comparing doubles using == may return false even when values appear
equal.

Instead, always compare using a tolerance: [Link](a - b) < 0.0001. This checks if values are close
enough rather than exactly equal.

3. Boolean Logic and Evaluation

Boolean logic controls decision making. AND (&&) requires both conditions to be true. OR (||) requires
at least one condition to be true. NOT (!) reverses a condition.

Operator precedence is critical: && is evaluated before ||. Always use parentheses to make your logic
clear.

Short-circuiting means Java may skip evaluating part of an expression. In OR, if the first condition is
true, the second is skipped. In AND, if the first condition is false, the second is skipped. This prevents
errors such as division by zero.

4. Conditional Statements and Ranges


When checking ranges, always use &&. For example, (x >= 50 && x <= 100) ensures x is within the
range. Using || would incorrectly allow almost all values.

When using if/else if chains, order matters. The first true condition executes, and the rest are skipped.

5. Loops (While and For)

A while loop runs as long as its condition is true. You must update variables inside the loop to avoid
infinite loops.

A for loop combines initialization, condition, and update in one line. It is commonly used when the
number of iterations is known.

Example: for (int i = 0; i < 10; i += 3) produces 0, 3, 6, 9. The increment determines the step size.

6. Break Statement

The break statement immediately exits a loop. Even if the loop condition is still true, the loop stops. This
is useful when a certain condition is met early.

7. Methods and Parameters

Methods are reusable blocks of code. Parameters are variables in the method definition. Arguments
are values passed when calling the method.

A method must return a value if its return type is not void. Forgetting a return statement causes an
error.

8. Void Methods

Void methods do not return values. They are used for actions like printing or modifying variables. You
cannot assign the result of a void method to a variable.

9. Constructors and Objects

Constructors initialize objects and run automatically when an object is created. The keyword this()
allows one constructor to call another. It must always be the first line.

Static variables belong to the class, not individual objects. All objects share the same static variable.
10. Arrays (Very Important Topic)

Arrays store multiple values. When you create an array with new int[5], it has 5 elements indexed from
0 to 4.

Accessing an index outside this range causes an error. This is called an


ArrayIndexOutOfBoundsException.

When copying values, Java copies the value, not the reference for primitive types.

11. Finding Minimum in Arrays

To find the minimum value, initialize with the first element, then compare all others. Always use < for
minimum and > for maximum.

12. Switch Statements

Switch statements allow multiple conditions. Each case should have a break statement to prevent
fall-through. Without break, execution continues into the next case.

13. Random Numbers

nextInt(n) generates numbers from 0 to n-1. You can shift the range by adding or subtracting values.
For example, nextInt(9) - 4 produces values from -4 to 4.

14. Pass-by-Value

Java passes copies of variables to methods. Changes inside a method do not affect the original
variable. This is why swapping variables inside a method does not work.

15. Exam Strategy

Always trace code step by step. Focus on logic, not memorization. Pay attention to data types, loop
conditions, and array indices. Most mistakes happen from small details.

You might also like