Question
stringlengths
1
113
Answer
stringlengths
22
6.98k
How to convert Java String to float?
We can convert String to float in java using
Can you provide an example of Java string to float?
public class StringToFloatExample{ public static void main(String args[]){ String s="23.6"; float f=Float.parseFloat("23.6"); System.out.println(f); }} Output: 23.6
How to convert Java float to String?
We can convert float to String in java using String.valueOf() and Float.toString() methods.
Can you provide an example of Java float to String using String.valueOf()?
public class FloatToStringExample1{ public static void main(String args[]){ float f=12.3F;//F is the suffix for float String s=String.valueOf(f); System.out.println(s); }} Output: 12.3
How to convert Java String to double?
We can convert String to double in java using Double.parseDouble() method.
Can you provide an example of Java String to double?
public class StringToDoubleExample{ public static void main(String args[]){ String s="23.6"; double d=Double.parseDouble("23.6"); System.out.println(d); }} Output: 23.6
How to convert Java double to String?
We can convert double to String in java using String.valueOf() and Double.toString() methods.
Can you provide an example of Java double to String using String.valueOf()?
public class DoubleToStringExample1{ public static void main(String args[]){ double d=12.3; String s=String.valueOf(d); System.out.println(s); }} Output: 12.3
How to convert Java String to charHow?
We can convert String to char in java using charAt() method of String class. Java Convert String to char The charAt() method returns a single character only. To get all characters, you can use loop.
Can you provide an example of Java string to char: charAt() method?
public class StringToCharExample1{ public static void main(String args[]){ String s="hello"; char c=s.charAt(0);//returns h System.out.println("1st character is: "+c); }} Output: 1st character is: h
How to convert Java char to String?
We can convert String to char in java using charAt() method of String class. Java Convert String to char The charAt() method returns a single character only. To get all characters, you can use loop.
Can you provide an example of Java char to String using String.valueOf() method?
public class CharToStringExample1{ public static void main(String args[]){ char c='S'; String s=String.valueOf(c); System.out.println("String is: "+s); }} Output: String is: S
How to convert Java String to Object?
We can convert String to Object in java with assignment operator. Each class is internally a child class of Object class. So you can assign string to Object directly.
Can you provide an example of Java String to Object?
public class StringToObjectExample{ public static void main(String args[]){ String s="hello"; Object obj=s; System.out.println(obj); }} Output: hello
How to convert Java Object to String?
We can convert Object to String in java using toString() method of Object class or String.valueOf(object) method.
Can you provide an example of Java Object to String using Converting User-defined class?
class Emp{} public class ObjectToStringExample{ public static void main(String args[]){ Emp e=new Emp(); String s=e.toString(); String s2=String.valueOf(e); System.out.println(s); System.out.println(s2); }} Output:
How to convert Java int to long?
We can convert int to long in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly.
Can you provide an example of Java int to long?
public class IntToLongExample1{ public static void main(String args[]){ int i=200; long l=i; System.out.println(l); }}
How to convert Java long to int?
We can convert long to int in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype). Here, we are going to learn how to convert long primitive type into int and Long object into int.
Can you provide an example of Java long to int?
public class LongToIntExample2{ public static void main(String args[]){ Long l= new Long(10); int i=l.intValue(); System.out.println(i); }} Output:
How to convert Java int to double?
We can convert int to double in java using assignment operator. There is nothing to do extra because lower type can be converted to higher type implicitly.
Can you provide an example of Java int to double?
public class IntToDoubleExample1{ public static void main(String args[]){ int i=200; double d=i; System.out.println(d); }} Output:
How to convert Java double to int?
We can convert double to int in java using typecasting. To convert double data type into int, we need to perform typecasting.
Can you provide an example of Java double to int: Typecasting?
public class DoubleToIntExample1{ public static void main(String args[]){ double d=10.5; int i=(int)d; System.out.println(i); }} Output:
How to convert Java char to int?
We can convert char to int in java using various ways. If we direct assign char variable to int, it will return ASCII value of given character.
Java char to int Example: Get ASCII value
public class CharToIntExample1{ public static void main(String args[]){ char c='a'; char c2='1'; int a=c; int b=c2; System.out.println(a); System.out.println(b); }} Output: 97 49
How to convert Java int to char?
We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable.
Can you provide an example of Java int to char?
public class IntToCharExample1{ public static void main(String args[]){ int a=65; char c=(char)a; System.out.println(a); }} Output: A
How to convert Java String to boolean?
We can convert String to boolean in java using Boolean.parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns instance of Boolean class. To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE" will return boo...
Can you provide an example of Java String to boolean using Boolean.parseBoolean()?
public class StringToBooleanExample{ public static void main(String args[]){ String s1="true"; String s2="TRue"; String s3="ok"; boolean b1=Boolean.parseBoolean(s1); boolean b2=Boolean.parseBoolean(s2); boolean b3=Boolean.parseBoolean(s3); System.out.println(b1); System.out.println(b2); Sy...
How to convert Java boolean to String?
We can convert boolean to String in java using String.valueOf(boolean) method. Alternatively, we can use Boolean.toString(boolean) method which also converts boolean into String.
Can you provide an example of Java boolean to String using String.valueOf()?
public class BooleanToStringExample1{ public static void main(String args[]){ boolean b1=true; boolean b2=false; String s1=String.valueOf(b1); String s2=String.valueOf(b2); System.out.println(s1); System.out.println(s2); }} Output: true false
How to convert Java Date to Timestamp?
We can convert Date to Timestamp in java using constructor of java.sql.Timestamp class. The constructor of Timestamp class receives long value as an argument. So you need to convert date into long value using getTime() method of java.util.Date class. You can also format the output of Timestamp using java.text.Sim...
Can you provide an example of Java Date to Timestamp?
import java.sql.Timestamp; import java.util.Date; public class DateToTimestampExample1 { public static void main(String args[]){ Date date = new Date(); Timestamp ts=new Timestamp(date.getTime()); System.out.println(ts); ...
How to convert Java Timestamp to Date?
We can convert Timestamp to Date in java using constructor of java.util.Date class. The constructor of Date class receives long value as an argument. So, you need to convert Timestamp object into long value using getTime() method of java.sql.Timestamp class. Let's see the constructor of Date class and signature o...
Can you provide an example of Java Timestamp to Date?
import java.sql.Timestamp; import java.util.Date; public class TimestampToDateExample1 { public static void main(String args[]){ Timestamp ts=new Timestamp(System.currentTimeMillis()); Date date=new Date(ts.getTime()); System.out.println(...
How to convert Binary to Decimal?
We can convert binary to decimal in java using Integer.parseInt() method or custom logic.
How to convert Java Binary to Decimals using Integer.parseInt()?
We can convert binary to decimal in java using Integer.parseInt() method or custom logic.
How to convert Decimal to Binary?
We can convert decimal to binary in java using Integer.toBinaryString() method or custom logic.
How to convert Java Decimal to Binary using conversion: Integer.toBinaryString()?
The Integer.toBinaryString() method converts decimal to binary string. The signature of toBinaryString() method is given below:
How to convert Hexadecimal to Decimal?
We can convert hexadecimal to decimal in java using Integer.parseInt() method or custom logic.
What is Java Hexadecimal to Decimal conversion: Integer.parseInt()?
The Integer.parseInt() method converts string to int with given redix. The signature of parseInt() method is given below:
How to convert Java Decimal to Hexadecimal?
We can convert decimal to hexadecimal in java using Integer.toHexString() method or custom logic.
How to convert Java Decimal to Hex using Interger.toHexString() method?
The Integer.toHexString() method converts decimal to hexadecimal. The signature of toHexString() method is given below: public static String toHexString(int decimal)
How to convert Java Octal to Decimal?
We can convert octal to decimal in java using Integer.parseInt() method or custom logic.
How to convert Java Octal to Decimal conversion: Integer.parsenInt()?
he Integer.parseInt() method converts a string to an int with the given radix. If you pass 8 as a radix, it converts an octal string into decimal. Let us see the signature of parseInt() method:
How to convert Java Decimal to Octal?
We can convert decimal to octal in java using Integer.toOctalString() method or custom logic.
Java Decimal to Octal conversion: Integer.toOctalString()
The Integer.toOctalString() method converts decimal to octal string. The signature of toOctalString() method is given below: public static String toOctalString(int decimal)
What is Collections in Java?
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Col...
What is Collection in Java?
A Collection represents a single unit of objects, i.e., a group.
What is a framework in Java?
It provides readymade architecture. It represents a set of classes and interfaces. It is optional.
What is Collection framework?
The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has: 1. Interfaces and its implementations, i.e., classes 2. Algorithm
What are the Methods of Collection interface?
Methods of Collection interface There are many methods declared in the Collection interface. They are as follows: Method: public boolean add(E e) Description: It is used to insert an element in this collection. Method: public boolean addAll(Collection<? extends E> c) Description: It is used to insert the spe...
What is Iterator interface?
Iterator interface provides the facility of iterating the elements in a forward direction only.
What are the Methods of Iterator interface?
Methods of Iterator interface There are only three methods in the Iterator interface. They are: Method: public boolean hasNext() Description:It returns true if the iterator has more elements otherwise it returns false. Method: public Object next() Description:It returns the element and moves the cursor point...
What is iterable Interface?
he Iterable interface is the root interface for all the collection classes. The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface. It contains only one abstract method. i.e., Iterator<T> iterator() It returns the i...
What is Collection Interface?
The Collection interface is the interface which is implemented by all the classes in the collection framework. It declares the methods that every collection will have. In other words, we can say that the Collection interface builds the foundation on which the collection framework depends. Some of the methods of Coll...
What is Java ArrayList?
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in the java.util package. It is like the Vector in C++. The ArrayList in Java can have the dup...
What is the class declaration for ArrayList class?
Let's see the declaration for java.util.ArrayList class. public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
What are the Constructors of the ArrayList?
Constructors of ArrayList Constructor: ArrayList() Description: It is used to build an empty array list. Constructor: ArrayList(Collection<? extends E> c) Description: It is used to build an array list that is initialized with the elements of the collection c. Constructor: ArrayList(int capacity) Descrip...
What are the Methods of ArrayList?
Methods of ArrayList Method: void add(int index, E element) Description: It is used to insert the specified element at the specified position in a list. Method: boolean add(E e) Description: It is used to append the specified element at the end of a list. Method: boolean addAll(Collection<? extends...
What are the differences between Java Non-generic and Generic Collection?
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic. Java new generic collection allows you to have only one type of object in a collection. Now it is type-safe, so typecasting is not required at runtime. Let's see the old non-generic example of creating a Java collection. ArrayL...
Can you provide an example of Java ArrayList?
FileName: ArrayListExample1.java import java.util.*; public class ArrayListExample1{ public static void main(String args[]){ ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add("Mango");//Adding object in arraylist list.add("Apple"); list.add("Ba...
How to Sort ArrayList?
The java.util package provides a utility class Collections, which has the static method sort(). Using the Collections.sort() method, we can easily sort the ArrayList. import java.util.*; class SortArrayList{ public static void main(String args[]){ //Creating a list of fruits List<String> list1=new A...
What are the ways to iterate the elements of the collection in Java?
There are various ways to traverse the collection elements: 1.By Iterator interface. 2.By for-each loop. 3.By ListIterator interface. 4.By for loop. 5.By forEach() method. 6.By forEachRemaining() method.
Can you provide an example of traversing the ArrayList elements?
Let's see an example to traverse the ArrayList elements through other ways FileName: ArrayList4.java import java.util.*; class ArrayList4{ public static void main(String args[]){ ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add("Ravi");//Adding object in arr...
Can you provide an example of User-defined class objects in Java ArrayList?
Let's see an example where we are storing Student class object in an array list. FileName: ArrayList5.java class Student{ int rollno; String name; int age; Student(int rollno,String name,int age){ this.rollno=rollno; this.name=name; this.age=age; } } import java.ut...
Can you provide an example of Java ArrayList Serialization and Deserialization?
Let's see an example to serialize an ArrayList object and then deserialize it. FileName: ArrayList6.java import java.io.*; import java.util.*; class ArrayList6 { public static void main(String [] args) { ArrayList<String> al=new ArrayList<String>(); al.ad...
Can you provide an example of Java ArrayList to add elements?
Here, we see different ways to add an element. FileName: ArrayList7.java import java.util.*; class ArrayList7{ public static void main(String args[]){ ArrayList<String> al=new ArrayList<String>(); System.out.println("Initial list of elements: "+al); //Adding elements to th...
Can you provide an example of Java ArrayList to remove elements?
Here, we see different ways to remove an element. FileName: ArrayList8.java import java.util.*; class ArrayList8 { public static void main(String [] args) { ArrayList<String> al=new ArrayList<String>(); al.add("Ravi"); al.add("Vijay"); ...
What are the important points in Java LinkedList class?
Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are: -Java LinkedList class can contain duplicate elements. -Java LinkedList cla...
What is the class declaration for LinkedList class?
Let's see the declaration for java.util.LinkedList class. public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
What are the Constructors of Java LinkedList?
Constructor: LinkedList() Description: It is used to construct an empty list. Constructor: LinkedList(Collection<? extends E> c) Description: It is used to construct a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.
What are the Methods of Java LinkedList?
Method: boolean add(E e) Description: It is used to append the specified element to the end of a list. Method: void add(int index, E element) Description: It is used to insert the specified element at the specified position index in a list. Method: boolean addAll(Collection<? extends E> c) Description: It is u...
Can you provide an example of Java LinkedList?
import java.util.*; public class LinkedList1{ public static void main(String args[]){ LinkedList<String> al=new LinkedList<String>(); al.add("Ravi"); al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); Iterator<String> itr=al.iterator(); while(itr.hasNext()){ System....
What are the difference between ArrayList and LinkedList?
ArrayList and LinkedList both implement the List interface and maintain insertion order. Both are non-synchronized classes. However, there are many differences between the ArrayList and LinkedList classes that are given below. 1) ArrayList internally uses a dynamic array to store the elements while LinkedList i...
Can you provide an example of ArrayList and LinkedList in Java?
Let's see a simple example where we are using ArrayList and LinkedList both. FileName: TestArrayLinked.java import java.util.*; class TestArrayLinked{ public static void main(String args[]){ List<String> al=new ArrayList<String>();//creating arraylist al.add("Ravi");//adding object...
What is Java List?
List in Java provides the facility to maintain the ordered collection. It contains the index-based methods to insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the null elements in the list. The List interface is found in the java.util package and inherits the...
What are the methods of Java List?
Method: void add(int index, E element) Description: It is used to insert the specified element at the specified position in a list. Method: boolean add(E e) Description: It is used to append the specified element at the end of a list. Method: boolean addAll(Collection<? extends E> c) Description: It is used to...
What is Java List and ArrayList?
List is an interface whereas ArrayList is the implementation class of List.
How to create a List in Java?
The ArrayList and LinkedList classes provide the implementation of List interface. Let's see the examples to create the List: //Creating a List of type String using ArrayList List<String> list=new ArrayList<String>(); //Creating a List of type Integer using ArrayList List<Integer> list=new ArrayList<Integer...
Can you provide an example of Java List?
Let's see a simple example of List where we are using the ArrayList class as the implementation. import java.util.*; public class ListExample1{ public static void main(String args[]){ //Creating a List List<String> list=new ArrayList<String>(); //Adding elements in the List list.add("Mango"); ...
How to convert Array to List?
We can convert the Array to List by traversing the array and adding the element in list one by one using list.add() method. Let's see a simple example to convert array elements into List. import java.util.*; public class ArrayToListExample{ public static void main(String args[]){ //Creating Array String[...
How to convert List to Array?
We can convert the List to Array by calling the list.toArray() method. Let's see a simple example to convert list elements into array. import java.util.*; public class ListToArrayExample{ public static void main(String args[]){ List<String> fruitList = new ArrayList<>(); fruitList.add("Mango"); ...
What is Get and Set Element in List?
The get() method returns the element at the given index, whereas the set() method changes or replaces the element. import java.util.*; public class ListExample2{ public static void main(String args[]){ //Creating a List List<String> list=new ArrayList<String>(); //Adding elements in the List l...
How to Sort List?
There are various ways to sort the List, here we are going to use Collections.sort() method to sort the list element. The java.util package provides a utility class Collections which has the static method sort(). Using the Collections.sort() method, we can easily sort any List. import java.util.*; class SortArray...
What is Java ListIterator Interface?
ListIterator Interface is used to traverse the element in a backward and forward direction.
What is the class declaration for Listlterator Interface?
public interface ListIterator<E> extends Iterator<E>
What are the Methods of Java ListIterator Interface?
Method: void add(E e) Description: This method inserts the specified element into the list. Method: boolean hasNext() Description: This method returns true if the list iterator has more elements while traversing the list in the forward direction. Method: E next() Description: This method returns the next element in t...
Can you provide an example of ListIterator Interface?
import java.util.*; public class ListIteratorExample1{ public static void main(String args[]){ List<String> al=new ArrayList<String>(); al.add("Amit"); al.add("Vijay"); al.add("Kumar"); al.add(1,"Sachin"); ListIterator<String> itr=al.listIterator...
Can you provide an example of List: Book?
Let's see an example of List where we are adding the Books. import java.util.*; class Book { int id; String name,author,publisher; int quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name = name; this.author = author; ...
What are the important points about Java HashSet?
Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface. The important points about Java HashSet class are: -HashSet stores the elements by using a mechanism called hashing. -HashSet contains unique elements only. -Hash...
What is the Difference between List and Set?
A list can contain duplicate elements whereas Set contains unique elements only.
What are the Constructors of the Java HashSet class?
Sn:1 Constructor: HashSet() Description: It is used to construct a default HashSet. Sn:2 Constructor: HashSet(int capacity) Description: It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet. Sn:3 Const...
What are the Methods of Java HashSet class?
Various methods of Java HashSet class are as follows: Sn:1 Method and Type: boolean Method: add(E e) Description: It is used to add the specified element to this set if it is not already present. Sn:2 Method and Type: void Method: clear() Description: It is used to remove all of the elements from the set....
Can you provide an example of Java HashSet?
Let's see a simple example of HashSet. Notice, the elements iterate in an unordered collection. import java.util.*; class HashSet1{ public static void main(String args[]){ //Creating HashSet and adding elements HashSet<String> set=new HashSet(); set.add("One"); set.ad...
What is the important points about the Java LinkedHashSet Class?
Java LinkedHashSet class is a Hashtable and Linked list implementation of the Set interface. It inherits the HashSet class and implements the Set interface. The important points about the Java LinkedHashSet class are: -Java LinkedHashSet class contains unique elements only like HashSet. -Java LinkedHashSet cla...
What is the class declaration for LinkedHashSet class?
Let's see the declaration for java.util.LinkedHashSet class. public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
What are the Constructors of the Java LinkedHashSet class?
Constructor: HashSet() Description: It is used to construct a default HashSet. Constructor: HashSet(Collection c) Description: It is used to initialize the hash set by using the elements of the collection c. Constructor: LinkedHashSet(int capacity) Description: It is used to initialize the capacity of the link...
Can you provide an example of Java LinkedHashSet?
Let's see a simple example of the Java LinkedHashSet class. Here you can notice that the elements iterate in insertion order. FileName: LinkedHashSet1.java import java.util.*; class LinkedHashSet1{ public static void main(String args[]){ //Creating HashSet and adding elements LinkedHashSet<S...