Menu

Recursive method in Java

How to write a recursive method in Java?

Below is the code snippet of a recursive method in Java to generate numbers and check the number and execute the method again. In the below example printNumber() is a recursive method that will get called inside its own body.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package rashidjorvee;

public class RecursiveMethod {

 public static void main(String[] args) {

  RecursiveMethod recursiveMethod=new RecursiveMethod();
  recursiveMethod.printNumber(2);

 }
 private void printNumber(int j) {
  try {
   int i=j; 
    if(i==2) {
     printNumber(4);
    }
    System.out.println("Number "+i);
    }
  catch(Exception e) {

  }
 }
}

List of all the AEM components

List of all the AEM component and detail of that component and where all this component is being used.

First, navigate to the components path using the below URI.
http://localhost:4502/libs/wcm/core/content/sites/components.html

From right-hand side apply the filters on the basis of site path and group to get the filtered list of components.
http://localhost:4502/libs/wcm/core/content/sites/components.html
Components in AEM

From the filtered list select your component or click on the name of the component to open the detail of that component in AEM.


Now move to Live usage tab to view the list of pages where this component is dropped.


How to create session from resourceResolver object?

Sometimes we need a session to work with some special JCR API which we can,t use with the help of resourceResolver then we can create an instance of a session in the same class file using the object of resourceResolver. Below is the code snippet.

First, we need to inject the ResourceResolverFactory interface to create a new resourceResolver in AEM. You could inject using the SCR annotation @Reference or Sling Model annotation @Inject.
@Reference
private ResourceResolverFactory resolverFactory;
Now create the ResourceResolver object using resourceResolverFactory. This line of code will create a rsourceResolver with administrator login.
ResourceResolver resourceResolver=resolverFactory.getServiceResourceResolver(null);
Now will create the instance of Session using the object of resourceResolver.
Session session = resourceResolver.adaptTo(Session.class);

How to run a Java Application or program without main method?

Below is the code snippet to execute and run a java program without main method.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package rashid.jorvee;

import javafx.stage.Stage;

public class RunAJavaApplicationWithoutMainMethod extends javafx.application.Application {
 
 @Override
 public void start(Stage primaryStage) throws Exception {
  System.out.println("Running Application!!!");
 }
}

Generate i18n key node element for AEM

Create an excel file with i18n key and value. Then in the next cell apply the concatenate formula to create the i18 XML element from given key and value. Below is the concatenate formula.
=CONCATENATE("<",A2," jcr:primaryType=","""","sling:MessageEntry",""""," sling:key=","""",A2,""""," sling:message=","""",B2,"""","/>")
After applying the formula you will get the xml element as below. Now just copy and paste these elements in your i18n language.xml file.

<userName jcr:primaryType="sling:MessageEntry" sling:key="userName" sling:message="Rashid"/>

Universal Human Rights

A person has following Universal Human Rights because he/she born a Human.
1. We Are All Born Free & Equal
We are all born free. We all have our own thoughts and ideas. We should all be treated in the same way.
2. Food and Shelter for All
We all have the right to a good life. Mothers and children, people who are old, unemployed or disabled, and all people have the right to be cared for.
3. No Torture
Nobody has any right to hurt us or to torture us.
3. Freedom of Thought and Religion
We all have the right to believe in what we want to believe, to have a religion, or to change it if we want.
4. Workers Rights
Every grown-up has the right to do a job, to a fair wage for their work, and to join a trade union.
5. Freedom of Expression
We all have the right to make up our own minds, to think what we like, to say what we think, and to share our ideas with other people.
6. The Right to Democracy
We all have the right to take part in the government of our country. Every grown-up should be allowed to choose their own leaders.
7. The Right to Play
We all have the right to rest from work and to relax.
8. The Right to Public Assembly
We all have the right to meet our friends and to work together in peace to defend our rights. Nobody can make us join a group if we don't want to.
9. Social Security
We all have the right to affordable housing, medicine, education, and childcare, enough money to live on and medical help if we are ill or old.
10. The Right to Your Own Things
Everyone has the right to own things or share them. Nobody should take our things from us without a good reason.
11. Marriage and Family
Every grown-up has the right to marry and have a family if they want to. Men and women have the same rights when they are married, and when they are separated.
12. Right to a Nationality
We all have the right to belong to a country.
13. The Right to Seek a Safe Place to Live
If we are frightened of being badly treated in our own country, we all have the right to run away to another country to be safe.
14. Freedom to Move
We all have the right to go where we want in our own country and to travel as we wish.
15. The Right to Privacy
Nobody should try to harm our good name. Nobody has the right to come into our home, open our letters, or bother us or our family without a good reason.
16. We're Always Innocent Till Proven Guilty
Nobody should be blamed for doing something until it is proven. When people say we did a bad thing we have the right to show it is not true.
17. No Unfair Detainment
Nobody has the right to put us in prison without good reason and keep us there or to send us away from our country.
18. Your Human Rights Are Protected by Law
We can all ask for the law to help us when we are not treated fairly.
19. You Have Rights No Matter Where You Go
I am a person just like you!
20. The Right to Life
We all have the right to life, and to live in freedom and safety.
21. Don't Discriminate
These rights belong to everybody, whatever our differences.
22. We Are All Born Free & Equal
We are all born free. We all have our own thoughts and ideas. We should all be treated in the same way.
23. Copyright
Copyright is a special law that protects one's own artistic creations and writings; others cannot make copies without permission. We all have the right to our own way of life and to enjoy the good things that art, science and learning bring.
24. Responsibility
We have a duty to other people, and we should protect their rights and freedoms.
25. A Fair and Free World
There must be proper order so we can all enjoy rights and freedoms in our own country and all over the world.
26. No Slavery
Nobody has any right to make us a slave. We cannot make anyone our slave.


For more detail information and read the reference:

trimToSize() in Java

trimToSize() method used to reduce the capacity of the ArrayList, StringBuffer and StringBuilder in Java. Trims the capacity of the instance(ArrayList, StringBuffer and StringBuilder) to be the list's current size. An application can use this operation to minimize the storage of those instances.
Let understand this method using a scenario, for example, we have created a StringBuffer and assign some value into it. suppose we have put "trim to size method in java" string into it. And when you will pull the length of the StringBuffer then it will return the actual size of the string, which, we have assign into it, in our case, it will return 27. But when you will fetch the capacity if the StringBuffer then it will give you output more than the length of StringBuffer, in our case it is returning 34. This means this Buffer has more capacity and could store more data into it. Since our text has the length of 27, and we don't want that extra capacity anymore in our instance then we could remove and free the extra capacity from that instance using trimToSize method. And this method will trim the capacity of an instance with the size or length of the instance.  

Here is an example code of trimToSize() method with StringBuffer

Output:
trim to size method in java, and length of StringBuffer copyText is:27
Capacity of copyText is::34
trim to size method in java, and length of StringBuffer copyText after trimToSize() is:27
Capacity of copyText after trimToSize() is::27