Wednesday, August 30, 2017

Java Bitwise Operators

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then −
OperatorDescriptionExample
& (bitwise and)Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) will give 12 which is 0000 1100
| (bitwise or)Binary OR Operator copies a bit if it exists in either operand.(A | B) will give 61 which is 0011 1101
^ (bitwise XOR)Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) will give 49 which is 0011 0001
~ (bitwise compliment)Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< (left shift)Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>> (right shift)Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 will give 15 which is 1111
>>> (zero fill right shift)Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.A >>>2 will give 15 which is 0000 1111
Reference : Tutorial Point



Sunday, June 18, 2017

Spring Profiles

* Spring provides @Profile annotation using which we create profiles. @Profile is used with @Configuration and spring stereotypes such as @Component, @Service etc.

* Different profile is created for different environment. For example we can have environment such as production, development and testing. In development environment we can enable development profile and in production environment we can enable production profile and so on.

*  A profile can be activated using (spring.profiles.active)
  • property file .properties/.yml
  • command line 
    • an Environment Variable (java -jar abc.jar  spring.profiles.active=prod)
    • a JVM Property (java -jar -Dspring.profiles.active=prod abc.jar)
    • You can set list of profiles like ; java -jar -Dspring.profiles.active="prod,test,x" abc.jar
  • programmatically
  • Context parameter in web.xml
  • @ActiveProfile in test
* When we add active profile using command line then the active profile added in property file is replaced

*We can add active and default profile programmatically by using ConfigurableEnvironment
 
 *You can also configure profile using spring.profiles.include that will be included for every active profile.

*In spring boot testing we can add active profile by using @ActiveProfiles annotation

 *We can create property file using profile name using the convention application-{profile}.properties|yml

* In application.yml file you can define  active profile using spring.profiles.active=dev 

* We can add active profiles using command line with java command. In this case active profile configured in property file will be replaced by active profile passed in command line

* How to set active profile programatically
  public static void main(String[] args) {
      SpringApplication application = new SpringApplication(MyApplication.class);
      ConfigurableEnvironment environment = new StandardEnvironment();
      environment.setActiveProfiles("dev","test");
      application.setEnvironment(environment);
      application.run(args);
  }    

*How to set default profile programatically - 1
  public static void main(String[] args) {
     SpringApplication application = new SpringApplication(MyApplication.class);
     ConfigurableEnvironment environment = new StandardEnvironment();
     environment.setDefaultProfiles("dev","test");
     application.setEnvironment(environment);
     application.run(args);
  }      

 * How to set default profile programatically -2
@Profile({"dev","default"}) 

*You can have negative profile. For example when you are annotating class with @Profile annotation, you can negotiate it. Like @Profile("!dev"). It means
  • if dev profile is active this bean won't be created
  • if dev profile is not active then this bean will be created. And will belong to any other profile than dev.

Reference/Useful links






Thursday, June 15, 2017

Inversion of Control And Dependency Injection

This post is about what is IOC container? And what is DI?

* Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).
Giving control to the container to get an instance of the object is called Inversion of Control., means instead of you are creating an object using the new operator, let the container do that for you.
The main tasks performed by IoC container are: to instantiate the application class. to configure the object. to assemble the dependencies between the objects.

* DI means the IoC principle of getting dependent object is done without using concrete objects but abstractions (interfaces). This makes all components chain testable, cause higher level component doesn't depend on lower level component, only from the interface. Mocks implement these interfaces.


There are several basic techniques to implement inversion of control. These are:
  • Using a factory pattern
  • Using a service locator pattern
  • Using a dependency injection of any given below type:

    1). A constructor injection
    2). A setter injection
    3). An interface injection
    * Spring supports only Constructor Injection and Setter/Getter Injection.
Useful/Source links:
Stack Overflow Discussion
Martin Fowler Defintions
Short Video Definition of IOC