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

Java_Basics

Uploaded by

nasimsarker121
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)
13 views

Java_Basics

Uploaded by

nasimsarker121
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/ 4

Java Basics

1. Java Features:

- Platform Independence: Write once, run anywhere (WORA) using the Java Virtual Machine

(JVM).

- Object-Oriented: Based on objects and classes.

- Robust: Strong memory management and error-checking.

- Multithreading: Allows concurrent execution of code.

- Secure: Includes built-in security features.

2. Basic Syntax:

- Case Sensitivity: Java is case-sensitive.

- File Naming: The class name and file name should match.

- Entry Point: The `main` method is the starting point of execution.

Example:

public class HelloWorld {

public static void main(String[] args) {

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

3. Data Types:

Java supports primitive and non-primitive data types.

| Data Type | Size | Example |


|-------------|-----------|---------------|

| byte | 1 byte | byte b = 10; |

| short | 2 bytes | short s = 100;|

| int | 4 bytes | int i = 1000; |

| long | 8 bytes | long l = 100000L;|

| float | 4 bytes | float f = 10.5f;|

| double | 8 bytes | double d = 20.5;|

| char | 2 bytes | char c = 'A'; |

| boolean | 1 bit | boolean b = true;|

4. Control Statements:

- Conditional:

if (condition) {

// code

} else if (condition) {

// code

} else {

// code

- Loops:

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

System.out.println(i);

while (i < 5) {

System.out.println(i);

i++;
}

do {

System.out.println(i);

i++;

} while (i < 5);

5. Classes and Objects:

class Animal {

String name;

void eat() {

System.out.println(name + " is eating.");

public class Main {

public static void main(String[] args) {

Animal dog = new Animal();

dog.name = "Buddy";

dog.eat();

6. Methods:

public int add(int a, int b) {

return a + b;

}
- Static Method:

public static void display() {

System.out.println("Static Method");

7. Arrays:

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

for (int num : numbers) {

System.out.println(num);

8. Exception Handling:

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero.");

} finally {

System.out.println("This will always execute.");

You might also like