- 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
667 Views
You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo { public static void main(String args[]) { try { System.out.println(new Date( ) + ""); Thread.sleep(5*60*10); System.out.println(new Date( ) + ""); } catch (Exception e) { System.out.println("Got an exception!"); } } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009
251 Views
With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void ... Read More
842 Views
Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester { public static void main(String args[]) { static int a = 10; } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression static int a = 10;
714 Views
In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester { final int A; //Scenario 1: assignment at time of declaration final int B = 2; public Tester() { //Scenario 2: assignment in constructor A = 1; } public void display() { System.out.println(A + ", " + B); } public static void main(String[] args) { Tester tester = new Tester(); ... Read More
3K+ Views
If a class has multiple functions by the same name but different parameters, it is known as Method Overloading. If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its superclass parameter must be different in case of overloading, the parameter must be same in case of overriding. Now considering the case of static methods, then static methods have following rules in terms of overloading and ... Read More
192 Views
In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.ExampleLive Demopublic class Tester { ... Read More
237 Views
This example shows how to copy the contents of one file into another file using read & write methods of FileStreams classes.ExampleLive Demoimport java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile")); out1.write("string to be copied"); out1.close(); InputStream in = new FileInputStream(new File("srcfile")); OutputStream out = new FileOutputStream(new File("destnfile")); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { ... Read More
2K+ Views
We can convert a stream to set using the following ways.Using stream.collect() with Collectors.toSet() method - Stream collect() method iterates its elements and stores them in a collection.collect(Collector.toSet()) method.Using set.add() method - Iterate stream using forEach and then add each element to the set.ExampleLive Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; public class Tester { public static void main(String[] args) { Stream stream = Stream.of("a", "b", "c", "d"); // Method 1 Set set = stream.collect(Collectors.toSet()); set.forEach(data -> System.out.print(data + " ")); ... Read More
596 Views
Being a type of Collection, we can convert a set to Stream using its stream() method.ExampleLive Demoimport java.util.HashSet; import java.util.Set; import java.util.stream.Stream; public class Tester { public static void main(String args[]) { Set set = new HashSet(); set.add("a"); set.add("b"); set.add("c"); set.add("d"); set.add("e"); set.add("f"); Stream stream = set.stream(); stream.forEach(data->System.out.print(data+" ")); } }Outputa b c d e f
49K+ Views
We can convert an array to arraylist using following ways.Using Arrays.asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.Collections.addAll() method - Create a new list before using this method and then add array elements using this method to existing list.Iteration method - Create a new list. Iterate the array and add each element to the list.Example import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Tester { public static void main(String args[]) { String[] array ... Read More