Java Articles

Page 275 of 450

Reverse and Add Function in Java

Sunidhi Bansal
Sunidhi Bansal
Updated on 03-Nov-2021 835 Views

We are given with an integer and the agenda here is to reverse the digits of the number and add the reversed number to the original number and check if the resultant number is a palindrome or not and the process is repeated until it does. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).For ExamplesInput − 1678Output − Palindrome of the given input 1678 293392Explanation − The input number is first reversed and then added to the original number, it is then checked for palindrome if it is not a palindrome ...

Read More

Reverse actual bits of the given number in Java

Sunidhi Bansal
Sunidhi Bansal
Updated on 03-Nov-2021 1K+ Views

Given an integer n that is not negative. The goal is to reverse the bits of n and report the number that results from doing so. While reversing the bits, the actual binary form of the integer is used; no leading 0s are taken into account.Let us see various input output scenarios for thisInput − 13Output − Reverse actual bits of the given number 11(13)10 = (1101)2. After reversing the bits, we get: (1011)2 = (11)10.Explanation − The binary bits are obtained from the input number which is then reversed and finally converted to decimal format which is returned as output.Input − 18Output − ...

Read More

Merge a linked list into another linked list at alternate positions in Java

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Oct-2021 969 Views

We are given two data structures as linked list Let’s say, List_1 and List_2. The task is to merge the elements of linked list ‘List_2’ into the linked list ‘List_1’ at an alternate position and if we are left with the elements which can't be merged into ‘List_1’ then it will be printed as the ‘List_2’ remaining elements.For Example-:In −List_1 =List_2 =Out − Merged List is-:Explanation − we are given with two lists i.e. List_1 and List_2. We had merged the possible elements of list_2 into List_1 at alternate positions. So, elements after merging in List_1 are 10- >3->2->1->1->4->2->5->5 and in List_2 are ...

Read More

How to get (format) date and time from mill seconds in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 07-Sep-2021 2K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object.To format milli seconds to date −Create the format string as dd MMM yyyy HH:mm:ss:SSS Z.The Date class constructor accepts a long value representing the milliseconds as a parameter and creates a date object.Finally format the date object using the format() method.Exampleimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {       long msec = 3550154;       //Instantiating the SimpleDateFormat class       SimpleDateFormat dateformatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");       //Parsing the given String to Date object       Date date = new Date(msec);       System.out.println("Date value: "+dateformatter.format(date));    } }OutputDate value: 01 Jan 1970 06:29:10:154 +0530Exampleimport java.util.Calendar; import java.util.Date; public class Sample {    public static void main(String args[]) {       Calendar calendar = Calendar.getInstance();       long msec = calendar.getTimeInMillis();       Date obj = new Date(msec); ...

Read More

What happens if we try to extend a final class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jul-2021 2K+ Views

In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is finale it cannot be extended.Extending a final classWhen we try to extend a final class that will lead to a compilation error saying “cannot inherit from final SuperClass”ExampleIn the following Java program, we have a final class with name SuperClass and we are trying to inherent it from another class (SubClass).final class SuperClass{    public void display() {       ...

Read More

final, finally and finalize in Java

Fendadis John
Fendadis John
Updated on 29-Jul-2021 12K+ Views

The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred. Using a finally block allows you to run any cleanup-type statements that you just wish to execute, despite what happens within the protected code.The finalize() method is used just before object is destroyed and can be called just prior to object ...

Read More

final, finally and finalize in Java

Fendadis John
Fendadis John
Updated on 29-Jul-2021 12K+ Views

The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred. Using a finally block allows you to run any cleanup-type statements that you just wish to execute, despite what happens within the protected code.The finalize() method is used just before object is destroyed and can be called just prior to object ...

Read More

Why can't we use the "super" keyword is in a static method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 28-Jul-2021 4K+ Views

A static method or, block belongs to the class and these will be loaded into the memory along with the class. You can invoke static methods without creating an object. (using the class name as reference).Where the "super" keyword in Java is used as a reference to the object of the superclass. This implies that to use "super" the method should be invoked by an object, which static methods are not.Therefore, you cannot use the "super" keyword from a static method.ExampleIn the following Java program, the class "SubClass" contains a private variable name with setter and getter methods and an ...

Read More

Difference Between Checked and Unchecked Exception in Java

AmitDiwan
AmitDiwan
Updated on 24-Mar-2021 14K+ Views

In this post, we will understand the difference between checked and unchecked exceptions in Java.Checked ExceptionsThey occur at compile time.The compiler checks for a checked exception.These exceptions can be handled at the compilation time.It is a sub-class of the exception class.The JVM requires that the exception be caught and handled.Example of Checked exception- ‘File Not Found Exception’Unchecked ExceptionsThese exceptions occur at runtime.The compiler doesn’t check for these kinds of exceptions.These kinds of exceptions can’t be caught or handled during compilation time.This is because the exceptions are generated due to the mistakes in the program.These are not a part of the ...

Read More

Difference Between Packages and Interfaces in Java

AmitDiwan
AmitDiwan
Updated on 24-Mar-2021 5K+ Views

In this post, we will understand the difference between packages and interfaces in Java.PackagesIt is a group of classes and/or interfaces that are together.It can be created using the "Package" keyword.It can be imported.It can be done using the "import" keyword.Examplepackage package_name; public class class_name {    .    (body of class)    . }InterfacesIt is a group of abstract methods and constants.It can be created using the "Interface" keyword.It can be extended by another interface.It can also be implemented by a class.It can be implemented using the ‘implement’ keyword.Exampleinterface interface_name { variable declaration; ...

Read More
Showing 2741–2750 of 4,498 articles
« Prev 1 273 274 275 276 277 450 Next »
Advertisements