JAVA
PRESENTED BY:-
HIMANI
Packages
Java.util package contains the collections framework, legacy collection
classes, event model, date and time facilities, internationalization and
miscellaneous utility classes.
Every type primitive or reference is associated with one java
.lang class object.
Java.lang.class objects.
Java.lang.class object is created by JVM as class is loaded.
There will be only one java.lang.class object for one type.
Java.lang.class object is common to all instances of a class.
Enum types, interfaces will also have their own
java.lang.class object.
We can obtain java.lang.class object associated with a class
object associated with a class either using .class or getClass()
method..
Class Loaders , Class Path
String, StringBufferClass,
StringBuilderClass
String vs StringBuffer
Immutable Classes
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the
wrapper classes and String class is immutable.
Apart from your written classes, JDK itself has lots of immutable classes. Following are such a list of immutable
classes in Java.
java.lang.String
Wrapper classes such as Integer, Long, Double etc
java.math.BigInteger and java.math.BigDecimal
Java enums etc.
Advantages of immutable classes:-
Predictability
Validity
Thread-safety
Cacheability
Arrays
Arrays are used to store the multiple values in a single variable ,instead of declaring separate
variables for each value.
Loop through an Array
Loop through an array elements with the for loop and use the length property to specify how many times the loop
should
There isrun.
also a for-each loop , which is used to loop through elements in arrays.
Types of Arrays:-
❑ Single Dimensional Array:- Single dimensional arrays in java is a
collection of similar types of elements in contiguous memory
locations.
❑ Multi Dimensional Array:- Multi dimensional arrays are used to store
data as a tabular form, like a table with rows and columns.
Arrays
Single Dimensional Array Syntax:-
dataType [ ] arrayName;
Multi- Dimensional Array Syntax:-
Single-Dimen Multi-Dimens int [ ] [ ] my Numbers = new int { { } , { } } ;
sional Array ional Array
Java Methods
A method is a block of code which only runs when it is called., we can pass data known as parameters , into a
method.
Methods are used to perform certain actions and they are known as functions.
We use methods to define the code once and use it many times.
Create a Method:-
A Method can also be executed and can be called many times.
Information can be passed to methods as a parameters. Parameters act as variables inside the method.
Static
When parameter is passed to the method ,it is called an argument. Method
Method
We can have as many parameters as like. Public
Two methods;- Static and Public. Method
Modifiers- Access Modifiers & Non- Access
Modifiers
Method Overloading
Method overloading is a fundamental concept in object-oriented programming (OOP) that allows multiple
methods with the same name to be defined, as long as they have different parameter lists.
Method Overloading allows different methods to have the same name, but different signatures where the
signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Method overloading in Java is also known as compile-time polymorphism or static polymorphism.
Method Overloading allows different methods to have the same name, but different signatures where the
signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Method overloading in.
Overloading
Explaination
Method Overriding
Method overriding occurs when a sub class has the same method as the parent class.
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is
already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the
same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then the method
in the subclass is said to override the method in the super-class.
Method overriding in Java is also known as the Run-Time polymorphism.
Java Constructors
A Constructor in java is a method that is used to initialize objects.
The Constructor is called when a object of a class is created.
It can be used to set the initial values for object attributes.
The Constructor name must match the class name and it cannot
have a return type.
Constructor can also take parameters , which is used to initialize
attributes.
As many parameters used as we can have in constructors.
It calls a default constructor if there is no constructor available in the class.
In such case, Java compiler provides a default constructor by default.
All Classes have constructors by default.
Encapsulation
Binding code and data together into a single unit are known as encapsulation.
Example like a capsule , it is wrapped with the different medicines.
Encapsulation meaning that is to make sure that sensitive data
is hidden from the users.
To achieve this, declare class variables or attributes as
private Or.., provide public get and set methods to access
and update the value of private variables.
We use the getName( ) and setName( ) methods to access
and update the variable.
▪ Advantages
▪ Better control of class
attributes and methods.
▪ Increased security of data.
▪ More Flexible.
Thankyou