Java
Java
TypeCasting in Java
TypeCasting in Java is the process of converting one primitive
data type into another. TypeCasting can be done automatically
and explicitly.
There are two types of TypeCasting in Java.
Widening or Automatic Type Conversion.
Narrowing or Explicit Type Conversion.
1. Widening or Automatic Type Conversion:
When we assign a value of a smaller data type to a large data
type, this process is known as Widening Type Casting. It is also
known as Automatic Type Conversion because the Java
compiler will perform the conversion automatically. This can
happen only when the two data types are compatible.
byte -> short -> int -> long -> float -> double ( Widening or
Automatic Type Conversion)
For example, In Java, int data types are compatible, but it isn't
compatible with char and boolean data types. Also, char and
boolean data types are not compatible with each other.
Example:
public class WideningConversation {
public static void main(String args[]) {
// Automatic Type Conversion.
int i = 2147483647; // Int max value in java.
long l = i; // Automatically converted to long, now we can
extend l's value.
l = l + 1;
double d = l; // Automatically converted to double.
System.out.println("Int value : " + i);
System.out.println("Long value : " + l);
System.out.println("Double value : " + d);
}
}
Output
Int value : 2147483647
Long value : 2147483648
Double value : 2.147483648E9
2. Narrowing or Explicit Type Conversion:
When we assign a value of a large data type to a small data
type, the process is known as Narrowing Type Casting. This
can’t be done automatically. We need to convert the type
explicitly. If we don’t perform casting, the java compiler will give
a compile-time error.
double -> float -> long -> int -> short -> byte ( Narrowing or
Explicit Type Conversion)
Example:
public class ExplicitConversation {
public static void main(String args[]) {
Predefined Method
Predefined methods that are already defined in the libraries.
It's also known as the built-in method or the standard library
method.
Some predefined methods in java are:
sqrt(), max(), min(), round(), etc.
These are defined inside the Math class.
Some predefined methods of String class are:
length(), toUpperCase(), toLowerCase(), equals(), etc.
Let’s look at some examples using predefined methods.
Example 1: Find the maximum of two numbers using the built-in
method.
public class MaxOfTwoNumbers {
public static void main(String args[]) {
// Maximum of two numbers using Math.max()
int maximum = Math.max(100, 30);
System.out.println(maximum);
}
}
Output:
100
Example 2: Find the minimum of two numbers using the built-in
method.
public class MinOfTwoNumbers {
public static void main(String args[]) {
// Minimum of two numbers using Math.min()
int minimum = Math.min(100, 30);
System.out.println(minimum);
}
}
Output:
30
Example 3: Find the square root of a number using the built-in
method.
public class SqrtOfNumber {
public static void main(String args[]) {
User-defined Method
The method written by the user is called the user-defined
method. We can modify these methods based on our
requirements.
No argument(s) passed and no return value
When a function has no arguments, it doesn’t receive any data
from the calling method. Similarly, when it doesn’t return a
value, the calling method doesn’t receive any data from the
called method.
Syntax:
Function declaration : void function();
Function call : function();
Function definition : void function()
{
Statements;
}
Example: In this example, we have created a method
checkEvenOdd() and we don’t pass any parameters to it. We
simply write the code to check whether a number is even or
odd as the body of the function. If the number is even, we
simply print “Even Number”, else we print “Odd Number” and
doesn’t return any value.
public class Solution {
public static void checkEvenOdd() {
int num = 24;
if(num % 2 == 0) {
System.out.println("Even Number");
}
else {
System.out.println("Odd Number");
}
}
public static void main(String args[]) {
// Method Calling
checkEvenOdd();
}}
Output:
Even Number
No arguments passed but return a value
There could be a requirement in our program where we may
need to design a method that takes no argument(s) but returns
a value to the calling method.
Example:
public class Solution {
public static int sumOfTwoNumbers() {
int a = 10;
int b = 20;
int sum = a + b;
return sum;
}
public static void main(String args[]) {
// No arguments passed in the method
int sum = sumOfTwoNumbers();
System.out.println(sum);
}
}
Output:
30
Arguments passed but don't return a value
In this example, we have created a method to check whether a
number is even or odd. This method doesn’t return any value
but when we call this function we need to pass argument(s) to
it.
Example:
public class Solution {
public static void findEvenodd(int num) {
if(num % 2 == 0) {
System.out.println("Even Number");
}
else {
System.out.println("Odd Number");
}
}
// Driver Method
public static void main(String args[]) {
int num = 24;
// argument passed in the method
findEvenodd(num);
}
}
Output:
Even Number
Arguments passed and do return a value
In this example, we have created a method that returns the
sum of the two numbers and accepts argument(s).
Example:
public class Solution {
public static int sumOfTwoNumbers(int num1, int
num2) {
int sum = num1 + num2;
// Return sum
return sum;
}
public static void main(String args[]) {
int a = 10;
int b = 20;
}
Output:
30
* Modifiers=
There are two type of Modifiers in Java
access modifiers and non-access Modifiers,
* access Modifiers
access madifiers is the access type of the Method, it specified
the visibility of the Method,
→ public
→ protected
→ private
→ default
Non-access modifiers
Non-access modifiers provide information
about the characteristics of a class , method
or variable to the JVM
→ Static
→ Final
→ Abstract
→ Synchronized
→ Volatile
→ Transient
→ Native
Access modifires
Private
The access level of a private modifier is only within the
class. It cannot be accessed from outside the class.
default
The access level of a default modifier is only within the
package. It cannot be accessed from outside the package. If you
do not specify any access level, it will be the default.
protected The access level of a protected modifier is within the
package and outside the package through child class. If you do
not make the child class, it cannot be accessed from outside the
package.
public
The access level of a public modifier is everywhere. It can
be accessed from within the class, outside the class, within the
package and outside the package.
Non-access modifiers
Non-access modifiers provide information about the
characteristics of a class, method, or variable to the JVM. Seven
types of Non-Access modifiers are present in Java. They are –
Static
can be accessed without creating as instance of the class,
Final
The value of the variable cannot be changed once it is
assigned, it can be appiled to method and class aslo,
Abstract
abstract keyword is used to declare a class as particially
impemented means that objet can not be created directly from
this class,
Synchronized
the synchronized keyword used to indicate that a
method can be accessed by only one thread at a time,
Transient
Attributes and methods are skipped when serializing
the object containing them
Volatile
The value of an attribute is not cached thread-locally,
and is always read from the "main memory"
* Constructas =
* Type of Constructor
· Default constructor (no-arg constructor)
· Parameterized constructor
* Default Constructor=
A Constructor which have not any
paramter is called default constructer.
Example
class A {
int age;
String name;
A() {
age = 23;
name = "Adil";
}
void show() {
System.out.println(age + " " + name);
}
}
class B {
public static void main(String args[]) {
A sc = new A();
sc.show();
}
}
Java Parameterized Constructor
A constructor which has a specific number of parameters is
called a parameterized constructor
class A {
int x, y;
A(int a, int b) {
x = a;
y = b;
}
A(int a, String b) {
System.out.println(a + " " + b);
}
void show() {
System.out.println(x + " " + y);
}
}
class B {
public static void main(String args[]) {
A sc = new A(100, 200);
sc.show();
System.out.println("");
A s = new A(10, "Adil");
}
}
Constructor Overloading
output= 111 Adil
222 Adil 25
Java static keyword
The static keyword in Java is used for memory management
mainly. We can apply static keyword with variables, methods,
blocks and nested classes. The static keyword belongs to the
class than an instance of the class.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class
this keyword in Java
this is a reference variable that refers to the current object.
Here is given the 6 usage of java this keyword.
· this can be used to refer current class instance variable.
· this can be used to invoke current class method (implicitly)
· this() can be used to invoke current class constructor.
· this can be passed as an argument in the method call.
· this can be passed as argument in the constructor call.
· this can be used to return the current class instance from
the method.
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires
all the properties and behaviors of a parent object.