![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
4K+ Views
Generate OTP is now a requirement on most of the website now-a-days. In case of additional authentication, system generates a OTP password adhering to OTP policy of the company. Following example generates a unique OTP adhering to following conditions −It should contain at least one number.Length should be 4 characters.Exampleimport java.util.Random; public class Tester { public static void main(String[] args) { System.out.println(generateOTP(4)); } private static char[] generateOTP(int length) { String numbers = "1234567890"; Random random = new Random(); char[] otp = new ... Read More
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
2K+ Views
String provides format() method can be used to print formatted output in java.System.out.printf() method can be used to print formatted output in java.Following example returns a formatted string value by using a specific locale, format and arguments in format() methodExampleimport java.util.*; public class StringFormat { public static void main(String[] args) { double e = Math.E; System.out.format("%f%n", e); System.out.format(Locale.GERMANY, "%-10.4f%n%n", e); } }OutputThe above code sample will produce the following result.2.718282 2, 7183The following is an another sample example of format strings.Examplepublic class HelloWorld { public static ... Read More
![Vikyath Ram](https://www.tutorialspoint.com/assets/profiles/13780/profile/60_74413-1512648006.jpg)
7K+ Views
A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −Syntaxtry { // Protected code } catch (ExceptionName e1) { // Catch block }The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by ... Read More
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
391 Views
Following programs shows the float arithmetic can cause dubious result if integer values are used using float variables.Examplepublic class Tester { public static void main(String[] args) { float a = 500000000; float b = -500000000; float c = 1; float sumabc1 = a+(b+c); float sumabc2 =(a+b)+c; System.out.println("Floating Point Arithmetic"); System.out.println("a + ( b + c ) : " + sumabc1); System.out.println("(a + b) + c : " + sumabc2); ... Read More
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
107 Views
The java.lang.Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.Class DeclarationFollowing is the declaration for java.lang.Object class −public class ObjectClass constructorsSr.No.Constructor & Description1Object()This is the Single Constructor.Class methodsSr.No.Method & Description1protected Object clone()This method creates and returns a copy of this object.2boolean equals(Object obj)This method indicates whether some other object is "equal to" this one.3protected void finalize()This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.4Class getClass()This method returns the ... Read More
![Arushi](https://www.tutorialspoint.com/assets/profiles/13510/profile/60_55588-1512537391.jpg)
378 Views
This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Exampleimport java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5}; int min = (int) Collections.min(Arrays.asList(numbers)); int max = (int) Collections.max(Arrays.asList(numbers)); System.out.println("Min number: " + min); System.out.println("Max number: " + max); } }ResultThe above code sample will produce the following result.Min number: 1 Max ... Read More
![Vikyath Ram](https://www.tutorialspoint.com/assets/profiles/13780/profile/60_74413-1512648006.jpg)
650 Views
java.io.File class provides following useful methods to figure out the free disk space available.Sr.No.Method & Description1public long getFreeSpace()Returns the number of unallocated bytes in the partition named by this abstract path name.2public long getTotalSpace()Returns the size of the partition named by this abstract pathname.3public long getUsableSpace()Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname.Following example showcases the use of above methods.Example Finalimport java.io.File; import java.text.NumberFormat; public class Tester { public static void main(String[] args) { NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); ... Read More
![Vikyath Ram](https://www.tutorialspoint.com/assets/profiles/13780/profile/60_74413-1512648006.jpg)
5K+ Views
Final Static VariablesClass variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.There would only be one copy of each class variable per class, regardless of how many objects are created from it.Static variables are normally declared as constants using the final keyword. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.Static variables are stored in the static memory, mostly declared as final and used as either public or private constants.Static variables are created when the program ... Read More
![Vikyath Ram](https://www.tutorialspoint.com/assets/profiles/13780/profile/60_74413-1512648006.jpg)
2K+ Views
Local VariableLocal variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, or block.Local variables are implemented at stack level internally.There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.final Local Variablefinal is the only allowed access modifier for local variables.final local variable is not required ... Read More
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
11K+ 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