0% found this document useful (0 votes)
8 views

Comprehensive Java Learning Notes

Uploaded by

newemail9033
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)
8 views

Comprehensive Java Learning Notes

Uploaded by

newemail9033
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
You are on page 1/ 6

Comprehensive Java Learning Notes

1. Basics of Programming

- Syntax: Java programs must follow a specific syntax structure. Each program starts with a class definition

and a main method. The main method is the entry point where execution begins.

Example:

```java

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!"); // Outputs text to the console

```

- Variables: A variable stores data that can be used and modified. Variables have a data type, a name, and a value.

Example:

```java

int age = 25; // 'age' is a variable of type int with value 25

String name = "John"; // 'name' is a variable of type String with value "John"

```

- Data Types: Java has primitive data types (int, float, char, boolean) and reference types like String and arrays.

- Comments: Comments help describe code. Single-line comments use `//`, and multi-line comments use `/* ... */`.

2. Input and Output

- Taking Input: Java uses the Scanner class to accept user input. Scanner is part of the java.util package and needs to

be imported.

Example:

```java
import java.util.Scanner; // Importing Scanner class

Scanner scanner = new Scanner(System.in); // Creating Scanner object

System.out.print("Enter your name: ");

String name = scanner.nextLine(); // Reads a line of input

System.out.println("Hello, " + name);

```

- Displaying Output: Use `System.out.println()` to print information to the console.

3. Operators

- Arithmetic Operators: These perform mathematical operations like addition, subtraction, etc.

Example:

```java

int a = 5, b = 3;

int sum = a + b; // sum is 8

int product = a * b; // product is 15

```

- Relational Operators: Used to compare two values and return true or false.

Example:

```java

int x = 10, y = 20;

boolean result = x < y; // result is true

```

- Logical Operators: Used for combining conditions. `&&` (AND), `||` (OR), `!` (NOT).

Example:

```java

boolean isAdult = true;

boolean hasPermission = false;

boolean canEnter = isAdult && hasPermission; // false because both conditions aren't true
```

- Assignment Operators: Assign values to variables. `=`, `+=`, `-=`, etc.

Example:

```java

int count = 10;

count += 5; // count is now 15

```

4. Conditional Statements

- If Statements: Executes code based on conditions.

Example:

```java

int age = 18;

if (age >= 18) {

System.out.println("You are an adult.");

} else {

System.out.println("You are a minor.");

```

Explanation: The `if` statement checks if `age` is 18 or above. If true, it prints "You are an adult";

otherwise, it prints "You are a minor".

5. Looping Structures

- For Loop: Repeats code a set number of times, usually when the number of iterations is known.

Example:

```java

for (int i = 0; i < 5; i++) {


System.out.println("Hello, World!");

```

Explanation: This loop prints "Hello, World!" five times. The loop variable `i` starts from 0, checks `i < 5`,

and increments `i` by 1 each time.

- While Loop: Repeats code based on a condition, useful when the number of iterations is not known.

Example:

```java

int i = 0;

while (i < 5) {

System.out.println("Hello, World!");

i++;

```

Explanation: This `while` loop works similarly but only continues while `i` is less than 5.

6. Functions and Methods

- Defining Functions: Functions (or methods) allow code reuse. Java functions are defined inside classes.

Example:

```java

public static void greet() {

System.out.println("Hello, World!");

```

Explanation: The `greet` method is created using `public static void`, meaning it has no return type (`void`).

- Calling Functions: Call the method by its name.


Example:

```java

greet();

```

- Return Values: Functions can return values of a specified data type.

Example:

```java

public static int add(int a, int b) {

return a + b;

```

Explanation: `add` takes two integers and returns their sum.

7. Data Structures

- Arrays: Used to store multiple values of the same type.

Example:

```java

int[] numbers = {1, 2, 3, 4, 5};

System.out.println(numbers[0]); // Accesses the first element

```

Explanation: `numbers` is an array holding integers. `numbers[0]` accesses the first element.

- ArrayList: Part of java.util package; allows dynamic resizing.

Example:

```java

import java.util.ArrayList;

ArrayList<String> list = new ArrayList<String>();


list.add("Hello");

System.out.println(list.get(0)); // Outputs "Hello"

```

Explanation: ArrayLists can dynamically resize, unlike arrays.

You might also like