Programming Articles - Page 2458 of 3366

Print matrix in snake pattern in C Programming.

Sunidhi Bansal
Updated on 04-Sep-2019 06:08:53

4K+ Views

Given an array of nxn size, the program must print the elements of an array in a snake pattern without doing any changes to their original locationsExampleInput: arr[]= 100 99 98 97    93 94 95 96    92 91 90 89    85 86 87 88 Output: 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85The program will traverse each row of a matrix and check for even or odd rows.If the row is even it will print the elements of that row from left to rightIf the row is odd it ... Read More

Which collection classes are thread-safe in Java?

raja
Updated on 29-Nov-2023 10:31:28

14K+ Views

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc. Stack The Stack class in Java implements the stack data structure that is based on the principle of LIFO. So, the Stack class can support many operations such as push, pop, peek, search, empty, etc. Example import java.util.*; public class StackTest { public static void main (String[] args) { Stack stack = new Stack(); ... Read More

When can a .class file get created in Java?

raja
Updated on 03-Jul-2020 08:24:35

2K+ Views

A Java class file has a ".class" extension and contains the Java bytecode. This class file can be executed by the Java Virtual Machine (JVM). A  ".class" file is created as a result of successful compilation by the Java compiler from the ".java" file. Each class in the .java file is compiled into a separate class file if the ".java " file has more than one class.Exampleclass A {    A() {       System.out.println("This is class A");    } } class B {    B() {       System.out.println("This is class B");    } } class C { ... Read More

How can we check an underflow occurs in Java?

raja
Updated on 03-Jul-2020 08:19:34

405 Views

When a value is assigned to a variable that is less than the minimum allowed value for that variable, then an underflow occurs. There is no exception thrown by the JVM if an underflow occurs in Java and it is the responsibility of a programmer to handle the underflow conditions.Examplepublic class UnderlowTest {    public static void main(String[] args) {       int num1 = -2147483648;       int num2 = -1;       System.out.println("Number 1: " + num1);       System.out.println("Number 2: " + num2);       long sum = (long)num1 + (long)num2;     ... Read More

How can we filter a JTable in Java?

raja
Updated on 12-Feb-2020 07:54:44

5K+ Views

A JTable provides a very flexible possibility to create and display tables. The TableModel interface defines methods for objects that specify the contents of a table. The AbstractTableModel class is typically extended to provide a custom implementation of a model table. A JTable class provides the ability to edit tables using the method setCellEditor() allows an object of the TableCellEditor interface.We can filter a table using the setRowFilter() method of TableRowSorter class.Exampleimport java.awt.*; import java.awt.event.*; import java.util.regex.*; import javax.swing.*; import javax.swing.table.*; public class FilterTableTest extends JFrame {    private JTable table;    private TableModel model;    public FilterTableTest() {       setTitle("FilterTable Test");       ... Read More

Can a "this" keyword be used to refer to static members in Java?

raja
Updated on 29-Nov-2023 10:58:08

928 Views

No, the "this" keyword cannot be used to refer to the static members of a class. This is because the “this” keyword points to the current object of the class and the static member does not need any object to be called. The static member of a class can be accessed directly without creating an object in Java. Example public class StaticTest { static int a = 50; static int b; static void show() { System.out.println("Inside the show() method"); b = a + 5; ... Read More

How to compare two dates in Java?

Vivek Verma
Updated on 20-Jun-2025 17:05:55

82K+ Views

In Java, we can compare two dates using the compareTo() method of the Comparable interface. This is a common way to compare two dates, and this method returns an integer value based on which you can specify which date occurs before, after, or if it is equal. Here are the return values by this method, based on which you verify the date appearance: ... Read More

How do threads communicate with each other in Java?

raja
Updated on 29-Nov-2023 11:11:33

6K+ Views

Inter-thread communication involves the communication of threads with each other. The three methods that are used to implement inter-thread communication in Java. wait() This method causes the current thread to release the lock. This is done until a specific amount of time has passed or another thread calls the notify() or notifyAll() method for this object. notify() This method wakes a single thread out of multiple threads on the current object’s monitor. The choice of thread is arbitrary. notifyAll() This method wakes up all the threads that are on the current object’s monitor. Example class BankClient { int balAmount = 5000; synchronized void withdrawMoney(int ... Read More

How does a ClassLoader work in Java?

raja
Updated on 03-Jul-2020 07:39:59

951 Views

A Java Class is stored in the form of byte code in a .class file after it is compiled. The ClassLoader loads the class of the Java program into memory when it is required. The ClassLoader is hierarchical and so if there is a request to load a class, it is delegated to the parent class loader.The types of ClassLoader in Java are given as followsBootstrap ClassLoaderExtensions ClassLoaderSystem ClassLoaderExamplepublic class ClassLoaderTest {    public static void main(String[] args) {       System.out.println("class loader for this class: " + ClassLoaderTest.class.getClassLoader());       System.out.println("class loader for DNSNameService: " + sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());     ... Read More

Importance of the getCause() method in Java?

raja
Updated on 03-Jul-2020 07:40:44

2K+ Views

The getCause() method is from Throwable class and we can use this method which returns the cause of the exception or returns null if the cause of the exception is not known. The getCause() method doesn’t accept any arguments and doesn’t throw an exception. It returns the cause that was provided by one of its constructors or that was determined by the formation of the initCause() method of Throwable class.Syntaxpublic Throwable getCause()Examplepublic class GetCauseMethodTest {    public static void main(String[] args) throws Exception {       try {          myException();       } catch(Exception e) {          System.out.println("Cause ... Read More

Advertisements