Skip to content

Commit 5eeb642

Browse files
Update README.md
1 parent 354ffbc commit 5eeb642

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
1-
# java
1+
# Java Language Basics
2+
3+
## Syntax
4+
Java syntax is similar to C and C++. It uses semicolons to end statements and curly braces `{}` to define blocks of code.
5+
6+
## Printing in Java
7+
Printing output in Java is typically done using `System.out.println()` or `System.out.print()` methods.
8+
9+
```java
10+
public class HelloWorld {
11+
public static void main(String[] args) {
12+
System.out.println("Hello, World!");
13+
}
14+
}
15+
```
16+
17+
## Variables in Java
18+
Java variables must be declared with a specific type. Common types include `int`, `double`, `boolean`, `String`, etc.
19+
20+
```java
21+
int age = 25;
22+
double price = 19.99;
23+
boolean isActive = true;
24+
String name = "John";
25+
```
26+
27+
## Loops
28+
Java supports `for`, `while`, and `do-while` loops.
29+
30+
```java
31+
// For loop
32+
for (int i = 1; i <= 5; i++) {
33+
System.out.println(i);
34+
}
35+
36+
// While loop
37+
int count = 1;
38+
while (count <= 5) {
39+
System.out.println(count);
40+
count++;
41+
}
42+
43+
// Do-while loop
44+
int num = 1;
45+
do {
46+
System.out.println(num);
47+
num++;
48+
} while (num <= 5);
49+
```
50+
51+
## Functions (Methods)
52+
Functions in Java are called methods. They are defined within classes and can be `static` (class-level) or instance methods
53+
54+
```java
55+
public class MyClass {
56+
// Static method
57+
public static void sayHello() {
58+
System.out.println("Hello, World!");
59+
}
60+
61+
// Instance method
62+
public void greet(String name) {
63+
System.out.println("Hello, " + name);
64+
}
65+
66+
public static void main(String[] args) {
67+
sayHello(); // Calling a static method
68+
MyClass obj = new MyClass();
69+
obj.greet("John"); // Calling an instance method
70+
}
71+
}
72+
```
73+
74+
## Returning Values
75+
Methods in Java can return values using the `return` keyword.
76+
77+
```java
78+
public class Calculator {
79+
public int add(int a, int b) {
80+
return a + b;
81+
}
82+
83+
public static void main(String[] args) {
84+
Calculator calc = new Calculator();
85+
int result = calc.add(5, 3);
86+
System.out.println("Sum: " + result);
87+
}
88+
}
89+
```
90+
91+
## Use of Lists (Arrays and ArrayLists)
92+
Java arrays are fixed-size collections of elements of the same type. ArrayLists are dynamically resizable lists.
93+
```java
94+
// Arrays
95+
int[] numbers = {1, 2, 3, 4, 5};
96+
System.out.println("First number: " + numbers[0]);
97+
98+
// ArrayLists
99+
import java.util.ArrayList;
100+
ArrayList<String> names = new ArrayList<>();
101+
names.add("Alice");
102+
names.add("Bob");
103+
System.out.println("First name: " + names.get(0));
104+
```
105+
106+
107+

0 commit comments

Comments
 (0)