Maruthi Krishna has Published 951 Articles

How to Convert a String to Hexadecimal and vice versa format in java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 08:01:08

14K+ Views

String to HexadecimalThe toHexString() method of the Integer class accepts an integer as a parameter and returns a hexadecimal string. Therefore, to convert a string to a hexadecimal String −Get the desired String.Create an empty StringBuffer object.Convert it into a character array using the toCharArray() method of the String class.Traverse ... Read More

How do we find out if first character of a string is a number in java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:58:57

3K+ Views

Using the isDigit() methodThe isDigit() method of the java.lang.Character class accepts a character as a parameter and determines whether it is a digit or not. If the given character is a digit this method returns true else, this method returns false.Therefore, to determine whether the first character of the given ... Read More

How to create a variable that can be set only once but isn't final in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:42:09

1K+ Views

Once you initialize a final variable you cannot modify its value further. i.e. You can assign value to a final variable only once. If you try to assign value to a final variable a compile time error will be generated.Example Live Demopublic class FinalExample {    final int j = 100; ... Read More

How can a String be validated (for alphabets) in java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:40:03

4K+ Views

To validate a string for alphabets you can either compare each character in the String with the characters in the English alphabet (both cases) or, use regular expressions.Example1The following program accepts a string value (name) from the user and finds out whether given string is a proper name by comparing ... Read More

Can a final variable be initialized when an object is created in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:38:33

835 Views

Once you declare a variable final, after initializing it, you cannot modify its value further. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them. If not a compile time error will be generated.Example Live Demopublic class ... Read More

How to copy a specific section of an array in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:38:13

278 Views

Using copyOf() methodThe copyOf() method of the Arrays class (java.util package) accepts two parameters −an array (of any type).an integer value representing length.And copies the contents of the given array from starting position to given length and returns the new array.Example Live Demoimport java.util.Arrays; public class CopyingSectionOfArray {    public static ... Read More

How to get first and last elements from ArrayList in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:35:41

14K+ Views

The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index.Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.size()-1 you ... Read More

How do we check if a String contains a substring (ignoring case) in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:26:41

4K+ Views

The contains() method of the String class accepts Sting value as a parameter, verifies whether the current String object contains the specified String and returns true if it does (else false).The toLoweCase() method of the String class converts all the characters in the current String into lower case and returns.To ... Read More

How to remove a SubList from an ArrayList in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:18:54

2K+ Views

Using the subList() and the clear() methodsThe subList() method of the List interface accepts two integer values representing indexes of the elements and, returns a view of the of the current List object removing the elements between the specified indices.The clear() method of List interface removes all the elements from ... Read More

Sorting contents of a string that holds integer values in Java

Maruthi Krishna

Maruthi Krishna

Updated on 11-Oct-2019 07:04:11

3K+ Views

To sort the string that contains numbers in java −Get the String.Create an empty integer array.The split() method of the string class accepts a string representing a delimiter, splits the current string into an array of tokens. Using this method split the given string into an array of tokens.The parseInt() ... Read More

Advertisements