Java Unit1
Java Unit1
Java Unit1
What is Java?
Java Example
Simple.java
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
Application
What is JVM
It is:
What it does
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
Features of Java
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Interpreted
8. High Performance
9. Multithreaded
10. Distributed
11. Dynamic
Simple
Object-oriented
Secured
o No explicit pointer
o Java Programs run inside a virtual machine
sandbox
o Bytecode Verifier
o Security Manager
Architecture-neutral
High-performance
Distributed
Dynamic
SR Java C++
1. Platform-independent, Platform
SR Java C++
dependent, should
Java bytecode works
be compiled for
on any operating
different
system.
platforms.
C++ is platform-
It can run on any OS
2. dependent. Hence
hence it is portable.
it is not portable.
Memory
Memory Management
4. Management in
is System Controlled.
C++ is Manual.
party threading
libraries.
C++ is both a
Java is only an object- procedural and an
7. oriented programming object-oriented
language. programming
language.
C++ supports
10. Java doesn’t support
Structures and
Structures and Unions.
Unions.
namespace scope.
C++ both
12. Java supports only call supports call by
by value. value and call by
reference.
Java is not so
13. C++ is nearer to
interactive with
hardware.
hardware.
Mozilla Firefox,
Wikipedia, LinkedIn, Amazon, Apple
14.
Android OS, Uber, and OS, Spotify,
Minecraft, Adobe Photoshop,
and Youtube.
Advantages:
1. Simple
Java is a simple programming language since it is
easy to learn and easy to understand. Its syntax is
based on C++, and it uses automatic garbage
collection; therefore, we don't need to remove
the unreferenced objects from memory. Java has
also removed the features like explicit pointers,
operator overloading, etc., making it easy to read
and write.
2. Object-Oriented
3. Secured
5. Platform independent
6. Multi-Threaded
1. Performance
2. Memory consumption
3. Cost
5. Garbage collection
Java Applet
Advantage of Applet
Drawback of Applet
o Plugin is required at client browser to
execute applet.
Hierarchy of Applet
1. Data Types:
- Primitive Data Types:
- `byte`: 8-bit signed integer, range from -128
to 127.
- `short`: 16-bit signed integer, range from -
32,768 to 32,767.
- `int`: 32-bit signed integer, range from -2^31
to 2^31-1.
- `long`: 64-bit signed integer, range from -
2^63 to 2^63-1.
- `float`: 32-bit floating-point number, used for
decimal numbers.
- `double`: 64-bit floating-point number,
higher precision than float.
- `char`: 16-bit Unicode character, representing
a single character.
- `boolean`: Represents true or false values.
- Examples:
```java
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isStudent = true;
```
- Variables:
- Must be declared before use.
- Example: `int count = 10;`
- Variables can be assigned values: `count =
20;` or initialized in one line: `int count = 20;`.
- Examples:
```java
// Identifiers
int studentAge;
double averageScore;
// Variables
int count = 10;
String name = "John Doe";
```
- Expressions:
- Complex expressions can be simplified and
made more readable using parentheses.
- Example: `result = (num1 + num2) * (num3 -
num4);`
- Examples:
```java
int num1 = 10, num2 = 5, num3 = 8, num4 =
2;
int result = (num1 + num2) * (num3 - num4);
```
4. Type Conversion:
- Implicit Conversion (Automatic):
- Occurs when converting smaller data types
to larger ones.
5. Control Flow:
- Conditional Statements:
- `if` statement: Executes a block of code if a
condition is true.
- `else if` statement: Additional conditions to
check.
- `else` statement: Executes if none of the
previous conditions are true.
- `switch` statement: Selects one of many code
blocks to execute.
- Looping Statements:
- `for` loop: Executes a block of code a
specified number of times.
- `while` loop: Repeats a block of code while a
condition is true.
- `do-while` loop: Similar to `while` but
ensures the code block is executed at least once.
- Branching Statements:
- `break`: Terminates the loop or switch
statement.
- `continue`: Skips the rest of the loop and
moves to the next iteration.
- `return`: Exits a method and can return a
value.
- Examples:
```java
// Conditional Statements
int score = 85;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 70) {
System.out.println("Good job!");
} else {
System.out.println("Keep practicing.");
}
// Looping Statements
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + i);
}
```
6. Arrays:
- Declaration and Initialization:
- Arrays can store multiple values of the same
data type.
- Accessing Elements:
- Elements are accessed using indices, starting
from 0.
- Example: `int value = numbers[2];`
- Array Length:
- The `length` property returns the number of
elements in an array.
- Examples:
```java
// Declaration and Initialization
int[] numbers = new int[5];
// Accessing Elements
numbers[2] = 42;
int value = numbers[2];
```
7. Constants:
- Constants are declared using the `final`
keyword.
- Examples:
```java
final double PI = 3.14159;
final int MAX_VALUE = 100;
```
8. Methods:
- Method Signature:
- Includes the access modifier, return type,
method name, and parameters.
- Method Body:
- Contains the implementation of the method.
- Calling a Method:
- Methods are called using the method name
and passing arguments if needed.
- Examples:
```java
// Method Declaration
public int add(int a, int b) {
return a + b;
}
// Calling a Method
int sum = add(5, 7);
```
- java.io:
- Handles input and output operations.
- Example: `FileInputStream`,
`FileOutputStream` for file operations.
- java.math:
- Supports large integer and decimal
arithmetic.
- Example: `BigInteger`, `BigDecimal`.
- java.text:
- Deals with formatting and parsing text.
- Example: `SimpleDateFormat` for date
formatting.
- java.util:
- Offers utilities and data structures.
- Example: `ArrayList` for dynamic arrays,
`HashMap` for key-value pairs.
- Examples:
```java
// java.lang
String name = "John";
Object obj = new Object();
// java.io
// (Example: Reading from a file using
FileInputStream)
// java.math
BigInteger bigInt = new
BigInteger("12345678901234567890");
// java.text
SimpleDateFormat dateFormat = new
SimpleDateFormat("dd/MM/yyyy");
// java.util
ArrayList<String>