0% found this document useful (0 votes)
5 views5 pages

Java Unit 1

The document provides an overview of Java, focusing on its bytecode compilation, core features such as platform independence and object-oriented principles, and data types including primitive and non-primitive types. It also covers variables, arrays, operators, control statements, and the fundamentals of object-oriented programming including classes, objects, inheritance, encapsulation, polymorphism, and abstraction. Additionally, it explains the use of static fields and methods, and the process of object construction through constructors.

Uploaded by

praneet10910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views5 pages

Java Unit 1

The document provides an overview of Java, focusing on its bytecode compilation, core features such as platform independence and object-oriented principles, and data types including primitive and non-primitive types. It also covers variables, arrays, operators, control statements, and the fundamentals of object-oriented programming including classes, objects, inheritance, encapsulation, polymorphism, and abstraction. Additionally, it explains the use of static fields and methods, and the process of object construction through constructors.

Uploaded by

praneet10910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

UNIT-I

Bytecode

 Description: In Java, source code is compiled into bytecode instead of directly into
machine code. Bytecode is a platform-independent, intermediate code that can be
executed by the Java Virtual Machine (JVM). This design is fundamental to Java's
"write once, run anywhere" principle, as the same bytecode can run on any system
with a compatible JVM, regardless of underlying hardware or OS.
 Process:
o Java source code (.java file) is compiled by the Java compiler into a .class
file containing bytecode.
o The JVM interprets or compiles the bytecode into machine-specific code at
runtime, allowing it to run on any platform.

Example:

java
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

o When compiled, HelloWorld.class is generated, containing bytecode that


can be executed on any JVM.

2. Features of Java

Java’s core features make it highly portable, secure, and efficient:

 Platform-Independent: Thanks to bytecode and the JVM, Java applications can run
on any operating system with a JVM.
 Object-Oriented: Java is based on classes and objects, making it modular, flexible,
and easier to maintain.
 Secure: Java provides built-in security features like bytecode verification,
sandboxing, and a security manager.
 Robust: Java handles memory management automatically and has strong exception-
handling capabilities.
 Multithreaded: Java has built-in support for concurrent execution using multiple
threads.
 Portable: Java programs can run on various platforms without modification,
enhancing their portability.

3. Data Types

Java has two main categories of data types:

 Primitive Data Types:


o byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes) – integer types.
o float (4 bytes), double (8 bytes) – floating-point types.
o char (2 bytes) – single 16-bit Unicode character.
o boolean (1 bit) – holds true or false values.
 Non-Primitive Data Types: Includes arrays, classes, and interfaces.

Example:

java
Copy code
int age = 25;
double salary = 1000.50;
char grade = 'A';
boolean isActive = true;

4. Variables and Arrays

 Variables: Named storage locations for values, which are declared with a specific
data type.
o Types of Variables:
 Local Variables: Defined within methods and accessible only within
those methods.
 Instance Variables: Defined within a class but outside any method;
each instance of the class has its own copy.
 Static Variables: Defined with the static keyword and shared across
all instances of a class.
 Arrays: Used to store multiple values of the same type in a single variable. Arrays are
fixed in size and can be one-dimensional or multidimensional.

Example:

java
Copy code
// Variable Declaration
int count = 10;

// Array Declaration and Initialization


int[] numbers = {1, 2, 3, 4, 5}; // One-dimensional array
String[][] matrix = {{"A", "B"}, {"C", "D"}}; // Two-dimensional
array

// Accessing Array Elements


System.out.println(numbers[0]); // Output: 1
System.out.println(matrix[1][1]); // Output: D

5. Operators

Java operators are used to perform operations on variables and values. They include:

 Arithmetic Operators: +, -, *, /, % for mathematical calculations.


 Relational Operators: ==, !=, >, <, >=, <= for comparisons.
 Logical Operators: && (AND), || (OR), ! (NOT) for boolean logic.
 Assignment Operators: =, +=, -=, *=, /= for assigning values.
Example:

java
Copy code
int a = 10, b = 5;
int sum = a + b; // sum = 15
boolean isEqual = (a == b); // false
boolean result = (a > b) && (b > 0); // true

6. Control Statements

Control statements manage the flow of execution in a Java program.

 If-Else Statement: Executes a block of code if a condition is true; otherwise,


executes another block.

java
Copy code
int num = 10;
if (num > 5) {
System.out.println("Number is greater than 5");
} else {
System.out.println("Number is 5 or less");
}

 Switch Statement: Allows multiple possible execution paths based on the value of an
expression.

java
Copy code
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day"); break;
}

 Loops:
o For Loop: Executes a block of code a specific number of times.

java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}

o While Loop: Executes as long as a condition remains true.

java
Copy code
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Objects & Classes

1. Object-Oriented Programming (OOP)

OOP in Java organizes software design around data and operations on that data, using key
principles:

 Class: A blueprint for creating objects. It defines the structure and behaviors
(methods) of objects.
 Object: An instance of a class, containing specific data.
 Inheritance: Allows a class to inherit fields and methods from another class.
 Encapsulation: Restricts direct access to data, often through private access
modifiers and accessor methods.
 Polymorphism: Enables methods to perform different tasks based on object context.
 Abstraction: Focuses on essential features, hiding details not relevant to the user.

2. Defining Classes

Classes define the structure and behavior of an object, and can include fields (data), methods
(functions), constructors, etc.

csharp
Copy code
**Example**:
```java
public class Car {
// Fields
String model;
int year;

// Method
void startEngine() {
System.out.println("Engine started.");
}
}
```

3. Static Fields and Methods

 Static Field: Belongs to the class itself and is shared across all instances. It’s accessed
using the class name.
 Static Method: A method that doesn’t need an instance to be called and can only
access static fields and methods.

Example:

java
Copy code
public class MathUtil {
// Static Method
static int square(int num) {
return num * num;
}
}
int result = MathUtil.square(5); // Accessing static method without
instance

4. Object Construction

Objects are created using constructors, which initialize an object’s state. A constructor has
the same name as the class and no return type. If no constructor is defined, Java provides a
default constructor.

arduino
Copy code
**Example**:
```java
public class Person {
// Fields
String name;
int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}

// Creating an Object
Person person = new Person("Alice", 30); // Object created using the
constructor
```

By organizing classes and objects this way, Java supports reusability, modularity, and clear
structure in programming, following OOP principles.

You might also like