Java Unit 1
Java Unit 1
Java Unit 1
```java
// Example of a lambda expression
(x, y) -> x + y
```
```java
// Example of a functional interface
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
```
```java
// Example using Streams to find the sum of
even numbers
List<Integer> numbers = Arrays.asList(1, 2, 3,
4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
```
- **Default Methods:** Default methods in
interfaces provide a way to add new methods to
existing interfaces without breaking
implementing classes.
```java
// Example of a default method in an interface
interface MyInterface {
default void defaultMethod() {
System.out.println("Default method");
}
}
```
```java
// Example of a method reference
List<String> names = Arrays.asList("Alice",
"Bob", "Charlie");
names.forEach(System.out::println);
```
```java
// Example using the Date and Time API
LocalDate today = LocalDate.now();
```
```java
// Example using Optional
Optional<String> name =
Optional.ofNullable(getName());
if (name.isPresent()) {
System.out.println("Name: " + name.get());
} else {
System.out.println("Name not available.");
}
```
**Language Basics**
```java
// Example of a Java program structure
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
- **Data Types:** Java has primitive (e.g., `int`,
`char`) and reference (e.g., `String`) data types.
```java
// Example of primitive and reference data
types
int age = 30;
String name = "Alice";
```
```java
// Example of identifiers and variables
int count = 10;
double price = 29.99;
```
**Writing Programs:**
```java
// Example of using command-line arguments
public static void main(String[] args) {
System.out.println("Arguments received: " +
args.length);
}
```
```java
// Example of operators and expressions
int x = 10;
int y = 5;
int sum = x + y;
boolean isGreater = x > y;
```
```java
// Example of type conversion rules
int integerNum = 10;
double doubleNum = 5.5;
double result = integerNum + doubleNum; //
Automatic type conversion
int intResult = (int) result; // Explicit type
casting
```
**Control Flow:**
- **If, if/else, and Ternary Operator:** Conditional
statements are used for decision-making based
on conditions.
```java
// Example of conditional statements and
ternary operator
int x = 10
;
int y = 5;
if (x > y) {
System.out.println("x is greater than y");
} else {
System.out.println("y is greater than or
equal to x");
}
// Ternary operator
int max = (x > y) ? x : y;
```
```java
// Example of the switch statement
int dayOfWeek = 2;
String day;
switch (dayOfWeek) {
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
default:
day = "Unknown";
}
```
```java
// Example of loop constructs
int count = 0;
// While loop
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
// For loop
for (int i = 0; i < 5; i++) {
System.out.println("i: " + i);
}
// For-each loop
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println("Number: " + number);
}
```
```java
// Example of declaring and using a method
public int add(int x, int y) {
return x + y;
}
```
**Class Libraries:**
```java
// Example of using standard classes
String text = "Hello, World!";
Integer integer = 42;
ArrayList<String> names = new ArrayList<>();
Date currentDate = new Date();
```