what is Type-casting in java?
- Type casting in Java is the process of converting a value from one data type to another. This can be done either automatically by the compiler (implicit casting) or manually by the programmer
There are two main types of casting: **
**Widening Casting (Implicit):
* This occurs when a smaller data type is converted to a larger data type. This conversion is done automatically by the compiler because there is no loss of data.
* Implicit casting, also known as upcasting in the context of object-oriented programming,
* converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
SYNTAX
int myInt = 9;
double myDouble = myInt; // Automatic casting from int to double
System.out.println(myDouble);
Output:
9.0
Narrowing Casting (Explicit):
* This occurs when a larger data type is converted to a smaller data type. This conversion requires explicit casting by the programmer because there is a possibility of data loss.
* Explicit casting, also known as Downcasting.
* converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
SYNTAX:
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting from double to int
System.out.println(myInt);
Output:
9 (data loss occurs here)
Difference between NARROWING and WIDENING
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ACCESS MODIFIERS
* Access modifiers in Java are keywords that control the visibility and accessibility of classes, methods, variables, and constructors within a Java program.
Types of Access Modifiers
* There are 4 types of access modifiers available in Java:
1.public:
* Members declared as public are accessible from anywhere in the program, including other classes, packages, and subclasses. This provides the broadest level of access.
* Description: The member is accessible from any other class, regardless of the package.
Syntax:
public class MyClass {
public int publicVariable;
public void publicMethod() {
// ...
}
}
2.private:
* Members declared as private are only accessible from within the same class where they are defined. They cannot be accessed by other classes, even within the same package or by subclasses. This offers the highest level of restriction.
* Description: The member is accessible only within the class where it is declared.
Syntax:
class MyClass {
private int privateVariable;
private void privateMethod() {
// ...
}
}
3.protected:{TBD}
* Members declared as protected are accessible within the same package and by subclasses, even if the subclasses are in a different package. This allows for controlled access within a hierarchy.
* Description: The member is accessible within its own package and by subclasses (even if the subclasses are in different packages).
Syntax:
package com.example.package1;
public class MyClass {
protected int protectedVariable;
protected void protectedMethod() {
// ...
}
}
package com.example.package2; // Different package
import com.example.package1.MyClass;
class MySubclass extends MyClass {
// Can access protectedVariable and protectedMethod
}
4.default (or package-private):
* If no access modifier is explicitly specified, the member has default access. This means it is only accessible within the same package. It cannot be accessed by classes in other packages, even by subclasses in a different package.
* Description: If no access modifier is specified, the member has default access. It is accessible only within its own package.
Syntax:
package com.example.my_package;
class MyClass {
int defaultVariable; // No modifier, so default access
void defaultMethod() { // No modifier, so default access
// ...
}
}
REFERED LINK:
- https://www.w3schools.com/java/java_type_casting.asp
- https://www.geekster.in/articles/type-casting-java/
- https://www.shiksha.com/online-courses/articles/type-casting-in-java-blogId-166877
- https://www.geeksforgeeks.org/access-modifiers-java/
- https://www.shiksha.com/online-courses/articles/access-modifiers-in-java/
Top comments (0)