Found 4338 Articles for Java 8

What are the different ways to print an exception message in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:27:07

8K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Printing the Exception messageYou can print the exception message in Java using one of the following methods which are inherited from Throwable class.printStackTrace() − This method prints the backtrace to the standard error stream.getMessage() − This method returns the detail message string of the current throwable object.toString() − This message prints the short description of the current throwable object.Example Live Demoimport java.util.Scanner;    public class ... Read More

Can we change return type of main() method in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:05:29

3K+ Views

The public static void main() method is the entry point of the Java program. Whenever you execute a program in Java, the JVM searches for the main method and starts executing from it.You can write the main method in your program with return type other than void, the program gets compiled without compilation errors. But, at the time of execution JVM does not consider this new method (with return type other than void) as the entry point of the program.It searches for the main method which is public, static, with return type void, and a String array as an argument.public static int ... Read More

Does main() method accept arguments other than string array, in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:08:04

4K+ Views

The public static void main() method accepts an array of values of the type String Java from the user.public class{    public static void main(String[] args){    } }You can pass them at the time of execution right after the class name separated with spaces as −java ClassName 10 20 30And, in the program (from the main method) you can extract these values from the String array and use.ExampleFor example, you can use command line arguments to pass a and b in the above program as −public class Sample {    public static void main(String[] args){       int ... Read More

Can we call Superclass’s static method from subclass in Java?

Maruthi Krishna
Updated on 29-Jun-2020 12:37:42

2K+ Views

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.Example Live Demopublic class Sample{    public static void display(){       System.out.println("This is the static method........");    }    public static void main(String args[]){       Sample.display();    } }OutputThis is the static method........It also works, if you call a static method using an instance. But, it is not recommended. Live Demopublic class Sample{    public static void display(){       System.out.println("This is the static ... Read More

Can we call methods of the superclass from a static method in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:38:24

3K+ Views

Inheritance can be defined as the process where one (parent/super) class acquires the properties (methods and fields) of another (child/sub). With the use of inheritance, the information is made manageable in a hierarchical order. The class which inherits the properties is known as a subclass and the class whose properties are inherited is called superclass.  In short, in inheritance, you can access the members (variables and methods) of the superclass using the subclass object.Example Live Democlass SuperClass {    public void display(){       System.out.println("Hello this is the method of the superclass");    } } public class SubClass extends SuperClass ... Read More

What happens if we call "super()" in a constructor without extending any class, in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:41:12

2K+ Views

A super keyword is a reference of the superclass object in Java. Using this, you can invoke the instance methods constructors and, variables, of a superclass.Calling the constructor of a superclassYou can call the constructor of a superclass from the subclass’s constructor.ExampleConsider the following example, it has two classes SuperClass class and SubClass where the SubClass extends the SuperClass. The superclass has a parameterized constructor, we are invoking it from the subclass using the "super" keyword. Live Democlass SuperClass{    SuperClass(int data){       System.out.println("Superclass's constructor: "+data);    } } public class SubClass extends SuperClass{    SubClass(int data) {       ... Read More

Foreach in C++ and Java

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

125 Views

In C++ and Java, there are another type of loop called foreach loop. This is not present in C. This loop has introduced in C++11 and Java JDK 1.5.0. The advantage of this loop is that, it can access the elements very quickly without performing initialization, testing and increment/decrement. This loop is used to access every element in one array or some containers. This loop is known as foreach but to denote this loop we have to use ‘for’ keyword. The syntax is different than normal for and foreach.for(datatype item : Array) { }Let us see some examples of foreach ... Read More

Comparison of Exception Handling in C++ and Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

175 Views

The exception handling feature is present almost any object oriented languages nowadays. In C++ and Java also we can get this kind of feature. There are some similarities between exception handling in C++ and exception handling in Java, like in both languages we have to use the try-catch block. Though there are some difficulties also. These are like below −In C++, we can throw any type of data as exception. Any type of data means primitive datatypes and pointers also. In Java we can only throw the throwable objects. Subclasses of any throwable class will also be throwable.Example Live Demo#include ... Read More

How can I append text to JTextArea in Java?

George John
Updated on 30-Jul-2019 22:30:26

2K+ Views

To append text to JTextArea, use the component’s append() method. Let’sa say the following is our text in the JTextArea -JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "This demonstrates the usage of JTextArea in Java. ");Now, append text to the same textArea -textArea.append("In this example we have deleted some text from the beginning."+ "We have also appended some text.");The following is an example to append text to JTextArea -Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       JTextArea textArea ... Read More

How can I set scrollbars to never appear in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

57 Views

To set scrollbars to never appear, use the JScrollPane.HORIZONTAL_SCROLLBAR_NEVER and JScrollPane.VERTICAL_SCROLLBAR_NEVER. Let’s say you created a Box with some button components. Now, create a JScrollPane:JScrollPane scrollPane = new JScrollPane();Set the Viewport view as Box:scrollPane.setViewportView(box);Now, set the scrollbars to never appear:scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);The following is an example to set scrollbars to never appear:Examplepackage my; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("One");       JButton button2 ... Read More

Advertisements