You are reading the article How To Use And Work With Trim Function? updated in December 2023 on the website Minhminhbmm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 How To Use And Work With Trim Function?
Introduction to PL/SQL TRIMPL/ SQL trim is the function provided in PL/ SQL Database Management System which helps to remove all the trailing or leading blank spaces or any other character if required. This function is mostly used to make sure that the data being stored in the column field is as much optimized as possible and does not contain any other blank spaces at starting or ending of the string being stored. However, this function can also be used in other scenarios where we have a requirement to remove the initial or ending occurrence of a particular character in the string. In this article, we will study the general syntax, usage, and implementation of PL/ SQL trim function along with the help of certain examples.
Start Your Free Software Development Course
Syntax and WorkingThe syntax of the PL/ SQL trim function is as shown below:
In the above syntax, the terms that are used and the working of trim function when they are specified or not specified is as explained below:
TRAILING – Whatever source string is supplied in the input, if we want to remove all the occurrences of the character to be removed parameter from the source string that occurs in the ending portion of the source string by specifying the keyword TRAILING after TRIM () function inside it as the first parameter. If the characters at the end match with the character to be trimmed then it is removed from the end of the source input string.
LEADING– Whatever source string is supplied in the input, if we want to remove all the occurrences of the character to be removed parameter from the source string that occur in the beginning portion of the source string by specifying the keyword LEADING after TRIM () function inside it as the first parameter. If the characters at the beginning match with the character to be trimmed then it is removed from the start of the source string.
BOTH – When we specify the keyword BOTH in the first parameter of the TRIM syntax then all the matching character occurrences that have equal value as that of the character to be trim parameter are removed from the beginning and the ending of the source string. By default, if we don’t specify any value in the first parameter then it is considered as BOTH.
Character to be trimmed – This character is considered for doing a match by oracle database to select which characters need to be removed. In case, if this parameter is not specified then the default value considered is blank space and the same value is considered as the character to be trimmed.
Output:
The string format is returned as the output of the input string. Whatever supplied string is processed for removal of the occurrences of the character to be trimmed is treated as the output.
Points to be considered:
In case if only one parameter is supplied in the trim () function then the DBMS removed all the trailing and leading occurrences of the blank spaces in the supplied parameter of the trim () function as this parameter is considered as the source string. If we try to specify either the source string ass null or the character to be trimmed as null the output of the trim function is null itself.
Support for Oracle/ PLSQL versions for TRIM() function:
The following versions of ORACLE support the usage of the TRIM() function:
Oracle 8i
Oracle 9i
Oracle 10g
Oracle 11g
Oracle 12c
Advantages of PL/SQL TRIM Examples of PL/SQL TRIMLet us consider some examples which will help us to understand the implementation of the TRIM () function in PL/ SQL.
Example #1Let us first consider an example that will help us to demonstrate how we can remove the LEADING occurrences of the particular character in the source string. Consider one string “33EDUCBA33” from which we want to remove the 3 characters from the beginning of the string. In such a case, we can make the use of following query program in PL/ SQL –
DECLARE sample input string(20) := '33EDUCBA33'; BEGIN dbms_output.put_line(TRIM(LEADING '3' FROM sampleInput)); END;The output of the execution of the above program is as shown below:
Example #2Now, let us consider the same input string and we will use the TRIALING parameter as the first parameter of the TRIM () function. This will lead to the removal of all the occurrences of the ending characters from the source string. The program for doing so in PL/ SQL will be as shown below:
DECLARE sampleInput string(20) := '33EDUCBA33'; BEGIN dbms_output.put_line(TRIM(TRAILING '3' FROM sampleInput)); END;The output of the execution of the above program is as shown below:
Example #3Let us consider an example where we will be using BOTH as the first parameter to the trim () function. In this case the program will become as shown:
DECLARE sampleInput string(20) := '33EDUCBA33'; BEGIN dbms_output.put_line(TRIM(BOTH '3' FROM sampleInput)); END;The output of the execution of the above program is as shown below:
In the above program even if we don’t specify any parameter in first place like TRAILING, LEADING, or BOTH still the function will remove the beginning and ending occurrences of the character to be trimmed from the source string.
ConclusionThe trim () function is used in PL/ SQL to remove any of the occurrences of the particular character in either the beginning or the ending of the source string or even in both. This function is mostly used for efficient use of space in database while storing the strings which removes all the occurrences of blank spaces if present at the end of the string. Any character at end of the beginning can be removed when used properly.
Recommended ArticlesWe hope that this EDUCBA information on “PL/SQL TRIM” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading How To Use And Work With Trim Function?
How To Use Vba Timer Function With Examples?
VBA TIMER
Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.
This function cannot be performed in Excel. In Excel, we can only see the time and add it to any cell but cannot measure it as Timer.
You can download this VBA TIMER Excel Template here – VBA TIMER Excel Template
Examples of VBA TIMERFollowing are the different examples:
Example #1In this example, we will see how to count or measure the time to run the completed code. For this, go to the insert menu of VBA and select a Module as shown below.
Once we do that, it will open a new module, as shown below. Write the sub-category of the current function name or any other name you choose.
Code:
Sub
Timer1()End Sub
Now define two dimensions as Seconds1 and Seconds2 as a SINGLE function, which means there were two individual single number data (Without decimal).
Code:
Sub
Timer1()Dim
Seconds1As Single
Dim
Seconds2As Single
End Sub
To Start the Timer, select the defined dimension Seconds1 and assign the function TIMER. And do the same thing for another dimension Seconds2 as shown below. The purpose of doing this is to measure the start and end of the time.
Code:
Sub
Timer1()Dim
Seconds1As Single
Dim
Seconds2As Single
Seconds1 = Timer() Seconds2 = Timer()End Sub
This completes the timer portion of the code. Now we need to see the time-lapse in running the code. We need to print the output in the message box, as shown below.
In the below screenshot, we printed the text “Time taken:” and the difference between Seconds2 and Seconds1 with unit seconds.
Code:
Sub
Timer1()Dim
Seconds1As Single
Dim
Seconds2As Single
Seconds1 = Timer() Seconds2 = Timer() MsgBox ("Time taken:" & vbNewLine & Seconds2 - Seconds1 & " seconds")End Sub
We will see a message box with a counted time of 0 Seconds, as the time required to run the complete code was 0.
We may see slightly more differences if the written code is huge.
Example #2There is another method where we can choose a time-lapse of any small amount of waiting time and let the user or operator wait till the process gets completed. This can be used to create a tool or macro with a huge line of code structure. Doing this will allow the user to wait for the whole code to run and the operation to complete because doing something when the code is running may crash the file.
For this, open a new module and write the Sub-category in the name of the required function or any name, as shown below.
Sub
Timer2()End Sub
Now to make this code small and simple, we will use the inbuilt functions of VBA. And for this type of Application, follow a dot (.), and then, from the list search, select the Wait function as shown below.
This Wait function will allow us to add waiting time till the complete code may get run. This wait time is Boolean. After that, when running the program, it will display the execution time as 0 seconds plus the desired waiting time using TimeValue.
Code:
Sub
Timer2() chúng tôi Now + TimeValue("00:00:10")End Sub
Here we consider 10 seconds as the waiting time to complete the code run.
To print the waiting time, we need to print the message in the message box with the help of the command MsgBox, as shown below.
Code:
Sub
Timer2() chúng tôi Now + TimeValue("00:00:10") MsgBox ("Waiting Time - 10 Seconds")End Sub
As we can see, in the message box, we added the text “Waiting time – 10 Seconds” to be printed.
Now run the code using the F5 key or manually. After waiting for 10 seconds, we will see a message box with the message used in the code.
Example #3 – VBA TimerThere is another easy way to see and show the current time in VBA. For this, we will directly use MsgBox and the rest of the code there only. For this, open a new module in VBA and write Sub-category in the name of the used function or any other name as shown below.
Code:
Sub
Timer3()End Sub
Now write MsgBox, which is a command to print the message. In the bracket, we can write any message to be printed in the message box. Here we have chosen “Time is:” as our text, and along with it, “& Now ()” is used. It will display the current time with the date in the pop-up message box.
Code:
Sub
Timer3() MsgBox ("Time is : " & Now())End Sub
Now in another message box, we will count the number of seconds passed in the whole day till the current time by the clock. For this, write MsgBox, and between the brackets, write the text “Timer is:” along with “& Timer (),” as shown below.
Code:
Sub
Timer3() MsgBox ("Time is : " & Now()) MsgBox ("Timer is: " & Timer())End Sub
Once done, run the code using the F5 key or manually. We will get two separate message boxes, as shown below.
In the first message box, we will get the current date and time in DD/MM/YYYY and hh:mm:ss AM/PM format, which is the default format Excel. In the second message box, we will see time lapsed on that day in seconds.
The second message box shows Timer is 59953.62 Seconds. If we divide that by 60 Seconds and 60 Minutes, we will get the exact Timer in hours lapsed on that day which is 16.65 Hours.
Things to Remember About VBA TIMER
Always save the file in Marco Enabled Workbook to avoid losing the written VBA Code.
Always compile the complete code step by step to ensure every code is incorrect.
In example #1, the SINGLE function displays the number as a whole. Even if we use DOUBLE, it will give the result in decimal.
Recommended ArticlesThis has been a guide to Excel VBA TIMER. Here we discussed how to use the VBA TIMER function, some practical examples, and a downloadable Excel template. You can also go through our other suggested articles–
How Does The Filter_Var Function Work In Php?
Introduction to PHP filter_var
Php filter_var() is a function that is used to filter a given variable with a specified filter. To sanitize and validate the data such as email_id, IP address, etc., in Php, the filter_var() function is used (which contains the data). Validation in the text means whether the entered data is in the correct format or not. For example, in an email id of the person, whether the ‘@’ sign is present or not. In a phone number field, all the numbers or digits should be present. Sanitization means to sanitize the data entered or remove the illegal or unnecessary characters from it to prevent any future issues. For example, removing unnecessary symbols and characters from user email.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
filter_var(variable, filtername, options)where,
variable: This parameter stands for the variable field, the variable which needs to be filtered. It is the mandatory field.
filtername: This parameter stands for the name of the filter which the user wants to use. It is an optional parameter. If not specified, FILTER_DEFAULT is used, which means that not filtering would be done to the given variable.
options: This parameter is optional. It specifies the options/ flags to be used. It is basically an associative array of bitwise disjunctions of flags or options. If this parameter is used in the filter_var() function, a flag must be provided in the ‘flags’ field, and a callable type must be passed for the callback function. After accepting all the parameters, the filtered and sanitized variable is returned.
Return Value: The above function returns the filtered value or false if the data/ variable does not get filtered.
How does the filter_var function work in Php?In PHP, the filter_var() method accepts the above-explained various parameters and returns the validated/ sanitized data. Validation means checking the format of the data as specified by the programmer, and Sanitization means removing the unnecessary characters from the data to return the data as required by the programmer.
Examples of PHP filter_varLet us understand the working of the filter_var() function in Php along with the examples:
Example #1Validating an Integer value using filter_var() function:
Code:
<?php $value = 789787; if (filter_var($value, FILTER_VALIDATE_INT)) { echo(“Congratulations!!! $value is a valid integer value”); } else { echo(“Sorry!! $value is not a valid integer value”); }
Output:
Explanation:
In the above code, the Integer value to be validated is stored in the variable ‘value’ and is then passed in the filter_var() method along with the ‘FILTER_VALIDATE_INT’ filter name to validate it. Finally, conditional operators if and else are applied to check the condition, and the respective output is printed on the console using the ‘echo.’
Example #2Validating the IP address of the computer device using the filter_var() function
Code:
<?php $ip = ‘180.0.0’; if (filter_var($ip, FILTER_VALIDATE_IP)){ echo(“Congratulations!! $ip is a valid IP address, passed by the you”); } else { echo(“Sorry $ip is an incorrect IP address”); }
Output:
In the above code, the IP address of the computer or any other network device is validated using the filter_var() method. The ip address that is to be validated is stored in the variable ‘ip.’ Since the IP address has its specific format ‘x.y.z.w,’ it is validated using the ‘FILTER_VALIDATE_IP’ in the filter_var() function. Finally, the ip address passed is validated, and the respective output is printed on the console using ‘echo.’
Example #3Sanitizing and Validating the URL address using the filter_var() function
Code:
<?php $check_url = filter_var($check_url, FILTER_SANITIZE_URL); if(!filter_var($check_url, FILTER_VALIDATE_URL) == false) { echo(“Congratulations!!! $check_url is the correct URL”); } else { echo(“Sorry!! $check_url is an invalid URL”); }
Output:
Explanation:
In the above code, the URL address, which has a specific format, is sanitized first and then validated using the filter_var() method. The URL to be checked is stored in the variable ‘check_url.’ To sanitize the url, ‘FILTER_SANITIZE_URL’ is passed as a filter name along with the url. Once sanitized, url is then validated using the ‘FILTER_VALIDATE_URL’ filter name along with the url, and the respective output on validation is printed on the console using ‘echo.’
Example #4Validating the email address of the user using the filter_var() function
Code:
<?php $email_check = “[email protected]”; if (filter_var($email_check, FILTER_VALIDATE_EMAIL)) { echo(“Congratulations!! $email_check is a valid email address”); } else { echo(“Sorry!! You have entered an incorrect email address”); }
Explanation:
In the above example, the email address which is to be checked is stored in the variable ‘email_check.’ It is validated using the filter_var() function in Php, bypassing the email variable and the respective filter name (FILTER_VALIDATE_EMAIL). Since the passed email is invalid, so the response is printed on the console using the ‘echo.’
Example #5Code:
<?php $value = 465675; { echo “Integer $value is within the specified range”; } else { echo “Sorry!! Integer $value is not in the range provided by you”; }
Output:
Explanation:
In the above example, the Integer value is to be validated for the given range, i.e., 10 to 400 is tested. Then, in the filter_var() function, the value to be tested is passed along with the filter name (FILTER_VALIDATE_INT) and 1 optional parameter, i.e., ‘options’ having the array with the minimum and maximum range specified. Finally, the variable is validated, and accordingly, the response is printed on the console using the ‘echo.’
ConclusionThe above description clearly explains what is filter_var() functions in Php and how it works to validate and sanitize the variable passed in it. It is one of the important functions that programmers commonly use to filter the data to prevent a security breach. However, this function facilitates the use of different filters by passing the different parameters according to the specific requirements, so the programmer needs to understand it deeply before using it in the program.
Recommended ArticlesThis is a guide to PHP filter_var. Here we discuss the introduction, syntax, and working of the filter_var function in Php along with different examples and code implementation. You may also have a look at the following articles to learn more –
How To Use The Fact Or Factdouble Function In Excel
In Microsoft Excel, a FACT or FACTDOUBLE function are both a Math and Trigonometry function. Math and Trigonometry functions in Excel perform mathematical calculations, including basic arithmetic, condition sums and products, exponents, logarithms, and trigonometric ratios. The FACT function in Excel returns the factorial of a number. The factorial of a number returns the 1*2*3…* number. The FACTDOUBLE function returns the double factorial of a number. The formula and syntax for both the FACT and FACTDOUBLE functions are below.
Formula and Syntax
FACT
Formula: FACT (number)
Syntax: Number: the non-negative number for which you want the factorial. If the number is not an integer, it is truncated. It is required.
FACTDOUBLE
Formula: FACTDOUBLE (number)
Syntax: The value for which to return the double factorial. If the number is not an integer, it is truncated. It is required.
Follow the steps below on how to use the FACT or FACTDOUBLE in Microsoft Excel.
How to use the FACT function in Excel
Launch Microsoft Excel.
Enter your data or use existing data.
Type into the cell where you want to place the result = FACT(A2)
Press Enter to see the result. The result was 120.
Now drag the fill handle down to see the other results.
In the results, you will see that the value of a negative number will give the error #NUM.
There are two other methods to use the FACT function.
An Insert Function dialog box will appear.
Inside the dialog box, in the section Select a Category, select Math and Trigonometry from the list box.
In the section Select a Function, choose the FACT function from the list.
A Function Arguments dialog box will open.
Then select FACT from the drop-down menu.
A Function Arguments dialog box will open.
Follow the same method in Method 1.
How to use the FACTDOUBLE function in Excel
Launch Microsoft Excel.
Enter your data or use existing data.
Type into the cell where you want to place the result = FACTDOUBLE(A2)
Press Enter to see the result. The result was 105.
For 7, an odd number, the double factorial number is equivalent to 7*5*3.
For 6, an even number, the double factorial number is equivalent to 6*4*2.
Like the Fact function, there are two other methods to use the FACTDOUBLE function.
An Insert Function dialog box will appear.
Inside the dialog box, in the section Select a Category, select Math and Trigonometry from the list box.
In the section Select a Function, choose the FACTDOUBLE function from the list.
A Function Arguments dialog box will open.
Then select FACTDOUBLE from the drop-down menu.
A Function Arguments dialog box will open.
Follow the same method in Method 1.
We hope you understand how to use the FACT or FACTDOUBLE function in Excel.
What are the types of information that Excel uses?In Microsoft Excel, there are 4 types of data. These are:
Text: This data includes alphabet, numeral and special symbols.
Number: This data includes all types of numbers, such as large numbers, small fractions, and quantitative.
Logical: Data is either TRUE or FALSE
Error: Data occurs when excel recognizes a mistake or missing data.
READ: How to use the MINVERSE and MMULT functions in Excel
How do I check if a cell contains a formula in Excel?Follow the steps below to find cells that contain formulas:
Select a cell or a range of cells.
READ: How to use the TEXTSPLIT function in Excel.
How To Show Qualifications With Limited Work Experience
2. Showcase your skill set and meaningful extracurricular experiences.
While candidates may lack formal work experience, they still likely possess relevant skills or meaningful experience in related work. Showcase experiences that can translate to the workforce by highlighting extracurricular activities in which you excelled in college.
Highlight leadership experience. For example, serving as your school’s club lacrosse president might not seem like it’s connected to a job as a project manager, but the leadership experience may separate you from other candidates. “College students who were elected team captains, elected members of the college’s student council, editors of a college publication, stage managers, student directors of a college theater production, or officers in any student organization can likely recount some recent leadership or management experiences,” said Timothy Wiedman, a retired associate professor of management and human resources at Doane University. “Accomplishing group goals while directing the work of other students is a skill that can often transfer to an employment setting.”
Showcase unique experiences and accolades. Include volunteer experience, athletic achievements and GPA to indicate your value. These accolades and experiences don’t necessarily need to be in a separate portion of your resume. “Unpaid positions can be listed just like jobs within the experience section of your resume; they don’t need to be relegated to a volunteer section,” said Kelly Donovan, a writer of executive resumes and job search coach. “Your description can make it clear that this was an unpaid volunteer role.”
Share student roles. Donovan noted that your role as a student can be listed just like a job in the experience section. “You can write bullet points highlighting interesting projects you worked on while in school that would be relevant to your target jobs.”
3. Develop your personal brand through an online portfolio.LinkedIn and various social media platforms let students showcase their talent to employers 24 hours a day. Include a well-designed resume, detailed skills explanations and a portfolio of work from internships or classes to impress potential employers.
For those with limited work experience, an active and professional LinkedIn profile can make a world of difference in the hiring process.
“Recent graduates should know that a paper resume can only do so much for them,” said Andrew Selepak, director of the graduate program in social media at the University of Florida. “I encourage all of my students to create portfolio websites and LinkedIn accounts, and to list both at the top of their resume. Unlike a paper resume, an online portfolio can include multimedia as well as projects the students have completed at an internship.”
Online portfolios are an excellent way to showcase your skill set with a more interactive and visually appealing resume. Incorporate photos and videos to help the portfolio stand out.
Remember, employers want to know that you’ll add value from day one. With a great resume, portfolio and personal brand, you can showcase your abilities and impress hiring managers.
“In a world of unique disruption, allowing the resume to showcase the skills a candidate can deliver helps immensely when work experience is limited,” said Carole Stizza, work success coach at Relevant Insight. “Limited work history doesn’t mean you don’t have immense value.”
Tip
Opt for an online portfolio over a digital resume. This way, you can more vividly and dynamically show employers your value.
How do you gain relevant experience while in school?If you’re still in school, you can take steps to gain relevant experience that will appeal to future employers – and you don’t necessarily have to work while completing your degree.
Network with people in your field. They say it’s all about who you know, and often, they’re right. While a hiring manager might not think much of your low-experience resume, someone who knows you might see your potential. That person can vouch for you with hiring managers they know or even hire you at their own company.
Participate in relevant extracurricular activities. Let’s say you’re double-majoring in mechanical engineering and computer engineering, and your double-major has you too busy for much else. With your limited time to spare, low-key involvement with your school’s robotics club can give you excellent experience to list on your resume. Detailing your accomplishments with the club can sway hiring managers in your favor.
Take an internship. If you can afford to take an internship, doing so is arguably the best way to gain experience while you’re in school. Try turning your summer internship into a school-year experience – you can list that entire period on your resume to make it seem more job-like. See if you can get college credit for your internship so you can kill two birds with one stone. And if you find a paid internship, even better.
Key Takeaway
If you’re on the other end of the spectrum (a workforce veteran), it’s important to age-proof your resume by focusing on recent experience and omitting older positions.
How Java @Inherited Work With Examples To Implement
Introduction to Java @Inherited
The @inherited in Java is an annotation used to mark an annotation to be inherited to subclasses of the annotated class. The @inherited is a built-in annotation, as we know that annotations are like a tag that represents metadata which gives the additional information to the compiler. Same as built-in annotation, which is exits in the Javadoc, it is possible to create another meta-annotation out of existing in the java. There are actually two types of annotations, one type of annotations applied to the java code like @override, and another type of annotations applied to the other annotation like @target @inherited. So @inherited is an annotation that is applied to other annotation whose we want to create subclasses or we want to inherit to make another user define annotation.
Start Your Free Software Development Course
Syntax
The syntax of the @inherited in java is –
@Inherited public @interface MyAnnotation {// code of the MyAnnotation } @MyAnnotation public class SuperClass { public class SubClass extends SuperClass {As in the above syntax, the class SubClass is inherited from the annotation @MyAnnotation, because it is inherited from SuperClass, and SuperClass has a @MyAnnotation annotation.
How does @Inherited work in Java?The @Inherited annotation is used or annotated to the annotation (MyAnnotation as in above syntax), which the @interface should prefix. Next, this annotation (MyAnnotation) can be used where ever need to apply as @MyAnnotation. These annotations can be applied just before the declaration of an element and can be applied to any element of the program like variables, class, constructors, methods, etc. When this user-defined annotation is annotated on the superclass, it is automatically inherited to subclasses (subclass as in the above syntax), as we can see in the below examples.
Examples to Implement @Inherited annotation in JavaNext, we write the java code to understand the @Inherited annotation more clearly with the following example where we use @Inherited annotation to inherit in the subclass from the superclass, as below –
Example #1First, we create an interface for annotation @MyAnnotation, which has two fields, name and code.
Code: chúng tôi
package demo; import java.lang.annotation.Inherited; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Inherited @Target ({ElementType.TYPE, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) public @interface MyAnnotation { String name () default "unknown"; String code () default " "; }Next, we create a superclass to use the above annotation by annotating any class or method or variable and provide the state name and state code.
Code: package demo; import demo.MyAnnotation; @MyAnnotation (name = "Karnataka", code = "KA") public class Super { public String getstateinfo () { return null; } }
Next, we use an annotation because it is metadata, which means we should be able to get this metadata or information to use the annotation information when we need it.
Code: chúng tôi
package demo; import demo.MyAnnotation; import demo.Super; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; public class Demo extends Super { public static void main ( String[] arg ) throws Exception { new Super (); getstateinfo (obj); Method m = obj.getMethod ("getstateinfo", new Class[]{}); getstateinfo (m); } static void getstateinfo (AnnotatedElement e) { try { System.out.println ("Finding annotations on " + e.getClass ().getName ()); Annotation[] annotations = e.getAnnotations (); for (Annotation a : annotations) { if (a instanceof MyAnnotation) { MyAnnotation stateInfo = (MyAnnotation) a; System.out.println("Name of Annotation :" + stateInfo.annotationType ()); System.out.println("State Name :" + chúng tôi ()); System.out.println("State code :" + chúng tôi ()); System.out.println(new Demo ().getClass ().getAnnotation (MyAnnotation.class)); System.out.println(new Super ().getClass ().getAnnotation (MyAnnotation.class)); } } } catch (Exception ex) { System.out.println( ex ); } } }Output: When we run the chúng tôi class, the output is.
Explanation: As in the above code, the MyAnnotation annotation is created an also annotated by @Inherited. In the Superclass, the MyAnnotation annotation was using by the statement @MyAnnotation and annotated to the class. And another class Demo is created, which is the subclass of the Superclass because it is extended to Superclass. Farther in the main () method of the Demo class, an object of the Superclass is creating and access its method that is getstateinfo (), through this methoditerating all its annotations and checking whether the annotation is inatnce of MyAnnotation, if yes then printing some of the information as we can see above. But one important thing is that the Demo class or any of its elements not annotated to the MyAnnotation, but it still showing that the MyAnnotation is annotated to this class because it is inherent to the Superclass and Superclass is inherited MyAnnotation.
Next, we rewrite the above java code to understand the @Inherited annotation more clearly with the following example where we will not use @Inherited annotation to annotation MyAnnotation (as annotation created in the above example) to check whether this annotation is inherited in the subclass from its superclass or not, as below –
Example #2Code: chúng tôi
package demo; import java.lang.annotation.Inherited; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target ({ElementType.TYPE, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) public @interface MyAnnotation { String name () default "unknown"; String code () default " "; }Output: Next, when we run the chúng tôi class, the output is.
Explanation: As in the above output, we can see that after state code, the “null” value is printed, that is the return value of the statement “new Demo ().getClass ().getAnnotation (MyAnnotation.class)”, which means that the demo class is not inherited (or annotated) any MyAnnotation annotation from it Superclass, because the @Inherited annotation is not annotated to MyAnnotation to inherit it in the subclass.
ConclusionThe @inherited in java is a built-in annotation applied to another annotation. It is used to marks an annotation to be inherited to subclasses of the annotated class. The @inherited is available in the package java.lang.annotation.Inherited.
Recommended ArticlesThis is a guide to Java @Inherited. Here we discuss an introduction to Java @Inherited along with the working, appropriate syntax and respective examples to implement. You can also go through our other related articles to learn more –
Update the detailed information about How To Use And Work With Trim Function? on the Minhminhbmm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!