0% found this document useful (0 votes)
6 views11 pages

1.Introduction to java

Uploaded by

Shweta Sahani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views11 pages

1.Introduction to java

Uploaded by

Shweta Sahani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

IntroductIon to java

Object-Oriented Programming (OOP)


Object-Oriented Programming (OOP) is a programming
paradigm based on the concept of "objects," which can
contain data (attributes) and code (methods). OOP focuses on
reusability, modularity, and organization. Its key concepts are:
• Class: A blueprint for objects, defining their attributes
and behaviors.
• Object: An instance of a class, representing real-world
entities.
• Encapsulation: Bundling of data and methods that
operate on the data into a single unit (class).
• Inheritance: Mechanism by which one class can inherit
fields and methods from another class, promoting code
reuse.
• Polymorphism: Ability of objects to take multiple forms,
usually through method overloading and overriding.
• Abstraction: Hiding implementation details and showing
only essential features.
Features of Java
Java is a high-level, object-oriented programming language
developed by Sun Microsystems (now owned by Oracle). It
has several features that make it powerful and widely used:
• Platform Independent: Java code is compiled into
bytecode, which runs on the Java Virtual Machine (JVM),
making it platform-independent.
• Object-Oriented: Java is purely object-oriented, meaning
everything in Java revolves around objects and classes.
• Simple: Java’s syntax is straightforward, making it easy to
learn and use.
• Secure: Java provides a secure environment with
bytecode verification, a security manager, and access
control features.
• Multithreaded: Java supports concurrent programming
with multithreading, allowing the execution of multiple
threads simultaneously.
• Robust: Java emphasizes strong memory management
and automatic garbage collection, reducing memory
leaks and crashes.
• Portable: Java programs can run on any system with a
JVM, making them portable across platforms.
• High Performance: Java’s performance is optimized
through Just-In-Time (JIT) compilers, making it relatively
fast for most applications.
How Java is Different from C++
Java and C++ are both object-oriented programming
languages, but they have several differences:
Feature Java C++
Manual memory
Memory Automatic garbage
management using
Management collection.
new and delete.
Does not support Supports multiple
Multiple
multiple inheritance inheritance using
Inheritance
directly. classes.
Feature Java C++
Platform- Platform-dependent
Platform
independent due to (compiled to native
Independence
the JVM. code).
Does not support Supports pointers
Pointers
pointers explicitly. explicitly.
Operator Not supported in
Supported in C++.
Overloading Java.
Compiled into
Compiled into
Compilation bytecode, which
machine code.
runs on the JVM.
Does not support Supports global
Global Variables
global variables. variables.
Data Types in Java
Java provides various data types, categorized into two types:
primitive and non-primitive.
• Primitive Data Types:
o byte: 8-bit integer.
o short: 16-bit integer.
o int: 32-bit integer.
o long: 64-bit integer.
o float: 32-bit floating-point number.
o double: 64-bit floating-point number.
o char: 16-bit Unicode character.
o boolean: Can store true or false.
• Non-Primitive Data Types:
o Classes, interfaces, arrays, and strings.

Control Statements
Control statements are used to control the flow of execution
in a program. Java provides the following control statements:
• Conditional Statements:
o if: Executes a block of code if a specified condition
is true.
o else: Executes a block of code if the condition is
false.
o else if: Used for multiple conditions.
o switch: Selects one of many code blocks to be
executed based on a variable's value.
• Looping Statements:
o for: Executes a block of code a certain number of
times.
o while: Executes a block of code as long as the
condition is true.
o do-while: Similar to while, but guarantees at least
one execution of the loop.
• Control Flow Statements:
o break: Terminates the loop or switch statement.
o continue: Skips the current iteration of the loop.
o return: Exits a method and optionally returns a
value.
Identifiers
In Java, identifiers are names used for classes, variables,
methods, and other elements in the program. Identifiers
must follow these rules:
• They must start with a letter, underscore (_), or dollar
sign ($).
• They can contain digits after the first character.
• They are case-sensitive (MyVar and myVar are different).
• Reserved keywords like int, class, and static cannot be
used as identifiers.
Arrays
An array in Java is a collection of variables of the same type
stored in contiguous memory locations. Arrays in Java are
objects and have the following characteristics:
• Arrays are zero-indexed.
• The size of the array is fixed and cannot be changed
once initialized.
• Arrays can be single-dimensional or multi-dimensional
(e.g., two-dimensional arrays).

Operators
Java provides various operators to perform operations on
variables and values. These are categorized into:
• Arithmetic Operators: +, -, *, /, % (modulus).
• Relational Operators: ==, !=, <, >, <=, >=.
• Logical Operators: &&, ||, !.
• Bitwise Operators: &, |, ^, ~, <<, >>, >>>.
• Assignment Operators: =, +=, -=, *=, /=, %=.
• Ternary Operator: ? : (Used as a shorthand for if-else).

Variables
A variable is a container for storing data values. In Java,
variables must be declared with a data type before use. The
three types of variables in Java are:
• Local Variables: Declared within a method and
accessible only within that method.
• Instance Variables: Declared inside a class but outside
any method and are specific to an instance of a class.
• Static Variables: Declared with the static keyword and
shared among all instances of a class.
Applications and Applets
Java supports two types of programs: applications and
applets.
• Java Applications: These are standalone programs that
run on the Java Virtual Machine. They can be console-
based or GUI-based using libraries like AWT and Swing.
• Java Applets: Applets are small Java programs that can
run inside a web browser. They require a browser with a
Java plugin or an applet viewer. Applets are now largely
obsolete due to security concerns and the rise of other
technologies like JavaScript.

You might also like