Skip to main content

Posts

Showing posts with the label JAVA Interview Question and Answers

Troubleshooting Guide: Windows 11 Taskbar Not Showing - How to Fix It

  If your Windows 11 taskbar is not showing, you can try several troubleshooting steps to resolve the issue. Here are some potential solutions you can try:

JavaScript Date Formatting: Convert to yyyy-mm-dd Format with Ease!

  To format a JavaScript date as yyyy-mm-dd (e.g., 2023-05-17), you can use the following code: function formatDate ( date ) { const year = date. getFullYear (); const month = String (date. getMonth () + 1 ). padStart ( 2 , '0' ); const day = String (date. getDate ()). padStart ( 2 , '0' ); return ` ${year} - ${month} - ${day} ` ; } // Example usage const date = new Date (); const formattedDate = formatDate (date); console . log (formattedDate); In this code, the formatDate function takes a Date object as input and returns the formatted date string in the desired format. It retrieves the year, month, and day components from the Date object and pads single-digit month and day values with a leading zero if necessary. Finally, it concatenates the components using the desired format pattern (yyyy-mm-dd) and returns the formatted string. You can replace the new Date() part in the example usage with any Date object you want to format, or you can pass a

Demystifying java.lang.OutOfMemoryError: PermGen Space in Java Applications

The error message "java.lang.OutOfMemoryError: PermGen space" indicates that the JVM (Java Virtual Machine) has run out of memory in the permanent generation space. The permanent generation is a part of the JVM memory that stores metadata about classes, methods, and other internal JVM structures. To resolve this issue, you can try the following solutions: Increase PermGen space: By default, the size of the permanent generation space is relatively small. You can increase it by adding the -XX:MaxPermSize or -XX:MaxMetaspaceSize option to the JVM arguments. For example: java - XX :MaxPermSize= 256m YourApplication This sets the maximum size of the permanent generation space to 256 megabytes. Note that the -XX:MaxPermSize option is used in older Java versions (up to Java 7), while -XX:MaxMetaspaceSize is used in Java 8 and later. Upgrade to a newer Java version: Starting from Java 8, the permanent generation space was replaced with the metaspace, which dynamically adjusts it

How to enumerate an enum in JAVA?

  Photo by Nout Gons: https://www.pexels.com/photo/city-street-photo-378570/ In Java, you can enumerate an enum using a foreach loop or the Enum.values() method. Here's an example of using a foreach loop to enumerate an enum : java enum Color { RED, GREEN, BLUE } for (Color color : Color.values()) { System.out.println(color); } In this example, the Color enum is defined with three values: RED , GREEN , and BLUE . The Color.values() method is called to retrieve an array of all possible Color values. The foreach loop then iterates over the array, printing each Color value to the console. Alternatively, you can use the Enum.valueOf() method to retrieve a specific enum constant by name. Here's an example: java Color color = Enum.valueOf(Color.class, "RED" ); System.out.println(color); In this example, the Enum.valueOf() method is used to retrieve the RED constant from the Color enum . The first argument to the method is the Class object for

How to stop SQL Injection in JAVA web application?

  To prevent SQL Injection attacks in a Java web application, you should implement the following best practices: Use Prepared Statements: Use prepared statements instead of dynamic SQL queries in your code. Prepared statements allow you to define the SQL statement separately from the parameters, preventing malicious input from being executed as SQL code. Sanitize User Input: Validate and sanitize all user input before using it in SQL queries. Use regular expressions and input validation techniques to ensure that the user input conforms to the expected format and does not contain any malicious code. Use Stored Procedures: Use stored procedures instead of inline SQL statements in your code. Stored procedures can be compiled and optimized by the database engine, reducing the risk of SQL injection attacks. Limit Database Permissions: Limit the database permissions of the user account that is used to connect to the database from the web application. The account should have the minimum requi