Found 9321 Articles for Object Oriented Programming

What is the difference between checked and unchecked exceptions in Java?

karthikeya Boyini
Updated on 25-Feb-2020 10:45:28

724 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.ExampleIf you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception.import java.io.File; import java.io.FileReader; public class FilenotFound_Demo {    public static void main(String args[]) {       File file = new File("E://file.txt");   ... Read More

What are unchecked exceptions in Java?

Sharon Christine
Updated on 16-Jun-2020 12:45:47

2K+ Views

An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.ExampleLive Demopublic class Unchecked_Demo {    public static void main(String args[]) {       int num[] = {1, 2, 3, 4};       System.out.println(num[5]);    } }OutputException in thread "main" java.lang.ArrayIndexOutOfBoundsException: ... Read More

What are checked exceptions in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

750 Views

A checked exception is an exception that occurs at the time of compilation, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions. if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception. Example import java.io.File; import java.io.FileReader; public class FilenotFound_Demo { public static void main(String args[]) { ... Read More

Is it necessary that a try block should be followed by a catch block in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20

477 Views

Not necessarily catch, a try must be followed by either catch or finally block. Example import java.io.File; public class Test{ public static void main(String args[]){ System.out.println("Hello"); try{ File file = new File("data"); } } } Output C:\Sample>Javac Test.java Test.java:5: error: 'try' without 'catch', 'finally' or resource declarations try{ ^ 1 error

What is the difference between compile time errors and run time errors in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

295 Views

Compile time errors are syntactical errors in the code which hinders it from being compiled. Example public class Test{ public static void main(String args[]){ System.out.println("Hello") } } Output C:\Sample>Javac Test.java Test.java:3: error: ';' expected System.out.println("Hello") An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. Example ... Read More

What are classpath of projects in Java eclipse projects?

Nikitha N
Updated on 20-Feb-2020 05:09:58

3K+ Views

You can include Jar files to which you need to set the classpath in the eclipse project using build pathStep 1 − Right click on the project Select Build Path → Configure Build Path.Step 2 − Select libraries select Add External JARs… button.Step3 − Then browse through the folder where the required jar files exits, select them and press open.Selected jar files will be added to the Libraries. Finally, press OK.Now, if you open the Referenced libraries in the project you can observe the added jar file.

How to build an ant file for a Java Eclipse project?

Srinivas Gorla
Updated on 25-Feb-2020 12:30:47

5K+ Views

Follow the steps given below, to integrate Ant into Eclipse.Make sure that the build.xml is a part of your java project, and does not reside at a location that is external to the project.Enable Ant View by following Window > Show View > Other > Ant > Ant.Open Project Explorer, drag the build.xml into the Ant View.Your Ant view looks similar to –Clicking on the targets, build / clean / usage will run Ant with the target. Clicking "fax" will execute the default target - usage.The Ant Eclipse plugin also comes with a good editor for editing build.xml files. The ... Read More

What is an Ant build in Java?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:20

537 Views

ANT stands for Another Neat Tool. It is a Java-based build tool from Apache.Ant is used to simplify the mundane tasks such as compiling the code, packaging the binaries, deploying the binaries to the test server, testing the changes, copying the code from one location to another etc. It is an Operating System build and deployment tool that can be executed from the command line.

How to search for a pattern in a Java string?

Rishi Raj
Updated on 20-Feb-2020 05:16:07

2K+ Views

Java provides the java.util.regex package for pattern matching with regular expressions. You can then search for a pattern in a Java string using classes and methods of this packages.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches {    public static void main( String args[] ) {       String line = "This order was placed for QT3000! OK?";       String pattern = "(.*)(\d+)(.*)";       Pattern r = Pattern.compile(pattern);       Matcher m = r.matcher(line);             if (m.find( )) {          System.out.println("Found value: " + m.group(0));     ... Read More

How to count the number of characters (including spaces) in a text file using Java?

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

557 Views

To count the number of lines in a file Instantiate the FileInputStream class by passing an object of the required file as parameter to its constructor. Read the contents of the file to a byte array using the read() method of FileInputStream class. Instantiate a String class by passing the byte array obtained, as a parameter its constructor. Finally, find the length of the string. Example import java.io.File; import java.io.FileInputStream; public class NumberOfCharacters { public static void main(String args[]) throws Exception{ File file = new File("data"); FileInputStream ... Read More

Advertisements