Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Advanced Programming with Java-1

The document provides an overview of Java, an object-oriented programming language developed by Sun Microsystems in 1995, emphasizing its platform independence and core concepts such as classes, objects, and methods. It covers Java architecture, installation, data types, conditional statements, access modifiers, exception handling, and the Java Collections framework. Additionally, it explains the importance of packages, constructors, and naming conventions in Java programming.

Uploaded by

mp8315199
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)
4 views

Advanced Programming with Java-1

The document provides an overview of Java, an object-oriented programming language developed by Sun Microsystems in 1995, emphasizing its platform independence and core concepts such as classes, objects, and methods. It covers Java architecture, installation, data types, conditional statements, access modifiers, exception handling, and the Java Collections framework. Additionally, it explains the importance of packages, constructors, and naming conventions in Java programming.

Uploaded by

mp8315199
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/ 23

Advanced Programming with Java

Java:- Java is an object-oriented programming language developed by Sun Microsystems of USA


in 1995.

t’s designed to be platform-independent, meaning code written in Java can run on any device
that has the Java Virtual Machine (JVM). This idea is summed up in the Java slogan: "Write once,
run anywhere."

It was originally called Oak by James Goslin (One of the inventors of Java)

JAVA -: Purely Object-Objected

-Object Oriented Programming tries to map code instructions with real world making the code
short and easier to understand.

What is Object Oriented Programming?

-Solving a problem by creating objects is one of the most popular approaches in programming.
This is called Object Oriented Programming.

What is DRY?

-DRY stands for -Do not repeat yourself.

Java Architecture:

How Java Works?

Java is compiled into the bytecode and then it is interpreted to machine code.
Java Installation
Go to Google and type “Install JDK” -> Install JAVA JDK

-used for developing and running Java programs.

Go to Google and type “install IntelliJ Idea” -> Install JAVA IDE

JDK-> JAVA Development Kit = Collection of tools used for developing and running Java
programs.

JRE-> JAVA Runtime Environment =Helps in executing programs developed in JAVA.

Basic structure of a Java Program.


- Package Com.Company; → Group Classes
// Class Declaration
public class Main {

// Main method - entry point of any Java application


public static void main(String[] args) {
// Code to be executed
System.out.println("Hello, world!");
}
}

1. Function –: In Java programming, a function (more commonly called a method in Java) is


a block of code that performs a specific task. You define a method once, and then you can
call it (use it) whenever needed, which helps keep your code organized, reusable, and
easier to read.

2. Classes -: in Java programming, a class is a blueprint or template for creating objects. It


defines the properties (fields/attributes) and behaviors (methods/functions) that the
objects created from the class will have.
Class Declaration:
• Everything in Java must be inside a class.

• The filename should match the public class name (Main in this example).

main Method:
• public: accessible from anywhere

• static: no need to create an object to call it

• void: doesn't return anything

• String[] args: allows command-line arguments


Code Execution:
• System.out.println() is used to print output to the console.

Objects-: In Java programming, objects are the core building blocks of object-oriented
programming (OOP). An object is an instance of a class, and it represents a real-world entity
with state and behavior.
Class-: A class is a blueprint for creating objects.

Filled by students
MBBS form Form for student

Naming Conventions: -
For Classes, we use Pascal Convention. Example Main, MyScanner, MyEmployee

For Functions and variables: -


we use CamelCase Convention. Example myScannar, myMarks,

How to model a problem in OOPs.


Noun → Class → Employee
Adjective→Attribute →Name, age, salary
Verb→Method → getSalary (), increment ()
Constructors-:
A member function used to initialize an object while crating it.

1. Default Constructor
2. Parameterized Constructor
Packages-
In Java, packages are used to group related classes, interfaces, and sub-
packages. Think of them like folders on your computer that organize files to keep
things neat and manageable.

Types of Packages in Java

1. Built-in Packages (Standard Packages)


These are provided by Java itself and are part of the JDK.
Some common built-in packages:
o java.lang – Core classes like String, Math, Object, etc. (imported
automatically)
o java.util – Collections framework, date/time, etc.
o java.io – Input and output (file handling, streams)
o java.net – Networking (sockets, URLs)
o java.sql – Database access via JDBC
o javax.swing – GUI components
Why Use Packages?
• Avoid name conflicts (e.g., two classes with the same name in different
packages)
• Modularize code
• Access control (public, protected, package-private)
• Reusability
Data Types
-In Java programming, data types define the kind of data a variable can hold. Java
is a strongly-typed language, which means every variable must be declared with a
data type. Data types in Java fall into two main categories:
1.Primitive Data Types
2.Non-Primitive (Reference) Data Types
Conditional Statements-:

-A conditional statement in Java is a way to make decisions in your code. It


allows your program to choose different actions based on whether a
condition is true or false.
- In simple terms:
“If something is true, do this. Otherwise, do something else.”

Types of Conditional Statements in Java:


1.if -:
Executes a block of code if the condition is true.
if (age > 18) {
System.out.println("You are an adult.");
}
2. if-else statement
- Executes one block if the condition is true, another if it’s false.
if (age > 18) {
System.out.println("Adult");
}
else {
System.out.println("Not an adult");
}
if-else if-else ladder
-Checks multiple conditions in sequence.
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
4.Switch statement-:
Used to select one of many code blocks to be executed.
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}
Access Modifiers-:
-Access modifiers are keywords used to set the access level for
classes, variables, methods, and constructors. They control which parts of your code can access
or modify other parts, helping to enforce encapsulation — one of the core principles of object-
oriented programming.

-Specify where a property /method is accessible.

-Access modifiers determine whether other classes can use a particular field or invoke a method
can be public, private, protected or default (no modifier)

There are four types of access modifiers

1. public
2. default
3. protected
4. private

1. Public -:
-Accessible from everywhere – any other class, whether in the same package or different
package.
2. private-: Accessible only within the same class.
- Not visible to other classes or even subclasses.
-Commonly used to hide internal details (encapsulation).
3. protected
• Accessible within the same package and in subclasses (even in other packages).
• Great for inheritance when you want to limit but still allow access in child classes.

4.Default (No modifier)


- When you don't write any access modifier, it's called
package-private.
-Accessible only within the same package.
Example.
Exception Handling-
-Exception handling in Java is a mechanism to handle
runtime errors, so the normal flow of the application can be
maintained. Instead of the program crashing when an error
occurs, exception handling allows you to catch and handle
the error gracefully.

What is an Exception?
An exception is an unwanted or unexpected event that
disrupts the normal flow of the program.
Examples:
• Dividing a number by zero
• Accessing an array index that doesn’t exist
• Opening a file that doesn't exist
Exception Handling Keywords in Java
1. try – contains the code that might throw an exception
2. catch – handles the exception
3. finally – block that always executes (used to clean up)
4. throw – used to throw an exception
5. throws – declares exceptions in method signature
Java Collections-: Collections refer to a framework that provides architecture to
store, retrieve, and manipulate groups of objects (like lists, sets, and maps).

-A collection represents a group of objects. Java collections provide Classes and Interfaces
for us to able to write code quickly and efficiently.

Why do we need Collections?

-We need Collections for efficient storage and better manipulation of data in java.
-To store, retrieve, manipulate, and communicate aggregate data.
- Provides ready-made data structures like Lists, Sets, Queues, Maps, etc.
-Saves time and effort (instead of creating your own data structures from scratch).

For ex. We use arrays to store integers but what if we want to


-Resize this array?
-Insert an element in between?
-Delete an element in Array?
-Apply certain operations to change this array?

How are Collections available?

-Collections in Java are available as Classes and Interfaces. Following are few commonly used
collections in Java.

ArrayList→ For variable Size Collection


Set→ For distinct collection
Stack→A LIFO data Structure
HashMap→For storing key-values pairs.
Collection class is available in java.util package
Example.

You might also like