Data type casting in java

A data type casting done in two ways

1. widening
2. narrowingdata-type-castingwidening


  •  Casting lower data type to higher data type is known as widening
  • The widening can be done either implicit or explicit.

Narrowing


Casting higher data type to lower data type is known as narrowing.the narrowing operation should be done explicitly.

NOTE: whenever we perform narrowing there will be a loss of data.

Data type casting example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package casting; 
public class datatypecasting 
{ 
       public static void main(string[] args) 
       { 
          system.out.println(""); 
          double d=12; 
     //widening (integer type is converted into double) it done automatically 
 
          int a=(int)12.6;
     //narrowing(double type is converted into integer) 
 
          system.out.println("d value: "+d); 
          system.out.println("a value: "+a); 
          system.out.println(""); 
      } 
} 
output: 
 
d value: 12.0 
a value: 12

Spring AOP Around advise

Around advice:


Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

Let’s understand it with Example

Project structure


Student.java

 
package com.training;
 
public class Student 
{
 
	String name;
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
 
 
	public void get_info()
	{
		System.out.println("Hello "+name);	
	}
 
}

loggingAspect.java

package com.training;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
 
@Aspect
public class LoggingAspect 
{
 
	@Around("execution(public void get_info())")
	public void loggingadvise(ProceedingJoinPoint p)
	{
 
		System.out.println("Advise is runnning before get_info() method is called.....");
 
		try 
		{
			p.proceed();
		}
		catch (Throwable e) 
		{
			e.printStackTrace();
		}
 
		System.out.println("Advise is runnning after get_info() method is called.....");
 
    }
}

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-5.0.0.xsd">
 
<aop:aspectj-autoproxy/>	
 
<bean name="stu" class="com.training.Student">
<property name="name"  value="bhavesh_lakhani" />
</bean>
 
<bean name="loggingAspect" class="com.training.LoggingAspect"/>
 
</beans>

TestAroundAdvise.java

package com.training;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestAroundAdvise 
{
 
	public static void main(String[] args) 
	{
 
	   ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
 
	   Student st=(Student)context.getBean("stu");
 
	   st.get_info();
 
	   System.out.println("********************");
 
	}
}

Output:

Download this project developed using eclipse
AroundAdvise