Question stringlengths 1 113 | Answer stringlengths 22 6.98k |
|---|---|
Can you provide an example of Java String getChars() Method? | FileName: StringGetCharsExample.java
public class StringGetCharsExample{
public static void main(String args[]){
String str = new String("hello javatpoint how r u");
char[] ch = new char[10];
try{
str.getChars(6, 16, ch, 0);
System.out.println(ch);
}catch(Exc... |
What is Java String indexOf()? | The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.
Signature
There are four overloaded indexOf() method in Java. The signature of indexOf() methods are given below:
1) int indexOf(int ch) - It returns the index position for ... |
Can you provide an example of Java String indexOf() Method? | FileName: IndexOfExample.java
public class IndexOfExample{
public static void main(String args[]){
String s1="this is index of example";
//passing substring
int index1=s1.indexOf("is");//returns the index of is substring
int index2=s1.indexOf("index");//returns the index of index substring
System.out.print... |
Can you provide an example of Java String indexOf(String substring) Method? | The method takes substring as an argument and returns the index of the first character of the substring.
FileName: IndexOfExample2.java
public class IndexOfExample2 {
public static void main(String[] args) {
String s1 = "This is indexOf method";
// Passing Substring
... |
Can you provide an example of Java String indexOf(String substring, int fromIndex) Method? | The method takes substring and index as arguments and returns the index of the first character that occurs after the given fromIndex.
FileName: IndexOfExample3.java
public class IndexOfExample3 {
public static void main(String[] args) {
String s1 = "This is indexOf method";
/... |
Can you provide an example of Java String indexOf(int char, int fromIndex) Method? | The method takes char and index as arguments and returns the index of the first character that occurs after the given fromIndex.
FileName: IndexOfExample4.java
public class IndexOfExample4 {
public static void main(String[] args) {
String s1 = "This is indexOf method";
//... |
What is Java String intern()? | The Java String class intern() method returns the interned string. It returns the canonical representation of string.
It can be used to return string from memory if it is created by a new keyword. It creates an exact copy of the heap string object in the String Constant Pool.
Signature
The signature of the intern() ... |
What is The need and working of the String.intern() Method? | When a string is created in Java, it occupies memory in the heap. Also, we know that the String class is immutable. Therefore, whenever we create a string using the new keyword, new memory is allocated in the heap for corresponding string, which is irrespective of the content of the array. Consider the following code s... |
Could you provide an example of Java String intern() Method Example? | FileName: InternExample.java
public class InternExample{
public static void main(String args[]){
String s1=new String("hello");
String s2="hello";
String s3=s1.intern();//returns string from pool, now it will be same as s2
System.out.println(s1==s2);//false because reference variables are pointing to ... |
What are the Points to Remember at intern() method? | Following are some important points to remember regarding the intern() method:
1) A string literal always invokes the intern() method, whether one mention the intern() method along with the string literal or not. For example,
String s = "d".intern();
String p = "d"; // compiler treats it as String p = "d".intern... |
What is Java String isEmpty()? | The Java String class isEmpty() method checks if the input string is empty or not. Note that here empty means the number of characters contained in a string is zero.
Signature
The signature or syntax of string isEmpty() method is given below:
public boolean isEmpty()
Returns
true if length is 0 otherwise false... |
Could you provide an example of Java String isEmpty() method example? | FileName: StringIsEmptyExample.java
public class IsEmptyExample{
public static void main(String args[]){
String s1="";
String s2="javatpoint";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}}
Output:
true
false
|
Could you provide an examples of Empty Vs. Null Strings? | Earlier in this tutorial, we have discussed that the empty strings contain zero characters. However, the same is true for a null string too. A null string is a string that has no value.
String str = ""; // empty string
String str1 = null; // null string. It is also not containing any characters.
The isEmpty(... |
Could you provide an example of Blank Strings? | Blank strings are those strings that contain only white spaces. The isEmpty() method comes in very handy to check for the blank strings. Consider the following example.
FileName: StringIsEmptyExample5.java
public class StringIsEmptyExample5
{
// main method
public static void main(String argvs[])
{
... |
What is Java String join()? | The Java String class join() method returns a string joined with a given delimiter. In the String join() method, the delimiter is copied for each element. The join() method is included in the Java string since JDK 1.8.
There are two types of join() methods in the Java String class.
Signature
The signature or syntax ... |
Could you provide Java String join() Method Example? | FileName: StringJoinExample.java
public class StringJoinExample{
public static void main(String args[]){
String joinString1=String.join("-","welcome","to","javatpoint");
System.out.println(joinString1);
}}
Output:
welcome-to-javatpoint
|
What is Java String lastIndexOf()? | The Java String class lastIndexOf() method returns the last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero.
Signature
There are four types of lastIndexOf() method in Java. The signature of the methods are given below:
1) int lastIndexOf(int ch) ... |
Can you provide an example of Java String lastIndexOf() method? | FileName: LastIndexOfExample.java
public class LastIndexOfExample{
public static void main(String args[]){
String s1="this is index of example";//there are 2 's' characters in this sentence
int index1=s1.lastIndexOf('s');//returns last index of 's' char value
System.out.println(index1);//6
}}
Out... |
Can provide an example of Java String lastIndexOf(int ch, int fromIndex) Method? | Here, we are finding the last index from the string by specifying fromIndex.
FileName: LastIndexOfExample2.java
public class LastIndexOfExample2 {
public static void main(String[] args) {
String str = "This is index of example";
int index = str.lastIndexOf('s',5);
System.out.... |
Could you provide an example of Java String lastIndexOf(String substring) Method? | It returns the last index of the substring.
FileName: LastIndexOfExample3.java
public class LastIndexOfExample3 {
public static void main(String[] args) {
String str = "This is last index of example";
int index = str.lastIndexOf("of");
System.out.println(index); ... |
Can you provide an example of Java String lastIndexOf(String substring, int fromIndex) Method? | It returns the last index of the substring from the fromIndex.
FileName: LastIndexOfExample4.java
public class LastIndexOfExample4 {
public static void main(String[] args) {
String str = "This is last index of example";
int index = str.lastIndexOf("of", 25);
System.o... |
What is Java String length()? | The Java String class length() method finds the length of a string. The length of the Java string is the same as the Unicode code units of the string.
Signature
The signature of the string length() method is given below:
public int length()
Specified by
CharSequence interface
Returns
Length of characters. In... |
Could you provide Java String length() method example? | FileName: LengthExample.java
public class LengthExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string
System.out.println("string length is: "+s2.length());//6 is the leng... |
What is Java String replace()? | The Java String class replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.
Since JDK 1.5, a new replace() method is introduced that allows us to replace a sequence of char values.
Signature
There are two types of replace() methods in Java String class.
public... |
Can you provide an example of Java String replace(char old, char new) method? | FileName: ReplaceExample1.java
public class ReplaceExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'
System.out.println(replaceString);
}}
Output:
jevetpoint is e very... |
Could you provide Java String replace(CharSequence target, CharSequence replacement) method example? | FileName: ReplaceExample2.java
public class ReplaceExample2{
public static void main(String args[]){
String s1="my name is khan my name is java";
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"
System.out.println(replaceString);
}}
Output:
my name was khan... |
What is Java String replaceAll()? | The Java String class replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string.
Signature
public String replaceAll(String regex, String replacement)
Parameters
regex : regular expression
replacement : replacement sequence of characters
Returns
replac... |
Could you provide Java String replaceAll() example: replace character? | Let's see an example to replace all the occurrences of a single character.
FileName: ReplaceAllExample1.java
public class ReplaceAllExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replaceAll("a","e");//replaces all occurrences of "a... |
Could you provide Java String replaceAll() example: replace word? | Let's see an example to replace all the occurrences of a single word or set of words.
FileName: ReplaceAllExample2.java
public class ReplaceAllExample2{
public static void main(String args[]){
String s1="My name is Khan. My name is Bob. My name is Sonoo.";
String replaceString=s1.replaceAll("is","was");//r... |
Java String replaceAll() example: remove white spaces? | Let's see an example to remove all the occurrences of white spaces.
FileName: ReplaceAllExample3.java
public class ReplaceAllExample3{
public static void main(String args[]){
String s1="My name is Khan. My name is Bob. My name is Sonoo.";
String replaceString=s1.replaceAll("\\s","");
System.out.println(... |
What is Java String split()? | The java string split() method splits this string against given regular expression and returns a char array.
|
Could you provide Java String split() method example? | The given example returns total number of words in a string excluding space only. It also includes special characters.
public class SplitExample{
public static void main(String args[]){
String s1="java string split method by javatpoint";
String[] words=s1.split("\\s");//splits the string based on whitespace... |
Could you provide Java String split() method with regex and length example? | public class SplitExample2{
public static void main(String args[]){
String s1="welcome to split world";
System.out.println("returning words:");
for(String w:s1.split("\\s",0)){
System.out.println(w);
}
System.out.println("returning words:");
for(String w:s1.split("\\s",1)){
System.out.println... |
What is Java String startsWith()? | The Java String class startsWith() method checks if this string starts with the given prefix. It returns true if this string starts with the given prefix; else returns false. |
Could you provide Java String startsWith() method example? | The startsWith() method considers the case-sensitivity of characters. Consider the following example.
FileName: StartsWithExample.java
public class StartsWithExample
{
// main method
public static void main(String args[])
{
// input string
String s1="java string split method by javatpoint"; ... |
Can you provide an example of Java String startsWith(String prefix, int offset) Method? | It is an overloaded method of the startWith() method that is used to pass an extra argument (offset) to the function. The method works from the passed offset. Let's see an example.
FileName: StartsWithExample2.java
public class StartsWithExample2 {
public static void main(String[] args) {
Stri... |
What is Java String substring()? | The Java String class substring() method returns a part of the string.
We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive. In other words, the beginIndex starts from 0, whereas the endIndex starts from 1.
There are two types of sub... |
Can you provide an example of Java String substring() method? | FileName: SubstringExample.java
public class SubstringExample{
public static void main(String args[]){
String s1="javatpoint";
System.out.println(s1.substring(2,4));//returns va
System.out.println(s1.substring(2));//returns vatpoint
}}
Output:
va
vatpoint
|
Can you provide an example of Applications of substring() Method | 1) The substring() method can be used to do some prefix or suffix extraction. For example, we can have a list of names, and it is required to filter out names with surname as "singh". The following program shows the same.
FileName: SubstringExample3.java
public class SubstringExample3
{
// main method
pub... |
What is Java String toCharArray()? | The java string toCharArray() method converts this string into character array. It returns a newly created character array, its length is similar to this string and its contents are initialized with the characters of this string.
|
Can you provide an example of Java String toCharArray() method? | public class StringToCharArrayExample{
public static void main(String args[]){
String s1="hello";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
}
}}
Output:
hello
|
What is Java String toLowerCase()? | The java string toLowerCase() method returns the string in lowercase letter. In other words, it converts all characters of the string into lower case letter.
The toLowerCase() method works same as toLowerCase(Locale.getDefault()) method. It internally uses the default locale.
|
Can you provide an example of Java String toLowerCase() method? | public class StringLowerExample{
public static void main(String args[]){
String s1="JAVATPOINT HELLO stRIng";
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}}
Output:
javatpoint hello string
|
Could you provide Java String toLowerCase(Locale locale) Method Example? | This method allows us to pass locale too for the various langauges. Let's see an example below where we are getting string in english and turkish both.
import java.util.Locale;
public class StringLowerExample2 {
public static void main(String[] args) {
String s = "JAVATPOINT HELLO stRIng";
... |
What is Java String toUpperCase()? | The java string toUpperCase() method returns the string in uppercase letter. In other words, it converts all characters of the string into upper case letter.
The toUpperCase() method works same as toUpperCase(Locale.getDefault()) method. It internally uses the default locale.
|
Can you provide an example of Java String toUpperCase() method? | public class StringUpperExample{
public static void main(String args[]){
String s1="hello string";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}}
Output:
HELLO STRING
|
What is Java String trim()? | The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string.
|
Can you provide an example of Java String trim() Method? | FileName: StringTrimExample.java
public class StringTrimExample{
public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"javatpoint");//without trim()
System.out.println(s1.trim()+"javatpoint");//with trim()
}}
Output
hello string javatpoint
hello stringj... |
What is Java String valueOf()? | The java string valueOf() method converts different types of values into string. By the help of string valueOf() method, you can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string and char array to string.
|
Could you provide an example of Java String valueOf() method? | public class StringValueOfExample{
public static void main(String args[]){
int value=30;
String s1=String.valueOf(value);
System.out.println(s1+10);//concatenating string with 10
}}
Output:
3010
|
Could you provide Java String valueOf(boolean bol) Method Example? | This is a boolean version of overloaded valueOf() method. It takes boolean value and returns a string. Let's see an example.
public class StringValueOfExample2 {
public static void main(String[] args) {
// Boolean to String
boolean bol = true;
boolean bol2 = false; ... |
Could you provide Java String valueOf(char ch) Method Example? | This is a char version of overloaded valueOf() method. It takes char value and returns a string. Let's see an example.
public class StringValueOfExample3 {
public static void main(String[] args) {
// char to String
char ch1 = 'A';
char ch2 = 'B';
String s1 ... |
Can you provide an example of Java String valueOf(float f) and valueOf(double d)? | This is a float version of overloaded valueOf() method. It takes float value and returns a string. Let's see an example.
public class StringValueOfExample4 {
public static void main(String[] args) {
// Float and Double to String
float f = 10.05f;
double d = 10.02;
... |
Can you provide an example of Java String valueOf() Complete? | Let's see an example where we are converting all primitives and objects into strings.
public class StringValueOfExample5 {
public static void main(String[] args) {
boolean b1=true;
byte b2=11;
short sh = 12;
int i = 13;
long l = 14L;
float f ... |
What is Java Regex? | The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
It is widely used to define the constraint on strings such as password and email validation. After learning Java regex tutorial, you will be able to test your regular expressions by the Java Regex Tester Tool.
Ja... |
What are the java.util.regex package? | The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.
1.MatchResult interface
2.Matcher class
3.Pattern class
4.PatternSyntaxException class |
Wha is Matcher class? | Matcher class Method
Method: boolean matches()
Description: test whether the regular expression matches the pattern.
Method: boolean find()
Description: finds the next expression that matches the pattern.
Method: boolean find(int start)
Description: finds the next expression that matches the pattern from th... |
What is Pattern class? | It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.
Pattern Class Method
Method: static Pattern compile(String regex)
Description: compiles the given regex and returns the instance of the Pattern.
Method: Matcher matcher(CharSequence input)
Description: ... |
Could you provide Example of Java Regular Expressions? | There are three ways to write the regex example in Java.
import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way ... |
Could you provide Regular Expression . Example? | The . (dot) represents a single character.
import java.util.regex.*;
class RegexExample2{
public static void main(String args[]){
System.out.println(Pattern.matches(".s", "as"));//true (2nd char is s)
System.out.println(Pattern.matches(".s", "mk"));//false (2nd char is not s)
System.out.println(Patter... |
What are the Regex Character classes? | Regex Character Class methods
Method: [abc]
Description: a, b, or c (simple class)
Method: [^abc]
Description: Any character except a, b, or c (negation)
Method: [a-zA-Z]
Description: a through z or A through Z, inclusive (range)
Method: [a-d[m-p]]
Description: a through d, or m through p: [a-dm-p] (union)
Method... |
Could you provide Regular Expression Character classes Example? | import java.util.regex.*;
class RegexExample3{
public static void main(String args[]){
System.out.println(Pattern.matches("[amn]", "abcd"));//false (not a or m or n)
System.out.println(Pattern.matches("[amn]", "a"));//true (among a or m or n)
System.out.println(Pattern.matches("[amn]", "ammmna"));//false... |
What are the Regex Quantifiers? | The quantifiers specify the number of occurrences of a character.
Regex Description methods
Method: X?
Description: X occurs once or not at all
Method: X+
Description: X occurs once or more times
Method: X*
Description: X occurs zero or more times
Method: X{n}
Description: X occurs n times only
Method: X{n,}
De... |
Could you provide Regular Expression Character classes and Quantifiers Example? | import java.util.regex.*;
class RegexExample4{
public static void main(String args[]){
System.out.println("? quantifier ....");
System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time)
System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time) ... |
What are the Regex Metacharacters? | The regular expression metacharacters work as shortcodes.
Regex Metacharacters
Regex: .
Description: Any character (may or may not match terminator)
Regex: \d
Description: Any digits, short of [0-9]
Regex:
Description:
Regex: \D
Description: Any non-digit, short for [^0-9]
Regex: \s
Description:... |
Can you provide an example of Regular Expression Metacharacters? | import java.util.regex.*;
class RegexExample5{
public static void main(String args[]){
System.out.println("metacharacters d....");\\d means digit
System.out.println(Pattern.matches("\\d", "abc"));//false (non-digit)
System.out.println(Pattern.matches("\\d", "1"));//true (digit and comes once)
Syst... |
What is Exception Handling in Java? | The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained.
In this tutorial, we will learn about Java exceptions, it's types, and the difference between checked and unchecked exceptions. |
What is Exception in Java? | Dictionary Meaning: Exception is an abnormal condition.
In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. |
What is Exception Handling? | Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
|
What is the Advantage of Exception Handling? | The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions. Let's consider a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception o... |
What are the Types of Java Exceptions? | There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:
1.Checked Exception
2.Unchecked Exception
3.Error |
What is the Difference between Checked and Unchecked Exceptions? | 1) Checked Exception
The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions. For example, IOException, SQLException, etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that inherit the RuntimeException are known a... |
What is Java Exception Keywords? |
Java provides five keywords that are used to handle the exception. The following table describes each.
Java Exception Keywords
Keyword: try
Description: The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by ei... |
Can you provide an example of Java Exception Handling Example? | et's see an example of Java Exception Handling in which we are using a try-catch statement to handle the exception.
JavaExceptionExample.java
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(Ar... |
What are Common Scenarios of Java Exceptions? | There are given some scenarios where unchecked exceptions may occur. They are as follows:
1) A scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) A scenario where NullPointerException occurs
If we have a null val... |
What is the purpose of Java try block? | Java try block is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.
J... |
What is the function of a catch block in Java? | Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.
The catch block must be used ... |
what is the Problem without exception handling? | Let's try to understand the problem if we don't use a try-catch block.
public class TryCatchExample1 {
public static void main(String[] args) {
int data=50/0; //may throw exception
System.out.println("rest of the code");
}
}
Outpu... |
what is Java Multi-catch block? | A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block.
Points to remember
-At a time only one exception occurs and at a time only one catch bl... |
What is Java Nested try block? | In Java, using a try block inside another try block is permitted. It is called as nested try block. Every statement that we enter a statement in try block, context of that exception is pushed onto the stack.
For example, the inner try block can be used to handle ArrayIndexOutOfBoundsException while the outer try blo... |
Why use nested try block? | Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.
Syntax:
....
//main try block
try
{
statement 1;
statement 2;
//try catch block within another try... |
What is Java finally block? | Java finally block is a block used to execute important code such as closing the connection, etc.
Java finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not.
The finally block f... |
Why use Java finally block? | -finally block in Java can be used to put "cleanup" code such as closing a file, closing connection, etc.
-The important statements to be printed can be placed in the finally block. |
What is Java throw Exception? | In Java, exceptions allows us to write good quality codes where the errors are checked at the compile time instead of runtime and we can create custom exceptions making the code recovery and debugging easier. |
What is Java throw keyword? | The Java throw keyword is used to throw an exception explicitly.
We specify the exception object which is to be thrown. The Exception has some message with it that provides the error description. These exceptions may be related to user inputs, server, etc.
We can throw either checked or unchecked exceptions in Ja... |
What is Java Exception Propagation? | An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method. If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack. This is called exception... |
Can you give me an example of Exception Propagation? | Exception Propagation Example
TestExceptionPropagation1.java
class TestExceptionPropagation1{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(St... |
What is Java throws keyword? | The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception. So, it is better for the programmer to provide the exception handling code so that the normal flow of the program can be maintained.
Exception Handling is mainly used to handle the chec... |
What is the Advantage of Java throws keyword? | Now Checked Exception can be propagated (forwarded in call stack).
It provides information to the caller of the method about the exception. |
Can you give me an example of Java throws? | Let's see the example of Java throws clause which describes that checked exceptions can be propagated by throws keyword.
Testthrows1.java
import java.io.IOException;
class Testthrows1{
void m()throws IOException{
throw new IOException("device error");//checked exception
}
void n()throws I... |
What is the Difference between throw and throws in Java? | Difference between throw and throws in Java
Basis of Differences: Definition
Throw: Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code.
Throws: Java throws keyword is used in the method signature to declare an exception which might be thrown by the functi... |
What is the Difference between final, finally and finalize in Java? | Difference between final, finally and finalize
The final, finally, and finalize are keywords in Java that are used in exception handling. Each of these keywords has a different functionality. The basic difference between final, finally and finalize is that the final is an access modifier, finally is the block in E... |
Can you give an example of Java final? | FinalExampleTest.java
public class FinalExampleTest {
//declaring final variable
final int age = 18;
void display() {
// reassigning value to age variable
// gives compile time error
age = 55;
}
public static void main(String[] args) {
... |
Can you give me an example of Java finally? | Let's see the below example where the Java code throws an exception and the catch block handles that exception. Later the finally block is executed after the try-catch block. Further, the rest of the code is also executed normally.
FinallyExample.java
public class FinallyExample {
public static void ma... |
Can you give me an example of Java finalize? | FinalizeExample.java
public class FinalizeExample {
public static void main(String[] args)
{
FinalizeExample obj = new FinalizeExample();
// printing the hashcode
System.out.println("Hashcode is: " + obj.hashCode());
obj = null;
... |
What is Exception Handling with Method Overriding in Java? | There are many rules if we talk about method overriding with exception handling.
Some of the rules are listed below:
-If the superclass method does not declare an exception
If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchec... |
What is Java Custom Exception? | In Java, we can create our own exceptions that are derived classes of the Exception class. Creating our own Exception is known as custom exception or user-defined exception. Basically, Java custom exceptions are used to customize the exception according to user need.
Consider the example 1 in which InvalidAgeExcepti... |
Why use custom exceptions? | Java exceptions cover almost all the general type of exceptions that may occur in the programming. However, we sometimes need to create custom exceptions.
Following are few of the reasons to use custom exceptions:
-To catch and provide specific treatment to a subset of existing Java exceptions.
-Business logic except... |
What is Java Inner Classes (Nested Classes)? | Java inner class or nested class is a class that is declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.
Additionally, it can access all the members of the outer class, including private data members and methods.
... |
What is the Advantage of Java inner classes? | There are three advantages of inner classes in Java. They are as follows:
1.Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the outer class, including private.
2.Nested classes are used to develop more readable and maintainable code because... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.