0% found this document useful (0 votes)
37 views3 pages

Introduction To Classes and Objects Java

Java is an object-oriented programming language centered around classes and objects, with a rich set of class libraries known as the Java API. Key concepts include memory management, encapsulation, constructors, and control statements, which are essential for effective Java programming. The document provides examples and explanations of these fundamental elements to aid in understanding Java development.

Uploaded by

sahiljadhav9284
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)
37 views3 pages

Introduction To Classes and Objects Java

Java is an object-oriented programming language centered around classes and objects, with a rich set of class libraries known as the Java API. Key concepts include memory management, encapsulation, constructors, and control statements, which are essential for effective Java programming. The document provides examples and explanations of these fundamental elements to aid in understanding Java development.

Uploaded by

sahiljadhav9284
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/ 3

# Introduction to Classes and Objects (Java)

## 1. Introduction
Java is an object-oriented programming (OOP) language that revolves around **classes** and
**objects**.
Classes define the structure and behavior (data and code) that objects of the class share.
Objects are instances of classes that contain actual values.

## 2. Java Class Libraries


Java comes with a vast collection of **class libraries** called the **Java API (Application
Programming Interface)**.
These pre-built classes provide essential functionality such as input/output, networking, data
structures,
GUI components, and more. For example, the `java.util` package contains classes like `ArrayList`,
`HashMap`, and `Collections`.

## 3. Typical Java Development Environment


A Java development environment includes:
- **Editor or IDE** (e.g., Eclipse, IntelliJ, NetBeans)
- **Compiler (javac)**: Converts Java source code into bytecode.
- **Java Virtual Machine (JVM)**: Executes the bytecode on any platform.
- **Debugger**: Helps locate logical and runtime errors.

## 4. Memory Concepts
Java divides memory into two major parts:
- **Stack**: Stores method calls and local variables.
- **Heap**: Stores dynamically created objects.
Every object reference in Java points to an object in the heap memory.

## 5. Arithmetic in Java
Java supports basic arithmetic operations (+, -, *, /, %) and follows standard operator precedence.
Example:
```java
int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a * b); // 50
```

## 6. Classes, Objects, Methods, and Instance Variables


- **Class**: Blueprint for creating objects.
- **Object**: Instance of a class.
- **Methods**: Define behaviors of the objects.
- **Instance Variables**: Represent the attributes of an object.

Example:
```java
class Student {
String name;
int age;
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ravi";
s1.age = 20;
s1.display();
}
}
```

## 7. Declaring a Class with a Method and Instantiating an Object


A **class** can contain fields and methods.
Objects are created using the `new` keyword.

Example:
```java
class Car {
void start() {
System.out.println("Car started");
}
}
class TestCar {
public static void main(String[] args) {
Car c1 = new Car();
c1.start();
}
}
```

## 8. set Methods and get Methods


**Encapsulation** allows controlling access to class variables using getters and setters.
```java
class Person {
private String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
```

## 9. Primitive Types vs Reference Types


- **Primitive types** (int, double, boolean, char) store actual values.
- **Reference types** store addresses of objects.

Example:
```java
int a = 5; // Primitive
Student s = new Student(); // Reference
```

## 10. Initializing Objects with Constructors


Constructors are special methods used to initialize objects.
```java
class Employee {
String name;
int id;
Employee(String n, int i) {
name = n;
id = i;
}
}
```

## 11. Floating Point Numbers


Java supports two floating point types:
- **float** (4 bytes)
- **double** (8 bytes)
```java
double price = 99.99;
float rate = 9.5f;
```

## 12. Control Statements


Java control statements manage the flow of execution:
- **if, if-else, switch**
- **for, while, do-while loops**
- **break, continue, return**

Example:
```java
for(int i=0; i<5; i++) {
System.out.println(i);
}
```

## 13. Arrays in Java


Arrays are collections of similar data types.
```java
int[] arr = {10, 20, 30, 40};
for(int num : arr) {
System.out.println(num);
}
```

## Summary
Classes and objects form the foundation of object-oriented programming in Java.
Understanding constructors, methods, encapsulation, and control structures
is crucial for developing efficient and organized Java programs.

You might also like