Difference Between throw and throws Keywords in Java

Sharon Christine
Updated on 20-Feb-2020 06:39:20

424 Views

The throw keyword is used to raise an exception explicitly.Examplepublic class Test {    public static void main(String[] args) {       throw new NullPointerException();    } }Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)The throws keywords in Java used to postpone the handling of a checked exception.public class Test {    public static void main(String[] args)throws NullPointerException {       throw new NullPointerException();    } }

Define a Class Inside a Java Interface

V Jyothi
Updated on 20-Feb-2020 05:46:54

7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library {    void issueBook(Book b);    void retrieveBook(Book b);    public class Book {       int bookId;       String bookName;       int issueDate;       int returnDate;    } } public class Sample implements Library {    public void issueBook(Book b) {       System.out.println("Book Issued");    }    public void retrieveBook(Book b) { ... Read More

Write Python Regular Expression to Match Multiple Words Anywhere

Rajendra Dharmkar
Updated on 20-Feb-2020 05:31:13

3K+ Views

The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']

Eclipse Keyboard Shortcut for public static void main in Java

Ramu Prasad
Updated on 20-Feb-2020 05:29:13

9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Put jQuery Code in an External JS File

Ali
Ali
Updated on 20-Feb-2020 05:23:20

7K+ Views

Create an external JavaScript file and add the jQuery code in it.ExampleLet’s say the name of the external file is demo.js. To add it in the HTML page, include it like the following −                               Hello     Above we added jQuery using Google CDN and the external file was included after that.Add jQuery code in demo.js, since you wanted to place jQuery code −$(document).ready(function(){    alert(“amit”) });

Find Package Explorer in Java Eclipse Project

V Jyothi
Updated on 20-Feb-2020 05:19:59

24K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

Search for a Pattern in a Java String

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

3K+ 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

Count the Number of Lines in a Text File Using Java

Arushi
Updated on 20-Feb-2020 05:15:24

2K+ Views

To count the number of lines in a fileInstantiate the FileInputStream class by passing an object of the required file as parameter to its constructor.Read the contents of the file to a bytearray using the read() method of FileInputStream class.Instantiate a String class by passing the byte array obtained, as a parameter its constructor.Now, split the above string into an array of strings using the split() method by passing the regular expression of the new line as a parameter to this method.Now, find the length of the obtained array.Exampleimport java.io.File; import java.io.FileInputStream; public class NumberOfCharacters {    public static void main(String args[]) throws Exception{ ... Read More

Count the Number of Words in a Text File Using Java

Paul Richard
Updated on 20-Feb-2020 05:11:12

6K+ Views

Read the number of words in text fileCreate a FileInputStream object by passing the required file (object) as a parameter to its constructor.Read the contents of the file using the read() method into a byte array. Insatiate a String class by passing the byte array to its constructor.Using split() method read the words of the String to an array.Create an integer variable, initialize it with 0, int the for loop for each element of the string array increment the count.Exampleimport java.io.File; import java.io.FileInputStream; public class Sample {    public static void main(String args[]) throws Exception{       int count =0;       File ... Read More

Classpath of Projects in Java Eclipse

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

4K+ 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.

Advertisements